Line 3: Line 3:
 
We have covered the basics of [[AVR_Timer_programming| AVR timer Programming]], during the Timer 1 example we saw that we had to monitor the Flags to check if the Timer has overflown which made the main program dependent on the status of the flags. Timers are independent unit's inside a micro-controller and to  make full use of them we will configure and use them with Interrupts. This makes the CPU free from polling the flags and timers can operate on their own. When the timing task is finished, it will interrupt and inform the CPU.  
 
We have covered the basics of [[AVR_Timer_programming| AVR timer Programming]], during the Timer 1 example we saw that we had to monitor the Flags to check if the Timer has overflown which made the main program dependent on the status of the flags. Timers are independent unit's inside a micro-controller and to  make full use of them we will configure and use them with Interrupts. This makes the CPU free from polling the flags and timers can operate on their own. When the timing task is finished, it will interrupt and inform the CPU.  
 
   
 
   
 +
For timer zero, the count goes from 0 to 255 and rolls over. With that the timer over flow flag is set and it can be used to trigger an interrupt.
 +
[[File:0 timer Interrupt.gif|x300px|center]]
 
Let us repeat the same example of blinking a LED connected to PD4 at 100ms delay with Timer 1 but this time using Interrupts. We have covered the [[Basics of AVR Interrupts]], you may wish to go through it first.  
 
Let us repeat the same example of blinking a LED connected to PD4 at 100ms delay with Timer 1 but this time using Interrupts. We have covered the [[Basics of AVR Interrupts]], you may wish to go through it first.  
  

Revision as of 15:46, 24 March 2016

Basics

We have covered the basics of AVR timer Programming, during the Timer 1 example we saw that we had to monitor the Flags to check if the Timer has overflown which made the main program dependent on the status of the flags. Timers are independent unit's inside a micro-controller and to make full use of them we will configure and use them with Interrupts. This makes the CPU free from polling the flags and timers can operate on their own. When the timing task is finished, it will interrupt and inform the CPU.

For timer zero, the count goes from 0 to 255 and rolls over. With that the timer over flow flag is set and it can be used to trigger an interrupt.

0 timer Interrupt.gif

Let us repeat the same example of blinking a LED connected to PD4 at 100ms delay with Timer 1 but this time using Interrupts. We have covered the Basics of AVR Interrupts, you may wish to go through it first.

All the calculations from the previous tutorial hold good. Except, since we are using the Timer OverFlow flag, the value 65536 - 1562 = 63974 is loaded in TCNT1.

Steps to configure the Timer Interrupt:

  1. Load the TCNT1 register with the value calculated above.
  2. Set CS10 and CS12 bits to configure pre-scalar of 1024
  3. Enable timer1 overflow interrupt(TOIE1), the register is shown below
  4. Enable global interrupts by setting global interrupt enable bit in SREG
  5. Toggle the LED in the ISR and reload the TCNT value.


TIMSK
Timer/Counter Interrupt Mask Register
D7 D6 D5 D4 D3 D2 D1 D0
- - TICIE1 OCIE1A OCIE1B TOIE1 - -

Code

Look at the while(1) loop, it is happy that there is no code to execute in there ;)