Skip to content

Instantly share code, notes, and snippets.

@mkleemann
Created January 2, 2012 20:52
Show Gist options
  • Save mkleemann/1552062 to your computer and use it in GitHub Desktop.
Save mkleemann/1552062 to your computer and use it in GitHub Desktop.
atmega8 timer0 - use overflow interrupt
// All values/comments are valid with ATmega8 running at Fosc = 4.000MHz
#include <avr/io.h>
#include <avr/interrupt.h>
// TIMER0 with prescaler clkI/O/1024
#define TIMER0_PRESCALER (1 << CS02) | (1 << CS00)
void main()
{
// ... do something ...
// init Timer0
TIMSK |= (1 << TOIE0); // interrupt enable - here overflow
TCCR0 |= TIMER0_PRESCALER; // use defined prescaler value
sei(); // global interrupt enable
// ... do something ...
while (1)
{
// ... do something ...
} /* end of while(1) */
}
// *** Interrupt Service Routine *****************************************
// Timer0 overflow interrupt handler (~65ms 4MHz@1024 precale factor)
ISR(TIMER0_OVF_vect)
{
// handle interrupt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment