/*This is the receiver end program*/ #include #include #include #define begin { #define end } // Declare your global variables here unsigned char lastcommand,command,badcommand; unsigned char speed,lastspeed; unsigned char dir, turn; unsigned char count,reload; unsigned int time0,breakcount; unsigned char forward; unsigned char danger,distance; unsigned char headlight,breaklight,forcebreak; //for brakelight control //********************************************************** //timer 0 overflow ISR interrupt [TIM0_OVF] void timer0_overflow(void) begin //reload to force 1 mSec overflow TCNT0=reload; time0++; if (time0==67) { //start ADC conversion ADCSR = 0xc7; time0=0; } end //********************************************************** void initialize(void) { //ADC input DDRA=0x00; //output headlight, brakelight PORTB=0xff; DDRB=0xff; //PORTC=0xff; DDRC=0xff; //output PWM & USART communication jumpers DDRD=0xff; // Timer/Counter 0 initialization time0=0; reload=256-125; TCCR0=0x03 ; TCNT0=reload; OCR0=0x00; //Timer1 setting, used to output PWM signal TCCR1A=0x91; TCCR1B=0x02; //UART setting UCSRA=0x00; UCSRB=0x10; UCSRC=0x36; //2400 baud UBRRH=0x01; UBRRL=0xa0; //initialization of variable lastcommand=0x55; dir=0; turn = 0; lastspeed=1; speed = 1; forward = 0; danger = 0; distance=0; breaklight=0; breakcount=0; forcebreak=0; //choose PINA.0 as analog input ADMUX = 0x20; //enable timer0 TIMSK=1; #asm("sei"); } //********************************************************** void changespeed(void) { //update lastspeed lastspeed=speed; //change car speed according to command if no dangers detected if ((!danger) || (!forward)) { switch (speed) { case 0 : OCR1A=128; //50% break; case 1 : OCR1A=0; //0% break; case 2 : OCR1A=191; //75% break; case 3 : OCR1A=255; //100% break; } } else OCR1A=0; // stop the car } //********************************************************** void main(void) { initialize(); while (1) { //polling receive complete flag, update command if (UCSRA.7==1) { if (UCSRA.4==1) badcommand=UDR; else command=UDR; } //polling ADC complete flag detect obstacle if (ADCSR.4==1) { distance=ADCH; if (distance > 0x20) { danger=1; changespeed(); forcebreak=1; } else if (danger==1) { danger=0; forcebreak=0; changespeed(); } } //see if command is changed if (lastcommand!=command) { lastcommand = command; //decode command speed = (command & 0x03); forward = (command & 0x10); if (command & 0x08) dir=0 ; else dir=1; if (command & 0x04) turn=0; else turn=1; headlight = (command & 0x40); //change speed if (speed!=lastspeed) { if (speed ==0x01) forcebreak=1; else { forcebreak=0; if (speed < lastspeed) { breakcount=7000; breaklight=1; } else breakcount=0; } changespeed(); } } // brakelight control if (breakcount>0) breakcount--; if ((!forcebreak) && (breakcount==0)) breaklight=0; if ( (forcebreak==1) || (breaklight==1)) PORTC.0=0; else PORTC.0=1; //car control PORTD.2=dir; PORTD.3=turn; PORTD.4=forward; //headlight if (headlight) { PORTC.1=0; PORTC.2=0; } else { PORTC.1=1; PORTC.2=1; } } //end while } //end main