1
\$\begingroup\$

I have been trying to do a simple operation based on the data byte received by the MCU(pic18F) from the PC terminal through UART. The operation is turning ON an LED depending on the content of the received data byte. So here is the code for the logic:

char data = 0;
unsigned char UARTReadByte()
{
   while(!PIR1bits.RCIF); //Wait until the receive buffer is full 
   return RCREG; //Return received byte 
}

void main()
{
   TRISAbits.TRISA1 = 0
   UARTInit(); //Initialize UART
   LCDInit(); //Initialize LCD
   while(1)
  {
     data = UARTReadByte(); //The received data from the virtual terminal (UART) is stored in another variable char 
     Write_Command(0x80); 
     Write_data(data); //Display the received data on the LCD
     if(data==1)  //check if the data is 1, if 1 turn ON the LED
     PORTAbits.RA0 = 1;
 }
}

The received number is displayed on the LCD, however the LED is not turning ON. I presume the MCU is not able to check and validate the received byte due to issues in the data format. Can anyone help me with solving this problem. Thank you all in advance.

\$\endgroup\$
4
\$\begingroup\$

If you are sending an ASCII character "1", then your if statement should be

if(data == '1')

as you have it, you are comparing the received character to the hex value 0x01 but the ASCII character '1' is 0x31.

\$\endgroup\$
  • \$\begingroup\$ Oh yeah!, missed that one. Thank you for pointing it out. \$\endgroup\$ – PsychedGuy Dec 12 '15 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.