// Copyright 2006 Yamane Akira //ICC-AVR application builder : 2005/09/13 13:28:55 // Target : M8 // Crystal: 4.0000Mhz #include #include unsigned char SW_ON; void port_init(void) { PORTB = 0xFF; DDRB = 0xFF; PORTC = 0x7F; //m103 output only DDRC = 0x00; PORTD = 0xFF; DDRD = 0x00; } //TIMER0 initialize - prescale:1024 // desired value: 20mSec // actual value: 19.964mSec (0.2%) void timer0_init(void) { TCCR0 = 0x00; //stop GICR = 0x00; // Int0 Disabled } #pragma interrupt_handler timer0_ovf_isr:10 void timer0_ovf_isr(void) { TCNT0 = 0xB2; //reload counter value SW_ON = 0x00; if ((PIND & 0x04)==0) { SW_ON++; } else { SW_ON = 0xFF; GICR = 0x40; TCCR0 = 0x00; } } #pragma interrupt_handler int0_isr:2 void int0_isr(void) { GICR = 0x00; // INT0 Disabled TCNT0 = 0xD9; // 10ms TCNT0 Start Value TCCR0 = 0x05; SW_ON = 0; } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer0_init(); MCUCR = 0x00; GICR = 0x40; TIMSK = 0x01; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } // void main(void) { unsigned char i; init_devices(); SW_ON=0; PORTB = ~0x00; i = 0x00; while(1) { MCUCR = 0x80; // Sleep Enable, Idle Mode asm("sleep\n"); if (SW_ON ==0x00) { i++; PORTB = ~i; } } }