//--------------------------------------------------------------------------------------- // Programm Nr.: 16 // Content : dimming a LED with timer2 fast pulse width modulation mode (PWM) // Author : digital AG Halle // Date : 27.07.2008 // System : ATmega8-16PU - Dai Hoc Hue Development Board //--------------------------------------------------------------------------------------- //The hardware-implemented fast PWM does NOT use calculation time of the MC. It works //indipendent! //--included Files----------------------------------------------------------------------- #include #include #include #include //--declarations------------------------------------------------------------------------- #ifndef F_CPU #define F_CPU 8000000 //CPU frequency = 8 MHz #endif uint8_t KEYS; uint8_t KEYS_OLD; //--INTERRUPT SERVICE ROUTINES (ISR)----------------------------------------------------- SIGNAL( TIMER2_COMP_vect ) { } //The ISR is empty but has to be written in the program. If it wasn't - the MC would jump //to the RESET routine at every timer2 interrupt. //--MAIN PROGRAMM------------------------------------------------------------------------ int main(void) { //--INITIALISATION--------------------------------------------------------------- //INTERRUPTS: TIMSK = (1< 127 are 50% TCCR2 = (1< prescaler = 64 //8 MHz / 64 -> PWM frequency = 125 kHz KEYS_OLD=PINC; //--MAIN LOOP-------------------------------------------------------------------- while(1) { KEYS=PINC; if (KEYS_OLD!=KEYS) { //mask = 1110 0010 = 226 dec. if ((KEYS&226)==!(0x02)) //if key 1 was pressed { OCR2=OCR2+5; //increase OCR2 -> brigther PORTD=OCR2; //binary state output } //mask = 1110 0100 = 228 dec. if ((KEYS&228)==!(0x04)) //if key 2 was pressed { OCR2=OCR2-5; //decrease OCR2 -> darker PORTD=OCR2; //binary state output } } KEYS_OLD=KEYS; } return 0; } //--MAIN PROGRAMM END-------------------------------------------------------------------- //--EXTERNAL ROUTINES-------------------------------------------------------------------- //--PROGRAMM END-------------------------------------------------------------------------