//--------------------------------------------------------------------------------------- // Programm Nr.: 13 // Content : blinking LED with timer0 interrupt // Author : digital AG Halle // Date : 27.07.2008 // System : ATmega8-16PU - Dai Hoc Hue Development Board //--------------------------------------------------------------------------------------- //*** Look at the usage of program memory of only 3,6 % ! *** //--included Files----------------------------------------------------------------------- #include #include #include #include //--declarations------------------------------------------------------------------------- #ifndef F_CPU #define F_CPU 8000000 //CPU frequency = 8 MHz #endif uint16_t count; uint8_t reload; //--INTERRUPT SERVICE ROUTINES (ISR)----------------------------------------------------- SIGNAL (TIMER0_OVF_vect) //event: timer0 overflow 255->0 { count++; //counter in the ISR (to extent ISR-time) if (count < 61) //condition time 1 reached PORTD |= (1<=122) //condition time 2 reached: end of counter cycle count=0; //reset counter } //--MAIN PROGRAMM------------------------------------------------------------------------ int main(void) { //--INITIALISATION--------------------------------------------------------------- DDRD = 1; // LED0 output PORTD = 1; // LED0 = off //INTERRUPTS: //Timer0 // target frequency = 1 Hz // because the toggling of the output-pin in the ISR, the frequency is only 1/2 -> we need 2 Hz // target_freq*(255-reload)=cpu_freq/prescaler // // with a prescaler of 256, the ISR_freq. is 15,625 kHz // by using the full range of timer0 (0...255) (=256 steps), we get 61 cycles to reach the 2 Hz. // // 8000000/256/256/61 = 2 // reload is not needed - we use the full timer0 range 0->255->0->255.... TIMSK |= (1< prescaler = 256 //--PROGRAMM CODE BEFORE MAIN LOOP (USED JUST ONE TIME)-------------------------- sei(); //enable flag for all interrupts //--MAIN LOOP-------------------------------------------------------------------- while(1) { } return 0; } //--MAIN PROGRAMM END-------------------------------------------------------------------- //--EXTERNAL ROUTINES-------------------------------------------------------------------- //--PROGRAMM END-------------------------------------------------------------------------