Timer 0 interrupt counter
Toolkit:AVR Development System
Location:/bipom/devtools/WinAVR/minimaxavrc/Examples/Labs/Lab06/Lab6_3
#define ONE_SECOND 500 #define HALF_SECOND 250 #define LED_RED 6 //PB6 #define LED_YELLOW 4 //PB4 #define LED_GREEN 5 //PB5 #define LED_RED_ON() (PORTB &=~ 0x40) #define LED_YELLOW_ON() (PORTB &=~ 0x10) #define LED_GREEN_ON() (PORTB &=~ 0x20) #define LED_RED_OFF() (PORTB |= 0x40) #define LED_YELLOW_OFF() (PORTB |= 0x10) #define LED_GREEN_OFF() (PORTB |= 0x20) unsigned int counter = 5 ; unsigned int Ticks = 0 ; unsigned int RedFlag,YellowFlag; // Flag for Red LED state ISR (TIMER0_OVF_vect) { // Interrupt every 2 mS if(Ticks == HALF_SECOND) { if(RedFlag) // checking Red LED state { RedFlag=0; LED_RED_ON(); // Turn on RED LED } else { RedFlag=1; LED_RED_OFF();// Turn off RED LED } counter++; if(counter > 20) // checking counter value { if(YellowFlag) // check Yellow LED state { YellowFlag=0; LED_YELLOW_ON(); // Turn on YELLOW LED } else { YellowFlag=1; LED_YELLOW_OFF();// Turn off YELLOW LED } } Ticks = 0; } Ticks++; } int main(void) { //Initialize port pins /* Define pull-ups and set outputs */ /* Define directions for port pins */ PORTB = (1<<LED_RED)|(1<<LED_YELLOW)|(1<<LED_GREEN); DDRB = (1<<LED_RED)|(1<<LED_YELLOW)|(1<<LED_GREEN); //Initialization Timer0 // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 125,000 kHz // Interrupt every 2mS TCCR0A=0x00; TCCR0B=0x03; TCNT0=0x00; OCR0A=0x00; OCR0B=0x00; //Timer0 Overflow Interrupt Enable TIMSK0 = 0x01; Ticks=0; // Global enable interrupts sei(); while (1) ; }