Skip to content

Instantly share code, notes, and snippets.

@chayanforyou
Forked from mkleemann/atmega8_timer0.c
Created October 1, 2022 02:53
Show Gist options
  • Save chayanforyou/a9b1f657203f910b7fde285449629f63 to your computer and use it in GitHub Desktop.
Save chayanforyou/a9b1f657203f910b7fde285449629f63 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