#include #include //#include //zu alt #ifndef OCR1A #define OCR1A OCR1 // 2313 support #endif #ifndef WGM12 #define WGM12 CTC1 // 2313 support #endif #ifndef PINC #define KEY_INPUT PIND // 2313 #else #define KEY_INPUT PINC // Mega8 #endif #define LED_DIR DDRB //#define XTAL 16000000L // nominal value #define XTAL 16000000L // after measuring deviation: 1.5s/d #define DEBOUNCE 256L // debounce clock (256Hz = 4msec) #define uchar unsigned char #define uint unsigned int uchar prescaler; uchar volatile second; // count seconds SIGNAL (SIG_OUTPUT_COMPARE1A) { /************************************************************************/ /* Insert Key Debouncing Here */ /************************************************************************/ #if XTAL % DEBOUNCE OCR1A = XTAL / DEBOUNCE - 1; // compare DEBOUNCE - 1 times #endif if( --prescaler == 0 ){ prescaler = (uchar)DEBOUNCE; second++; // exact one second over #if XTAL % DEBOUNCE // handle remainder OCR1A = XTAL / DEBOUNCE + XTAL % DEBOUNCE - 1; // compare once per second #endif } } int main( void ) { LED_DIR = 0xFF; DDRD |= (1 << PD6); while ( KEY_INPUT & 1 ); // start with key 0 pressed TCCR1B = 1<= 30) PORTD |= (1 << PD6); for(;;){ if( second == 60 ) second = 0; LED_DIR = second; // display second (binary) } }