Timer Interrupts On The AVR in C Simple Example: STK500 Setup
Timer Interrupts On The AVR in C Simple Example: STK500 Setup
com Making Electronics Fun
Timer Interrupts on the AVR in C Simple Example
Timer Interrupts
If you are just stumbling on this page, you might want to check out the entire series of AVR related
programming guides here.
Timer interrupts are an excellent way of having your AVR do something at a given interval. They can
fire off and interrupt what ever else the AVR is doing making for very precise timing. They are one of
the best ways to implement custom waveforms for things such as positioning robot servos, dimming
LED's, and driving speakers at different frequencies.
STK500 Setup
For this example, make sure that you have your PORTA jumpered to LEDs, as was discussed in
ourPort Output guide.
The Interrupt Header
In order to use the built in interrupt features in WinAVR you need to include the interrupt header like
this:
#include <avr/interrupt.h>
The ISR keyword
WinAVR uses the keyword ISR to denote an Interrupt Service Routine. We need to define
theISR for timer1 overflow. You do it like this:
// timer1 overflw
ISR(TIMER1_OVF_vect) {
// process the timer1 overflow here
}
Turning on the Timer Interrupt
In order for the interrupt to fire, you must enable it in the TIMSK register. To enable
both timer0and timer1 interrupts, use the following code in main:
// enable timer overflow interrupt for both Timer0 and Timer1
TIMSK=(1<<TOIE0) | (1<<TOIE1);
Setup Your Timer
Once you have your ISR defined, and you have enabled the interrupt in TIMSK, you can setup your
timer how ever you want. When the timer rolls over, the interrupt will be fired. Here is an example of
setting up timer0 to count from 0 to 255, with a divide by 1024 prescaler. This will make
thetimer0 interrupt fire 30.63 times every second with a frequency of 8.0MHz (8,000,000 / 255 /
1024 = 30.63).
// set timer0 counter initial value to 0
TCNT0=0x00;
// start timer0 with /1024 prescaler
TCCR0 = (1<<CS02) | (1<<CS00);
Enable Interrupts
http://efundies.com/avr/avr_timer_interupts_c_simple.htm 1/3
12/14/2015 Efundies.com Making Electronics Fun
This step is easy, simply call sei(); to turn on the global interrupt enable flag.
A Simple Example
Here is a simple example that turns on both timer0 and timer1. It accomplishes the following:
Sets up timer0 in divide by 1024 mode, counting from 0 to 255
Sets up timer1 in divide by 1024 mode, counting from 0 to 65,535
On timer0 interrupt, toggles PORTA bit 0.
On timer1 interrupt, toggles PORTA bit 1.
You will see PORTA bit 0 blinking on and off 15 times / second, and PORTA bit 1 blinking on and
off every 8.3 seconds.
/********************************************************************************
Includes
********************************************************************************/
#include <avr/io.h>
#include <stdbool.h>
#include <avr/interrupt.h>
/********************************************************************************
Interrupt Routines
********************************************************************************/
// timer1 overflw
ISR(TIMER1_OVF_vect) {
// XOR PORTA with 0x02 to toggle the LSB
PORTA=PORTA ^ 0x02;
}
// timer0 overflow
ISR(TIMER0_OVF_vect) {
// XOR PORTA with 0x01 to toggle the second bit up
PORTA=PORTA ^ 0x01;
}
/********************************************************************************
Main
********************************************************************************/
int main( void ) {
// Configure PORTA as output
DDRA = 0xFF;
PORTA = 0xFF;
// enable timer overflow interrupt for both Timer0 and Timer1
TIMSK=(1<<TOIE0) | (1<<TOIE1);
// set timer0 counter initial value to 0
TCNT0=0x00;
// start timer0 with /1024 prescaler
TCCR0 = (1<<CS02) | (1<<CS00);
// lets turn on 16 bit timer1 also with /1024
TCCR1B |= (1 << CS10) | (1 << CS12);
// enable interrupts
sei();
while(true) {
}
}
You can download the complete source code here.
This program compiles down to 246 bytes for us.
Next Up, A More Complicated Example
If you want to see some more timer interrupt examples, then check out our More Complex Timer
http://efundies.com/avr/avr_timer_interupts_c_simple.htm 2/3
12/14/2015 Efundies.com Making Electronics Fun
Interrupt Functions guide.
Or head back to our index of AVR Guides here.
Written by Jason Bauer
Jason Bauer is an owner and programmer for Portforward.com. He's allergic
to twitter andfacebook, but you can find more of his articles in the Guides section.
http://efundies.com/avr/avr_timer_interupts_c_simple.htm 3/3