//--------------------------------------------------------------------------------------- // Programm Nr.: 15 // Content : sound output 1 kHz with timer0 interrupt // Author : digital AG Halle // Date : 27.07.2008 // System : ATmega8-16PU - Dai Hoc Hue Development Board //--------------------------------------------------------------------------------------- // programm memory usage = 3,9 % //--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 { TCNT0 = reload; //load timer0 count-register for the next run count++; //counter in the ISR (to extent ISR-time) if (count < 10) //condition time 1 reached PORTB |= (1<=20) //condition time 2 reached: end of counter cycle count=0; //reset counter } //--MAIN PROGRAMM------------------------------------------------------------------------ int main(void) { //--INITIALISATION--------------------------------------------------------------- DDRB = 2; //PORTB.BIT1 output (PIEZO) PORTB = 2; //PIEZO start value = off (high-level (because it is between MC-output and Vcc)) //INTERRUPTS: //Timer0 // target frequency = 1 kHz // because the toggling of the output-pin in the ISR, the frequency is only 1/2 -> we need 2 kHz // target_freq*(255-reload)=cpu_freq/prescaler // // 8000000/8=1000000 Hz (prescaler=8) // 1000000/50= 20000 Hz (ISR-frequency) // count value 1 = 10 -> // count value 2 = 20 -> we need 2 cycles for 1 wave -> factor 1/2 -> 1 kHz // reload = 255-50 = 205 reload=205; TIMSK |= (1< prescaler = 8 //CPU-freq. = 8 MHz / 8 -> 1 MHz timer freq. //--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-------------------------------------------------------------------------