Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Help with timers and c++
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help with timers and c++

by rustycage55 on Fri Feb 21, 2020 6:17 pm

Hi everyone

I've been following the multitasking course in the Adafruit learn space. Here is the link in case you want to read it.

[url]https://learn.adafruit.com/multi-tasking-the-arduino-part-2?view=all
[/url]

After battling for hours with object oriented programming and classes and stuff, i found some coding that i simply can't understand and i hope someone could explain it to me.

Code: Select all | TOGGLE FULL SIZE
void setup()
{
  sweeper1.Attach(9);
  sweeper2.Attach(10);
 
  // Timer0 is already used for millis() - we'll just interrupt somewhere
  // in the middle and call the "Compare A" function below
  OCR0A = 0xAF;
  TIMSK0 |= _BV(OCIE0A);
 
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(0, Reset, FALLING);
}
 
void Reset()
{
  sweeper1.reset();
  sweeper2.reset();
}
 
// Interrupt is called once a millisecond,
SIGNAL(TIMER0_COMPA_vect)
{
  unsigned long currentMillis = millis();
  sweeper1.Update(currentMillis);
 
  //if(digitalRead(2) == HIGH)
  {
     sweeper2.Update(currentMillis);
     led1.Update(currentMillis);
  }
 
  led2.Update(currentMillis);
  led3.Update(currentMillis);
}


Need help with the next lines

OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);

SIGNAL(TIMER0_COMPA_vect)

Any help would be appreciated. As far as i could research, the _bv part is a bitwise operation but i dont get the TIMSK0 part. Also, is this still c++ code? it looks different than regular functions.

Thanks in advance!
Italo

rustycage55
 
Posts: 7
Joined: Fri Sep 22, 2017 2:35 pm

Re: Help with timers and c++

by adafruit_support_bill on Fri Feb 21, 2020 7:32 pm

This code is specific to the Arduino UNO and similar Atmega 328-based Arduino compatibles. It is setting a bit (OCIE0A) in the timer mask register (TIMSK0 ) of the Atmega328 to generate the interrupt.

adafruit_support_bill
 
Posts: 78972
Joined: Sat Feb 07, 2009 10:11 am

Re: Help with timers and c++

by rustycage55 on Sat Feb 22, 2020 11:18 am

adafruit_support_bill wrote:This code is specific to the Arduino UNO and similar Atmega 328-based Arduino compatibles. It is setting a bit (OCIE0A) in the timer mask register (TIMSK0 ) of the Atmega328 to generate the interrupt.


Thank you very much for your response. Didn't know about the existence of some specific commands or functions to work with the arduino UNO. By any chance, do you know where can i get more info about them? maybe a list of the specific commands and uses?

Regards!
Italo

rustycage55
 
Posts: 7
Joined: Fri Sep 22, 2017 2:35 pm

Re: Help with timers and c++

by adafruit_support_bill on Sat Feb 22, 2020 12:15 pm

The ultimate reference is the Atmega328 datasheet. Where "datasheet" is a rather euphemistic term for a 350 page tome: https://www.mouser.com/datasheet/2/268/ ... 315184.pdf

There are a few good on-line tutorials that break down the timer-specific parts in an easier to understand format. Here are a couple:
https://oscarliang.com/arduino-timer-an ... -tutorial/
https://circuitdigest.com/microcontroll ... r-tutorial

adafruit_support_bill
 
Posts: 78972
Joined: Sat Feb 07, 2009 10:11 am

Re: Help with timers and c++

by rustycage55 on Sun Feb 23, 2020 6:22 pm

adafruit_support_bill wrote:The ultimate reference is the Atmega328 datasheet. Where "datasheet" is a rather euphemistic term for a 350 page tome: https://www.mouser.com/datasheet/2/268/ ... 315184.pdf

There are a few good on-line tutorials that break down the timer-specific parts in an easier to understand format. Here are a couple:
https://oscarliang.com/arduino-timer-an ... -tutorial/
https://circuitdigest.com/microcontroll ... r-tutorial



Thank you! very interesting links. This opens a whole new world of posibilities with the arduino. Never thought i would get this far lol.

Also, now i see what you mean about the atmega 328 datasheet, it's scary.

Have a good day!

rustycage55
 
Posts: 7
Joined: Fri Sep 22, 2017 2:35 pm

Please be positive and constructive with your questions and comments.