Problem with Reading data through serial communication
Posted: 06 Oct 2010 15:08
Hi,
I'm having problem reading data received through rs232 using serial communication . I am able to read the first byte sent to the PIC which will be use control the forward/backward of a vehicle. but the second byte which will be be use to control the Left/right of the vehicle not working.
I'm hoping someone could look at the code and enlighten me on what is wrong with it.
Here's the code:
unsigned short i = 0;
char Rx_buff[4];
char New_Flag = 0;
void interrupt(){
char i = 0;
if (PIR1.RCIF) {
if (Usart_Data_Ready()){
Rx_buff = Usart_Read();
i++;
}
New_Flag = 1;
}
}
void main(){
TRISB = 0;
PORTB = 0;
TRISD = 0;
PORTD = 0;
Lcd_Init(&PORTB);
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1, "RS232 READY");
Delay_ms(300);
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE=1; //enable receive interrupt
Usart_init(9600);
while(1){
if(New_Flag){
New_Flag = 0;
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1, "RS232 Com Status");
Lcd_Out(2,1, &Rx_buff[0]);
Lcd_Out(2,3, &Rx_buff[1]);// TESTING
}
if ( Rx_buff[0] == 0) {
PORTD = 0x00;
Lcd_Out(2,8, "STOP");
}
else if ( Rx_buff[0] < 60 ) {
PORTD = 0x3A;
Lcd_Out(2,8, "FORWARD");
}
else if ( Rx_buff[0] > 80 ) {
PORTD = 0x35;
Lcd_Out(2,8, "BACK");
}
else if ( Rx_buff[1] == 0 ) {
PORTD = 0x00;
Lcd_Out(2,8, "STOP");
}
else if ( Rx_buff[1] < 130 ) {
PORTD = 0x39;
Lcd_Out(2,8, "LEFT");
}
else if ( Rx_buff[1] > 190 ) {
PORTD = 0x36;
Lcd_Out(2,8, "RIGHT");
}
else {
PORTD = 0x00;
Lcd_Out(2,8, "STOP");
}
}
}
I'm having problem reading data received through rs232 using serial communication . I am able to read the first byte sent to the PIC which will be use control the forward/backward of a vehicle. but the second byte which will be be use to control the Left/right of the vehicle not working.
I'm hoping someone could look at the code and enlighten me on what is wrong with it.
Here's the code:
unsigned short i = 0;
char Rx_buff[4];
char New_Flag = 0;
void interrupt(){
char i = 0;
if (PIR1.RCIF) {
if (Usart_Data_Ready()){
Rx_buff = Usart_Read();
i++;
}
New_Flag = 1;
}
}
void main(){
TRISB = 0;
PORTB = 0;
TRISD = 0;
PORTD = 0;
Lcd_Init(&PORTB);
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1, "RS232 READY");
Delay_ms(300);
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE=1; //enable receive interrupt
Usart_init(9600);
while(1){
if(New_Flag){
New_Flag = 0;
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1, "RS232 Com Status");
Lcd_Out(2,1, &Rx_buff[0]);
Lcd_Out(2,3, &Rx_buff[1]);// TESTING
}
if ( Rx_buff[0] == 0) {
PORTD = 0x00;
Lcd_Out(2,8, "STOP");
}
else if ( Rx_buff[0] < 60 ) {
PORTD = 0x3A;
Lcd_Out(2,8, "FORWARD");
}
else if ( Rx_buff[0] > 80 ) {
PORTD = 0x35;
Lcd_Out(2,8, "BACK");
}
else if ( Rx_buff[1] == 0 ) {
PORTD = 0x00;
Lcd_Out(2,8, "STOP");
}
else if ( Rx_buff[1] < 130 ) {
PORTD = 0x39;
Lcd_Out(2,8, "LEFT");
}
else if ( Rx_buff[1] > 190 ) {
PORTD = 0x36;
Lcd_Out(2,8, "RIGHT");
}
else {
PORTD = 0x00;
Lcd_Out(2,8, "STOP");
}
}
}