/* Receiver.c Program to Receive a Character using the Transceiver Module */ // Included files #include #include #include #include "oi.h" #define USB 1 #define CR8 2 // Constants #define TRUE 1 #define FALSE 0 void delay10ms(uint16_t delay_10ms); uint8_t byteRx(void); void byteTx(uint8_t value); void writeChar(char c, uint8_t com); uint8_t getSerialDestination(void); void delay(void); void setSerial(uint8_t com); void main(void) { uint16_t i=0; uint8_t rx_data; uint8_t buffer[1000]; uint8_t start = 1; uint8_t byte_count = 0; uint16_t array_count = 0; uint16_t j=0; uint8_t Start_Flag = FALSE,Stop_Flag = TRUE; uint8_t temp =0x00; /*SET THE SERIAL PORT PARAMETERS TO COMMUNIATE WITH THE PC*/ UBRR0 = 19; UCSR0B = 0x18; UCSR0C = 0x06; DDRB = 0X10; PORTB = 0X10; /*Connect the Receive Pin of the Transceiver to PORTC PIN0 */ /*PC0 must be configured in input mode*/ DDRC = 0XFF; PORTC =0XFF; DDRC = 0XFE; while(1) { for(i=0;i<=20;i++) { buffer[i] = 0x01; //read_string[i] = 0X00; } i=0; /*Do nothing till a start bit is detected*/ temp =0X00; do { } while((PINC & 0X01)== 0x01); /*Start storing all the bits once a start bit has been detected*/ while(i<= 50) { _delay_us(417); buffer[i] = PINC & 0X01; _delay_us(417); //writeChar((rx_data+48),USB); i = i + 1; } writeChar(13,USB); /*Initialise start flag to false and stop flag to true*/ Start_Flag = FALSE; Stop_Flag = TRUE; byte_count = 0; array_count = 0; i=0; do { if( (Start_Flag == TRUE)&& (Stop_Flag == FALSE)) { /*Once a stop bit is detcted, send the bult character to PC*/ if(byte_count == 8) { if(buffer[i] == 0x01) { writeChar(temp,USB); Stop_Flag = TRUE; Start_Flag = FALSE; } } /*Store 8 bits after a start bit into a temp varible and combine them to form the character*/ if(byte_count <= 7) { if(buffer[i] == 0x00) { temp = (temp >> 1) | 0x00; } if(buffer[i] == 0x01) { temp = (temp >> 1) | 0x80; } byte_count = byte_count + 1; } } if((Start_Flag == FALSE)&& (Stop_Flag == TRUE) ) { /*Once a start bit has been detected in the buffer*/ if(buffer[i] == 0x00) { Start_Flag = TRUE; Stop_Flag = FALSE; array_count = array_count + 1; byte_count=0; } if(buffer[i] == 0x01) { } } i = i + 1 ; }while(i<=50); } } void initialize(void) { // Turn off interrupts cli(); // Configure the I/O pins DDRB = 0x10; PORTB = 0xCF; DDRC = 0x02; PORTC = 0xFF; DDRD = 0xE6; PORTD = 0x7D; // Set up the serial port for 57600 baud UBRR0 = Ubrr57600; UCSR0B = (_BV(TXEN0) | _BV(RXEN0)); UCSR0C = (_BV(UCSZ00) | _BV(UCSZ01)); } void powerOnRobot(void) { // If Create's power is off, turn it on if(!RobotIsOn) { while(!RobotIsOn) { RobotPwrToggleLow; delay10ms(50); // Delay in this state RobotPwrToggleHigh; // Low to high transition to toggle power delay10ms(10); // Delay in this state RobotPwrToggleLow; } delay10ms(350); // Delay for startup } } void baud28k(void) { // Send the baud change command for 28800 baud writeChar(CmdBaud, CR8); writeChar(Baud19200, CR8); // Wait while until the command is sent while(!(UCSR0A & _BV(TXC0))) ; // Change the atmel's baud rate UBRR0 = Ubrr19200; // Wait 100 ms delay10ms(10); } void delay10ms(uint16_t delay_10ms) { // Delay for (delay_10ms * 10) ms while(delay_10ms-- > 0) { // Call a 10 ms delay loop _delay_loop_2(46080); } } uint8_t byteRx(void) { // Receive a byte over the serial port (UART) while(!(UCSR0A & _BV(RXC0))) ; return UDR0; } void flushRx(void) { uint8_t temp; // Clear the serial port while(UCSR0A & _BV(RXC0)) temp = UDR0; } void byteTx(uint8_t value) { // Send a byte over the serial port while(!(UCSR0A & _BV(UDRE0))) ; UDR0 = value; } uint8_t getSerialDestination(void) { if (PORTB & 0x10) return USB; else return CR8; } void delay(void) { int i=0,j=0; for(i=1;i<=1000;i++) { for(j=1;j<=1000;j++) { } } } void writeChar(char c, uint8_t com) { uint8_t originalDestination = getSerialDestination(); if (com != originalDestination) { setSerial(com); delay(); } byteTx((uint8_t)(c)); if (com != originalDestination) { setSerial(originalDestination); delay(); } } void setSerial(uint8_t com) { if(com == USB) PORTB |= 0x10; else if(com == CR8) PORTB &= ~0x10; } void SendStringtoPC(char *Message) { setSerial(USB); while(*Message) { while(!(UCSR0A & _BV(UDRE0))) ; UDR0 = *Message; Message++; } }