/* //***************************************************************************** // // File Name : 'blink.c' // Title : Interrupt Driven LED Blink // Author : William Riley // Date : 10/13/2005 // Revised : 10/13/2005 // Version : 1.0 // Target MCU : Atmel AVR // Editor Tabs : 4 // //***************************************************************************** Blinks an LED connected to pin PB0 using timer0 overflow interrupt. +5V | | |-| |-| R1 |-| LED | | /| | PB0 -----|< |------* | \| */ #include #include #include #define MAX_INT_COUNT 31250 // with 16MHz clock and /256 prescale gives a 500ms cycle int main(void) { PORTA = 0xFF; PORTB = 0xFF; PORTC = 0xFF; PORTD = 0xFF; DDRB = 0xFF; /* CS02 CS01 CS00 ------------------------- 0 0 1 | No Prescaling 0 1 0 | /8 0 1 1 | /64 1 0 0 | /256 1 0 1 | /1024 */ TCCR0 = ( 1 << CS02 ) | ( 0 << CS01 ) | ( 0 << CS00 ); // set prescale to /256 TIMSK |= _BV( TOIE0 ); // enable Timer/Counter0 interrupt sei(); // enable global interrupts for(;;) { } return 0; } SIGNAL(SIG_OVERFLOW0) { static uint16_t interrupt_count = 0; TCNT0 = 0; interrupt_count++; if(interrupt_count > MAX_INT_COUNT) { PORTB ^= 0x01; // toggle pin PB0 interrupt_count = 0; } }