#include class Keyboard{ public: Keyboard(){ timer =0;}; ~Keyboard(){}; inline void init(){ avr = (struct LAYOUT * ) IO_MEMORY_OFFSET; led_switch(ALL); }; inline void led_switch(short value){ avr->ddrB = SET; avr->portB ^= value; //let see if it works //if we do not XOR we have to do a AND to resae and a OR to //write the bits : avr->portB |= value; avr->portB &= value; }; inline void enable_int(){ avr->status_reg |= (1<<7) ; //global interruption enable bit avr->gen_int_mask |= (1<<7) ; //external INT1 enable, software interruption avr->portD |= (1<<3); //enable portD int1 avr->tc_int_mask |= (1<<1); //interrupt generator avr->mcu |= (1<<3) | (1<<2); }; //page 34 stop the timer inline void clock_stop(){ avr->tc0_control_reg &= ( 0<<2 | 0<<1 | 0<<0 ); timer=0; }; //page 34 enable the timer clock as CK/1024 inline void clock_start(){ //we neet to set the overflow time avr->tc0_control_reg |= (1<<2) | (1<<0) ; avr->tc0_control_reg &= ~(1<<1); }; inline void key_event() { //BUTTON3 Pressed //mcu bit 3 and 2 //1 0 falling //1 1 rising // avr->portB ^= 1;//debug if (avr->mcu & (1<<2)) { avr->mcu &= ~(1<<2); clock_stop(); // avr->portB ^= 2;//debug avr->portB |= 8; } else { avr->mcu |= (1<<2); clock_start(); // avr->portB ^= 4; //debug } } // private: LAYOUT *avr; unsigned int timer; }; /******************************************/ Keyboard k; //wrapper void key_int(){ k.key_event(); }; void timer_int(){ k.timer++; if(k.timer == 54){ k.timer = 0; k.clock_stop(); k.avr->portB ^=8; } k.avr->portB ^= 16; }; // interruption handlers extern "C"{ void __vector_2 (void) __attribute__((signal)); void __vector_2 (void) { key_int(); }; void __vector_7 (void) __attribute__((signal)); void __vector_7 (void) { timer_int(); }; }; /******************************************/ /* MAIN */ int main(){ k.init(); k.led_switch(NONE); k.clock_stop(); k.enable_int(); while(true); };