// ECE476 Project Remote unit with RF receiver and ultrasonic transmitter // 4/5/2006 // // connect RF receiver to port D.0 (RX) // connect Ultrasonic transducer to OC2 (PD7) // Includes #include #include //I like these definitions #define begin { #define end } #define sync_char 0b10101010 //170 #define start_char 0b11001010 //202 #define end_char 0b11010100 //212 #define SOUNDSPEED 0.033 // cm per us //the encoding flash char code[16] = {0b10001011,0b10001101,0b10010011,0b10010101,0b10010110, 0b10011001,0b10011010,0b10011100,0b10100011,0b10100101,0b10100110,0b10101001, 0b10101100,0b10110001,0b10110010,0b10110100}; // constants #define TXBUFSIZE 30 // transmit string buffer size #define RXBUFSIZE 15 // receive string buffer size // declare functions void puts_int(void); // initialize transmit to PC void gets_int(void); // initialize receiver from PC // misc variables unsigned int soundTimer; // count down time in ms unsigned char soundOn; //RXC ISR variables unsigned char r_index; //current string index unsigned char r_buffer[RXBUFSIZE]; //input string unsigned char r_ready; //flag for receive done unsigned char r_char; //current character unsigned char r_started; // packet started //TX empth ISR variables unsigned short int t_index; //current string index unsigned char t_buffer[TXBUFSIZE]; //output string unsigned char t_ready; //flag for transmit done unsigned char t_char; //current character //**************************************************************** // Interrupt Service Routines //**************************************************************** //======================================================== // TIMER 0: Compare match ISR //======================================================== interrupt [TIM0_COMP] void timer0_comp(void) begin if (soundTimer > 0) --soundTimer; end //======================================================== // UART character-ready ISR //======================================================== interrupt [USART_RXC] void uart_rec(void) begin // if(UCSRA.4 == 0) // check for frame error // begin //------------------------------------------------------- // check for sync byte if(r_index==0) begin r_buffer[r_index]=UDR; if(r_buffer[0]==sync_char) r_started=1; else r_started=0; if(r_buffer[0]==sync_char && r_started == 1) begin r_index++; // PORTD.7 = 0; end end //------------------------------------------------------- // check for start byte else if (r_index==1) begin r_buffer[r_index]=UDR; if(r_buffer[1]==start_char) begin r_index++; end // else if(!(r_buffer[1]==sync_char)) // begin // wrong stuff // r_index = 0; // r_started = 0; // PORTD.7 = 1; // end end //------------------------------------------------------- // rest of the packet else begin r_buffer[r_index++]=UDR; // if (r_index==4) // begin // rx_length = decode(rx_data[3],rx_data[4]); // end // else if(r_index>=max_rx_length || rx_data[r_index]==212) // begin if(UDR == end_char || r_index > 4) begin // PORTD.7=1; r_ready=1; //signal cmd processor UCSRB.7=0; //stop rec ISR end end // end // check for frame // else // begin // PORTD.7 = 0; // end end //======================================================== // UART xmit-empty ISR //======================================================== interrupt [USART_DRE] void uart_send(void) begin t_char = t_buffer[++t_index]; if (t_char == 0) begin UCSRB.5=0; //kill isr t_ready=1; //transmit done end else UDR = t_char; //send the char end //**************************************************************** // -- non-blocking keyboard check initializes ISR-driven // receive. This routine merely sets up the ISR, which then // does all the work of getting a command. //**************************************************************** void gets_int(void) begin r_ready=0; r_started=0; r_index=0; UCSRB.7=1; end //**************************************************************** // -- nonblocking print: initializes ISR-driven // transmit. This routine merely sets up the ISR, then //send one character, The ISR does all the work. //**************************************************************** void puts_int(void) begin t_ready=0; t_index=0; if (t_buffer[0]>0) begin putchar(t_buffer[0]); UCSRB.5=1; end end //**************************************************************** // MAIN //**************************************************************** void main(void) begin //======================================================== // Initialization //======================================================== // setup USART UCSRB = 0x18 ; // UCSRC = 0xA6; // set even parity // UBRRL = 103 ; // sets baud rate to 9600 UBRRL = 249 ; // sets baud rate to 4000 // UBRRL = 416 ; // sets baud rate to 2400 // UBRRL = 207 ; // sets baud rate to 4800 // set I/O ports DDRD = 0x82; // port D is used as RX, and OC2 output PORTD.7 = 1; soundTimer = 0; soundOn = 0; r_ready = 0; t_ready = 1; // set timer0 OCR0 = 249; // makes a 1 ms interrupt TIMSK = 0b00000010; // TIMSK.1 = 1; // turn on timer 0 compare match ISR TCCR0 = 0b00001011; // prescalar of 64 and clear on match // TCCR2 = 0b00011000; // CTC mode with prescalar of 0 (stopped), should be 8 when on OCR2 = 40; // make 24.4 kHz output on OC2 #asm sei #endasm // start receiving commands gets_int(); // MAIN loop while(1) begin // #asm("sleep"); if(r_ready && soundOn == 0) begin if(r_buffer[2] == code[0] && r_buffer[3] == code[15]) begin // make ultrasound noise r_buffer[2] = 0; r_buffer[3] = 0; soundTimer = 20; // how many ms to make sound soundOn = 1; TCCR2 = 0b00011010; // CTC mode with prescalar of 8 end else begin r_buffer[2] = 0; r_buffer[3] = 0; gets_int(); end end // r_ready\ if(soundOn == 1 && soundTimer == 0) begin // stop sound soundOn = 0; TCCR2 = 0b00000000; // stop timer 2 PORTD.7 = 1; gets_int(); end // else // begin // while(!t_ready); // sprintf(t_buffer,"%d ",soundTimer); // puts_int(); // end // while(!t_ready); // sprintf(t_buffer,"end "); // puts_int(); end // end while end //end main