Difference between revisions of "AVR Timer Interrupts"
Line 1: | Line 1: | ||
[[Category:AVR Tutorials]] | [[Category:AVR Tutorials]] | ||
=Basics= | =Basics= | ||
− | 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 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. |
+ | Let us repeat the same example of generating a 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. | ||
{| style="text-align:center;" class="table table-responsive table-bordered" | {| style="text-align:center;" class="table table-responsive table-bordered" |
Revision as of 15:06, 22 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.
Let us repeat the same example of generating a 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.
TIMSK | |||||||
---|---|---|---|---|---|---|---|
D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
- | - | TICIE1 | OCIE1A | OCIE1B | TOIE1 | - | - |
Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<avr/io.h> | |
#include<avr/interrupt.h> | |
#define LED PD4 | |
ISR (TIMER1_OVF_vect) // Timer1 ISR | |
{ | |
PORTD ^= (1 << LED); | |
TCNT1 = 63974; // for 1 sec at 16 MHz | |
} | |
int main() | |
{ | |
DDRD = (0x01 << LED); //Configure the PORTD4 as output | |
TCNT1 = 63974; // for 1 sec at 16 MHz | |
TCCR1A = 0x00; | |
TCCR1B = (1<<CS10) | (1<<CS12);; // Timer mode with 1024 prescler | |
TIMSK = (1 << TOIE1) ; // Enable timer1 overflow interrupt(TOIE1) | |
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG | |
while(1) | |
{ | |
} | |
} | |