//Code for Car //executing on MEGA163 //Includes #include //definitions #define begin { #define end } #define adc_frontIR 0b01100000 #define adc_frontT 0b01100001 #define adc_backT 0b01100010 #define adc_xout 0b01100110 #define adc_yout 0b01100100 #define adc_leftIR 0b01100101 #define frontIR 0 #define frontT 1 #define backT 2 #define xout 3 #define yout 4 #define leftIR 5 #define sideIR 6 #define tx_length 20 //declarations bit led,led2; char status, tx_byte; // 0 - A/d of Xout // 1 - A/d of Yout // 2 - A/d of FrontIR // 3 - A/d of Xout // 4 - A/d of Yout // 5 - A/d of FrontTemp // 6 - A/d of Xout // 7 - A/d of Yout // 8 - A/d of BackTemp // 9 - A/d of Xout // 10- A/d of Yout // 11- A/d of Left IR // 12- jump to Digital IR inputs // 13- acquisition done, encode // 14- transmitting // 15- transmit done, waiting char data[7]; // data[0] - A/d of FrontIR // data[1] - A/d of FrontTemp // data[2] - A/d of BackTemp // data[3] - A/d of Xout // data[4] - A/d of Yout // data[5] - A/d of Left IR // data[6] - Right IR, Back IR's char tx_data[tx_length]; unsigned int time; //the encoding... flash char code[16] = {0b10001011,0b10001101,0b10010011,0b10010101,0b10010110, 0b10011001,0b10011010,0b10011100,0b10100011,0b10100101,0b10100110,0b10101001, 0b10101100,0b10110001,0b10110010,0b10110100}; //********************************************************** //timer 0 compare ISR interrupt [TIM0_COMP] void timer0_compare(void) begin if(time<100) time++; end //********************************************************** void encode(void) begin tx_data[0] = 0xaa; //calibration tx_data[1] = 0xaa; //calibration tx_data[2] = 0xaa; //synchronization tx_data[3] = 0xaa; //synchronization tx_data[4] = 0xaa; //synch tx_data[5] = code[data[0]>>4]; //A/d Front IR High Nibble tx_data[6] = code[(data[0]<<4)>>4]; //A/d Front IR Low Nibble tx_data[7] = code[data[1]>>4]; //A/d Front T High Nibble tx_data[8] = code[(data[1]<<4)>>4]; //A/d Front T Low Nibble tx_data[9] = code[data[2]>>4]; //A/d Back T High Nibble tx_data[10]= code[(data[2]<<4)>>4]; //A/d Back T Low Nibble tx_data[11]= code[data[3]>>4]; //A/d Xout IR High Nibble tx_data[12]= code[(data[3]<<4)>>4]; //A/d Xout IR Low Nibble tx_data[13]= code[data[4]>>4]; //A/d Yout IR High Nibble tx_data[14]= code[(data[4]<<4)>>4]; //A/d Yout IR Low Nibble tx_data[15]= code[data[5]>>4]; //A/d Left IR High Nibble tx_data[16]= code[(data[5]<<4)>>4]; //A/d Left IR Low Nibble tx_data[17]= code[(data[6]<<4)>>4]; //Side IR, Back IR tx_data[18] = 0xaa; tx_data[19] = 0xaa; end //initialization void init() begin //INITIALIZE the PORTS //Used for A/D conversion DDRA = 0x00; //Used for digital IR sensor input DDRB = 0xf0; PORTB.0 = 1; PORTB.6 = 1; PORTB.7 = 1; //Used for LED array, output DDRC = 0xff; //PORTD used for TX. PORTC=0xff; //d7 led, d1 TX, d0,d2..6 unused DDRD = 0xff; PORTD.7=0; //turn d7 led on as power led //set up timer 0 TIMSK=2; //turn on timer 0 cmp match ISR OCR0 = 250; //set the compare re to 250 time ticks //prescalar to 64 and turn on clear-on-match TCCR0=0b00001011; //A/D Converter //init the A to D converter //channel zero/ left adj /INTERNAL Aref ADMUX = adc_xout; //enable ADC and set prescaler to 1/128*16MHz=125,000 //and clear interrupt enable //and start a conversion ADCSR = 0b11000111; //ADCSR.6 //setup the transmission... UCSRB = 0b00001000; //Bit3-Transmit Enable UBRRL = 249; //4000 baud //initialize variables time=0; status=0; tx_byte=0; led=0; led2=0; PORTD.7=1; PORTC = 0xff; end void main() begin init(); #asm ("sei"); while(1) begin if(ADCSR.4) begin switch(status) begin case 0: data[xout]=(ADCH >> 2); ADMUX=adc_yout; ADCSR.6=1; status++; break; case 1: data[yout]=(ADCH >> 2); ADMUX=adc_frontIR; ADCSR.6=1; status++; break; case 2: data[frontIR]=ADCH; ADMUX=adc_xout; PORTB.6 = 1; ADCSR.6=1; status++; break; case 3: data[xout]+=(ADCH >> 2); ADMUX=adc_yout; ADCSR.6=1; status++; break; case 4: data[yout]+=(ADCH >> 2); ADMUX=adc_frontT; ADCSR.6=1; status++; break; case 5: data[frontT]=ADCH; ADMUX=adc_xout; ADCSR.6=1; status++; break; case 6: data[xout]+=(ADCH >> 2); ADMUX=adc_yout; ADCSR.6=1; status++; break; case 7: data[yout]+=(ADCH >> 2); ADMUX=adc_backT; ADCSR.6=1; status++; break; case 8: data[backT]=ADCH; ADMUX=adc_xout; ADCSR.6=1; status++; break; case 9: data[xout]+=(ADCH>>2); ADMUX=adc_yout; ADCSR.6=1; status++; break; case 10: data[yout]+=(ADCH>>2); ADMUX=adc_leftIR; ADCSR.6=1; status++; break; case 11: data[leftIR]=ADCH; PORTB.7 = 1; ADMUX=adc_xout; ADCSR.6=1; status++; break; default: break; end//switch end//if if(status==12) begin data[sideIR] = PINB; status++; end if(status==13) begin encode(); status++; tx_byte=0; end if(status==14) begin //start transmitting if(UCSRA.5 & tx_byte