How to Implement AVR Timer Interrupts in C for Atmel Microcontrollers

Thumbnail image of Jason Bauer
Jason Bauer
December 01, 2015 (Last Updated: ) | Reading Time: 3 minutes

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 our Port 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 the ISR for timer1 overflow. You do it like this:

// timer1 overflow
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 timer0 and 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 however 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 the timer0 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

This step is easy, simply call sei (); to turn on the global interrupt enable flag.

AVR Timer Interrupts Example

Here is a simple example that turns on both timer0 and timer1. It accomplishes the following:

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 
#include <avr/interrupt.h>
 
// ********************************************************************************
// Interrupt Routines
// ********************************************************************************
// timer1 overflow
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);
	// let's 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 Interrupt Functions guide. Or head back to our index of AVR Guides here.

Comments

avatar

Prof. Dattaraj Vidyasagar - 2016-04-09 03:28:04

reply

Wow. Thank you so much. You site is just great Thanks Tell me how to subscribe to your updates?

avatar

Ganesh - 2016-04-27 05:15:49

reply

When I ran this code, I getting an error ? as

In function __vector_16': 49: multiple definition of __vector_16' /home/circuits/123D-Circuits-arduino-compiler/build/sketch.ino:17: first defined here error: ld returned 1 exit status

volatile uint16_t tot_overflow; void timer0_init() { TCCR0B |= (1<<CS02); TCNT0 =0; TIMSK0 |= (1 << TOIE0); sei(); tot_overflow =0; } void setup() { timer0_init(); DDRD |= (1<= 245) { if(TCNT0>=25) { PORTD ^= (1<<2); TCNT0=0; tot_overflow =0; } }
}


but when I do small modification in the code, it gets executed modifications were instead of void loop - I defined int main() and I called setup function from main function?

Can you tell me what would be the problem?

avatar

Ganesh - 2016-04-27 11:36:31

reply

When I ran this code, I getting an error ? as

In function __vector_16′: 49: multiple definition of __vector_16′ /home/circuits/123D-Circuits-arduino-compiler/build/sketch.ino:17: first defined here error: ld returned 1 exit status

volatile uint16_t tot_overflow; void timer0_init() { TCCR0B |= (1<<CS02); TCNT0 =0; TIMSK0 |= (1 << TOIE0); sei(); tot_overflow =0; } void setup() { timer0_init(); DDRD |= (1<= 245) { if(TCNT0>=25) { PORTD ^= (1<<2); TCNT0=0; tot_overflow =0; } }
}

but when I do small modification in the code, it gets executed modifications were instead of void loop – I defined int main() and I called setup function from main function?

Can you tell me what would be the problem?

avatar

Sundar - 2016-07-01 03:08:16

reply

I got an error

TIMSK is undeclared TCCR0 is undeclared

can help me

avatar

Alex - 2019-02-23 22:38:45

reply

Hi

Someone kindly can explain me how have been calculated the values below.

  • PORTA bit 0 blinking on and off 15 times / second
  • PORTA bit 1 blinking on and off every 8.3 seconds

Leave a reply

Thank you! Your comment has been successfully submitted. It will be approved as soon as possible.

More from Efundies