// // Cornell 2005 // Edward Lo and Sihan Goi // ECE476 final project // Temperature sensor and fan wireless transmitter // #include #include #include //I like these definitions #define begin { #define end } //task scheduling timers #define t1 30 //30 ms debounce state machine //wireless packet definitions (maintain DC balance) #define start_packet 0b10100110 #define stop_packet 0b11010010 enum { NoPush, MaybePush, Pushed, MaybeNoPush }; unsigned int time1; //task scheduling timeout counter unsigned char buttons, buttons2, inbutton; //saves the button press unsigned char butstate; //debounce state machine unsigned char cdata; //data byte to transmit void initialize(void); //set everything up void getInput(void); //debounce buttons //********************************************************** //Timer0 compare ISR // Simply ticks down the task timers. interrupt [TIM0_COMP] void timer0_compare(void) { if (time1>0) --time1; } //********************************************************** //Transmit the data byte // Encapsulates the data packet with a start and end packet. // Send 0xaa's to setup receiver gain. void senddata(void) { //setup the receiver for proper gain and sync putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xaa); putchar(0xff); putchar(0x00); //send data putchar(start_packet); putchar(cdata); putchar(stop_packet); cdata = 0; } //********************************************************** //Button debouncing state machine // Gets the button presses into "inbutton" and then encodes it // into the "cdata" variable. This becomes the data packet. void getInput(void) { time1=t1; //button debounce switch( butstate ) begin case NoPush: buttons = PINA; if( ~buttons != 0 ) butstate = MaybePush; break; case MaybePush: buttons2 = PINA; if( buttons2 == buttons ) begin butstate = Pushed; inbutton = ~buttons; end else butstate = NoPush; break; case Pushed: buttons2 = PINA; if( buttons2 == buttons ) butstate = Pushed; else butstate = MaybeNoPush; break; case MaybeNoPush: buttons2 = PINA; if( buttons2 == buttons ) butstate = Pushed; else begin butstate = NoPush; inbutton = 0; end break; end //parse input switch( inbutton ) { case 0b10000000: case 0b01000000: case 0b00100000: case 0b00010000: cdata = 0b10000000 | (inbutton >> 1) | 0b10; inbutton = 0; break; case 0b00000001: cdata = 0b10000110; inbutton = 0; break; } } //********************************************************** //Main program // Debounce buttons and send presses to serial port. void main(void) { initialize(); while(1) { if( time1 == 0 ) getInput(); //get debounced input if( cdata != 0 ) senddata(); //transmits button press } } //********************************************************** //Get everyting started void initialize(void) { //setup ports DDRA = 0x00; // buttons PORTA = 0xff; // pull-up resistors DDRD = 0xff; // output for status light and transmitter PORTD = 0xff; // power status on //serial setop for debugging using printf, etc. UCSRB = 0x18; UBRRL = 207; //4800 //setup timer0 for 1ms TIMSK = 2; OCR0 = 249; TCCR0 = 0b00001011; //initialize timers time1 = t1; time2 = t2; //setup debounce states butstate = NoPush; inbutton = 0; cdata = 0; #asm ("sei"); //turns on LED PORTD.7 = 0; }