Problem with Reading data through serial communication

General discussion on mikroC.
Post Reply
Author
Message
Dash04
Posts: 3
Joined: 25 Sep 2010 20:01

Problem with Reading data through serial communication

#1 Post by Dash04 » 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");
}

}
}

Dash04
Posts: 3
Joined: 25 Sep 2010 20:01

Re: Problem with Reading data through serial communication

#2 Post by Dash04 » 08 Oct 2010 01:34

Is there anyone able to help explain what is wrong with the code?

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: Problem with Reading data through serial communication

#3 Post by Sobrietytest » 10 Oct 2010 11:39

Try this...

Code: Select all


void interrupt(){
char i = 0;

if (PIR1.RCIF) {
while (Usart_Data_Ready()){      //CHANGE THIS LINE
Rx_buff[i] = Usart_Read();
i++;
}
New_Flag = 1;
}
}


Dash04
Posts: 3
Joined: 25 Sep 2010 20:01

Re: Problem with Reading data through serial communication

#4 Post by Dash04 » 14 Oct 2010 17:19

Sobrietytest wrote:Try this...

Code: Select all


void interrupt(){
char i = 0;

if (PIR1.RCIF) {
while (Usart_Data_Ready()){      //CHANGE THIS LINE
Rx_buff[i] = Usart_Read();
i++;
}
New_Flag = 1;
}
}

Hi Sobrietytest
I changed the code but only manage to get the first byte data.
The LCD display i use to check the receive data also doesn't show the second byte.

Post Reply

Return to “mikroC General”