#include #include #include #define begin { #define end } //debounce state #define NoPush 1 #define MaybePush 2 #define Pushed 3 #define MaybeNoPush 4 //direction #define forward 1 #define reverse 0 //steering // Declare your global variables here unsigned char command,speedin,speedmag,direction,steering,noturn,switchin; unsigned char light,left,right; unsigned char PushState1,PushFlag1,PushState0,PushFlag0,PushStateL,PushStateR; interrupt [USART_TXC] void uart_tran(void) { UDR=command; } void initialize(void) { DDRA=0x00; //output LED PORTB=0xff; DDRB=0xff; //switch input DDRC=0x00; //USART communication jumpers //PORTD=0xff; DDRD=0xff; PushState0 = NoPush; //light button state PushStateL = NoPush; //left button state PushStateR = NoPush; //right button state light=0; //USART setting UCSRA = 0x00; UCSRB = 0x08; UCSRC = 0x36; //2400 baud UBRRH = 0x01; UBRRL = 0xa0; SFIOR=0x00; ADMUX=0x20; //allows the receiver to adjust the gain command=0x55; putchar(command); putchar(command); putchar(command); putchar(command); putchar(command); putchar(command); putchar(command); putchar(command); #asm ("sei"); } //debounce left button void debounce_left () { switch (PushStateL) begin case NoPush: if (~PINC.3 == 1) PushStateL=MaybePush; else PushStateL=NoPush; break; case MaybePush: if (~PINC.3 == 1) begin PushStateL=Pushed; left=1; end else PushStateL=NoPush; break; case Pushed: if (~PINC.3 == 1) PushStateL=Pushed; else PushStateL=MaybeNoPush; break; case MaybeNoPush: if (~PINC.3 == 1) PushStateL=Pushed; else begin PushStateL=NoPush; left=0; end break; end } //debounce right button void debounce_right() { switch (PushStateR) begin case NoPush: if (~PINC.2 == 1) PushStateR=MaybePush; else PushStateR=NoPush; break; case MaybePush: if (~PINC.2 == 1) begin PushStateR=Pushed; right=1; end else PushStateR=NoPush; break; case Pushed: if (~PINC.2 == 1) PushStateR=Pushed; else PushStateR=MaybeNoPush; break; case MaybeNoPush: if (~PINC.2 == 1) PushStateR=Pushed; else begin PushStateR=NoPush; right=0; end break; end } //debounce light button void debounce_light (void) { switch (PushState0) begin case NoPush: if (~PINC.1 == 1) PushState0=MaybePush; else PushState0=NoPush; break; case MaybePush: if (~PINC.1 == 1) begin PushState0=Pushed; PushFlag0=1; end else PushState0=NoPush; break; case Pushed: if (~PINC.1 == 1) PushState0=Pushed; else PushState0=MaybeNoPush; break; case MaybeNoPush: if (~PINC.1 == 1) PushState0=Pushed; else begin PushState0=NoPush; //turns the light off if it is on, turns the light on if it is off if (PushFlag0) light=~light; PushFlag0=0; end break; end } void main(void) { initialize(); while (1) { UCSRB=0x48; //enable transmit complete interrupt ADMUX=0x20; //left adjusted and ADC0 ADCSR = 0xc7; //ADC enable and start conversion debounce_left (); debounce_right (); //encode steering if (left==right) command=command | 0x04; //noturn if both button pressed else { command = command & 0xfb; //turn if (left) command = command | 0x08 ; //direction is left else command = command & 0xf7; //direction is right } debounce_light (); //encode light if (light) command=command | 0x40; else command = command & 0xbf; while (ADCSR.4==0) { //wait if A/D conversion had not finished } speedin = ADCH; //update speed input from ADC //encode speed if (speedin > 0xa3) { //direction=forward; command = (command | 0x10); //encode speed magnitude if (speedin < 0xcd) speedmag=0; else if (speedin < 0xef) speedmag=2; else speedmag=3; PORTB.0=1; } else if (speedin < 0x67 ) { //direction=reverse; command = (command & 0xef); //encode speed magnitude if (speedin > 0x3f) speedmag=0; else if (speedin > 0x0d) speedmag=2; else speedmag=3; PORTB.0=1; } else { speedmag=1; PORTB.0=0; //turn on the light at the controller } command = (command & 0xfc) | speedmag ; } }