Electrical – Timer in ATmega2560

atmegaavrtimer

//TIMER4 initialize - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value:  1.000Hz (0.0%)
void timer4_init(void)
{
     TCCR4B = 0x00; //stop
     TCNT4H = 0x1F; //Counter higher 8 bit value
     TCNT4L = 0x01; //Counter lower 8 bit value
     OCR4AH = 0x00; //Output Compair Register (OCR)- Not used
     OCR4AL = 0x00; //Output Compair Register (OCR)- Not used
     OCR4BH = 0x00; //Output Compair Register (OCR)- Not used
     OCR4BL = 0x00; //Output Compair Register (OCR)- Not used
     OCR4CH = 0x00; //Output Compair Register (OCR)- Not used
     OCR4CL = 0x00; //Output Compair Register (OCR)- Not used
     ICR4H  = 0x00; //Input Capture Register (ICR)- Not used
     ICR4L  = 0x00; //Input Capture Register (ICR)- Not used
     TCCR4A = 0x00; 
     TCCR4C = 0x00;
     TCCR4B = 0x04; //start Timer
}

I'm trying to figure how to use the Timer 4 in ATmega2560. This is the code I've found for a 1 sec timer. A timer interrupt takes places after every 1 sec. How can I use this code to achieve a 0.1 sec timer (i.e. schedule the ISR for every 0.1s)?

Best Answer

Using a timer is easier than it sounds. It works like this:

You have a timer that is fed with a clock. The clock can be the system clock, an external source (not available on every AVR) or the system clock divided by a pre scaler. You can trigger an ISR on an overflow of the timer. The timer-register can be 8 or 16bit wide. (I am not sure if 32 bit wide timer register exist)

Example:

Using a 8 bit timer with a 1MHz timer clock will give you an overflow every 255us, meaning your ISR would be called every 255us:

enter image description here

There is also an option to adjust the trigger threshold with registers so you are able to trigger on different values than the overflow value, like 127us. There is also another way to achieve this: initialize the timer counter value to a value other than zero and wait for the overflow to happen. If the overflow occurred you reset the timer to your start position again.

Short side note: If your ISR takes longer than your ISR interval then you will miss some ISRs!

If you are going to use timers then you always have to consider the following things:

  • What frequency do I need?
  • What duty-cycle do I want? (for PWM applications)
  • What system clock am I using?
  • Do I want an ISR to be triggered or to toggle an output pin?

If you can answer all these questions then you can move forward!

The data sheet gives all answers on how to setup the timer:

You want to use timer 4 on the ATmega2560 which is 16 bits wide (source: page 158 of the complete ATmega2560 datasheet).

Then you need to calculate a fitting pre scaler for your clock and calculate a fitting timer overflow threshold value...

I know the data sheet can sometimes be misleading at first glance but give yourself a little time and you will understand it!