2
\$\begingroup\$

I am trying to receive data using UART. I am using PIC32MX795F512L Micro Controller and using 32 bit peripheral library, so I using functions like putsUART or ReadUART1 Now in the code :

int main()
{
 OpenUART1( UART_EN | UART_NO_PAR_8BIT | UART_1STOPBIT  , UART_RX_ENABLE | UART_TX_ENABLE, (FPB/16/BAUDRATE)-1 );
 TRISDbits.TRISD5 = 0;
  while(1)
  {
    char RX_data;
    RX_data = ReadUART1();
    if(RX_data == '1')
    {
      LATDbits.LATD5 = 1;
      putsUART1("LED ON");
     }

     elseif(RX_data == 'q')
     {
       LATDbits.LATD5 = 0;
       putsUART1("LED OFF");
      }
   }
}

Now whenever I type 1 in the terminal led turns on but when I type q then led doesnt turns off. It works with digits but not with letters. Like if I do

elseif(Rx_data == '2')

Then it will work but not with

elseif(RX_data == 'q')

I don't know where I am missing the point. Please help.

\$\endgroup\$
  • \$\begingroup\$ Are you typing 'q' after typing 'l'? Or are you running the firmware again when typing 'q'? \$\endgroup\$ – m.Alin Nov 18 '15 at 10:25
  • \$\begingroup\$ yes I am typing q after 1 \$\endgroup\$ – user46573544 Nov 18 '15 at 10:30
  • \$\begingroup\$ Maybe your terminal software sends a CR or LF character or both after the '1' character and that's why you loose the 'q' reading. Try and see if your terminal software has any option to automatically append CR/LF characters ta a transmission. \$\endgroup\$ – m.Alin Nov 18 '15 at 10:36
  • \$\begingroup\$ I am using docklight. Its not sending any CR LF \$\endgroup\$ – user46573544 Nov 18 '15 at 10:37
  • \$\begingroup\$ The code you just posted in your edit can't be compiling. 'rx' doesn't exist any more and there's no space between else and if. If this is directly from your code, which it should be, then you're just flashing your old binary over and over again \$\endgroup\$ – Asmyldof Nov 18 '15 at 10:47
4
\$\begingroup\$

Assuming you only want it to do one or the other, you need to modify your code as follows:

char RX_data = ReadUART1();
if(RX_data == '1')
{
    LATDbits.LATD5 = 1;
    putsUART1("LED ON");
}
else if(RX_data == 'q')
{
    LATDbits.LATD5 = 0;
    putsUART1("LED OFF");
}

If you logically step through your original code, you will see that when you enter 'q', it gets read and compared to '1', which it doesn't match, but then you perform another read, so you lose the 'q' that you'd already read.

\$\endgroup\$
  • \$\begingroup\$ I don't know why its not working, I have made it char acc to your answer. Everything seems perfect but still not getting the output \$\endgroup\$ – user46573544 Nov 18 '15 at 10:32
  • \$\begingroup\$ @user46573544 I just copied your code, which you say works ok for '1'. In fact, comapring an int with a char is valid, as long as the values do not exceed the range of char. I have amended my answer to be more specific but if this code doesn't work, you need to show a bit more of the context. I'm assuming that this is whithin a while loop. Make sure you only read once, then test all possibilities. \$\endgroup\$ – Roger Rowland Nov 18 '15 at 10:34
  • \$\begingroup\$ I didnt work. This is the only code I have. I am reading it once and then checking all the if else. There might be some other mistake. \$\endgroup\$ – user46573544 Nov 18 '15 at 10:36
  • 2
    \$\begingroup\$ @user46573544 So this piece of code is not inside a while (1) { ... } loop? Please amend your question and post the whole of your main function. \$\endgroup\$ – Roger Rowland Nov 18 '15 at 10:37
  • \$\begingroup\$ yes its inside while(1). I have edited my code \$\endgroup\$ – user46573544 Nov 18 '15 at 10:42
0
\$\begingroup\$

If ReadUART() is giving you hard time. You can use other options like getsUART1()

The document says:

Description: This function reads a string of data of specified length and stores it into the buffer location specified.

Include: plib.h

Prototype: unsigned int getsUART1(unsigned int length,char *buffer, unsigned int uart_data_wait);

Code Example: getsUART1(12, myBuffer, 123);

Or you could try putting values into the UART registers.

\$\endgroup\$

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.