#include #include #include #include #include #include #define t0 500 //Define task1 timer0 interval of execution #define t1 30 // for debounce #define pin 0x55 #define headerPacket 50 #define but0 0x22 #define but1 0x33 #define but2 0x44 int time0, time1; int leds; char button0Flag, button1Flag, button2Flag; char state0, state1, state2; int counter; void debounce0(void); void debounce1(void); void debounce2(void); //************************************************************************ interrupt [TIM0_COMP] void timer0_compare(void) { if (time0>0)--time0; if (time1>0)--time1; } //************************************************************************ // thi task will simlply flash the leds void task0 (){ time0=t0; leds = leds ^ 0xff; PORTD= leds; } // the init routine void init (){ //set up the ports DDRD=0xff; // PORT D is output leds = 0x00; PORTD= leds; DDRB=0x00; // PORT B is an input PORTB = 0xff; // set the pull ups //set up timer 0 TIMSK = 2; //turn on timer 0 cmp match ISR OCR0 = 249; //set the compare re to 250 time ticks TCCR0=0b00001011; //prescalar to 64 and turn on clear-on-match //Setup rs232 transmisson UCSRB = 0x08; //transmit only on PORTD.1 UBRRL = 207; //setup on 4800 baud // start isrs #asm sei #endasm // general initialization state0=1; state1=1; state2=1; button0Flag=0; button1Flag=0; button2Flag =0; time0 = t0; time1 = t1; } //************************************************************************ void main(void) { init(); while (1) { //putchar (0xaa); debug purposes // toggle leds if (time0==0) { task0(); } // debounces the buttons if (time1==0) { time1 = t1; debounce0(); debounce1(); debounce2(); } // if each any of the buttons are pressed. // send the dc balanced initial packets of 0xaa // then send the header packet key // then send the command (ie, button number key) // then send the end packet if (button0Flag) { button0Flag =0; for (counter =0; counter < headerPacket; counter++) { putchar(0xaa); } putchar(pin); putchar(but0); putchar(pin); leds = leds ^ 0xff; PORTD= leds; } if (button1Flag) { button1Flag =0; for (counter =0; counter < headerPacket; counter++) { putchar(0xaa); } putchar(pin); putchar(but1); putchar(pin); leds = leds ^ 0xff; PORTD= leds; } if (button2Flag) { button2Flag =0; for (counter =0; counter < headerPacket; counter++) { putchar(0xaa); } putchar(pin); putchar(but2); putchar(pin); leds = leds ^ 0xff; PORTD= leds; } } } void debounce0 (void) { switch (state0) { case 1: if (PINB.0 ==0) state0 =2; break; case 2: if (PINB.0 ==0) { state0 =3; button0Flag =1; } else state0 =1; break; case 3: if (PINB.0 ==0) state0 =3; else state0=4; break; case 4: if (PINB.0 == 0) state0=3; else { button0Flag =0; state0=1; } break; } } void debounce1 (void) { switch (state1) { case 1: if (PINB.1 ==0) state1 =2; break; case 2: if (PINB.1 ==0) { state1 =3; button1Flag =1; } else state1 =1; break; case 3: if (PINB.1 ==0) state1 =3; else state1=4; break; case 4: if (PINB.1 == 0) state1=3; else { button1Flag =0; state1=1; } break; } } void debounce2 (void) { switch (state2) { case 1: if (PINB.2 ==0) state2 =2; break; case 2: if (PINB.2 ==0) { state2 =3; button2Flag =1; } else state2 =1; break; case 3: if (PINB.2 ==0) state2 =3; else state2=4; break; case 4: if (PINB.2 == 0) state2=3; else { button2Flag =0; state2=1; } break; } }