• AVR Freaks

Hot!PIC24 UART not reading data. Please help

Author
asmith34
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2019/01/17 14:04:17
  • Location: 0
  • Status: offline
2019/04/09 12:25:07 (permalink)
0

PIC24 UART not reading data. Please help

I have an explorer 16/32 board with a PIC24FJ1024GB610 connected to an LS2208 barcode scanner via the RX pin of the RS232 DB9 connector after its gone through a voltage divider to obtain a signal with peak voltage of a little over 3V. Using a Saleae signal analyzer I'm able to see that the signal coming out of the barcode scanner when it scans is a 9600 baud rate, 8 bit, 1 stop bit, no parity, that is inverted as depicted in the attached picture.
 
After extensively looking at the data sheets for the PIC24FJ1024GB610 I was able to emulate a similar signal to the one the barcode scanner puts out in order to test that my UART1 configurations were correct. The issue I'm having now is that no matter what I do I can't seem to get any indication that my barcode signal is actually being read by the microcontroller. I've been trying to test to see if the UART1 even notices when the buffer receives data but it doesn't seem to at all when using U1STAbits.URXDA or U1STAbits.RIDLE. I currently have my RX pin set to RB2 which should correspond to pin 23 on our board an I believe I've set the pin correctly as an RX input. The UART portions of my code are as follows:
 
#define rxPin _RB2
 
void InitU1(void){
U1MODEbits.BRGH = 0;
U1BRG =12; 
U1MODE =0x8010; 
U1STA = 0x5400;
PORTBbits.RB2 = 0;
TRISBbits.TRISB1 = 0; // enable RTS , output
IPC2bits.U1RXIP = 4;
RPINR18bits.U1CTSR = 6; //set CTS pin RP6
RPINR18bits.U1RXR = 13; //set RX pin RP8
RPOR0bits.RP1R = 3; //set TX pin RP1
RPOR1bits.RP3R = 4; //set RTS pin RP3

}
 
char putU1(char c)
{
U1TXREG = c; // Write value to transmit FIFO
return c;
}
 
char getU1 (void)
{
return U1RXREG ; // from receiving buffer
}
 
int main(void) {
LCD_Initialize();
InitU1(); 
int i;
ms_delay(1000);

char rx[15] = "000000000000000";
while(1){
if (U1STAbits.URXDA == 1){
for (i = 0 ; i < 15 ; i++)
{
if ((rx = getU1()) == '\n') break;
}
rx = '\0';
printf("RX input: %s\n", rx); 

}
uint8_t l = U1STAbits.URXDA;
printf("%i\n", l);

}
}

 
The logic of the code is that if a signal is detected on UART1 then the buffer will fill the variable rx with an array of characters as quickly as possible to avoid the 4 character buffer from overfilling and then printing the rx character array to an lcd using printf. While idle the program displays the output of U1STAbits.URXDA which is just a repeated sequence of 0's until a signal gets picked up. My issue is that at no point do I ever get a value other than 0 for U1STAbits.URXDA even after a barcode is scanned and the buffer should have a character available. I'm not sure the explorer board is receiving my barcode signal at all. Any suggestions would be greatly appreciated!
post edited by asmith34 - 2019/04/09 12:34:24

Attached Image(s)

#1

13 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/09 13:25:03 (permalink)
    0
    Is your selected RX pin analog capable?
    If yes, do you switch it to digital mode anywhere?
     

    Nearly there...
    #2
    MBedder
    Circuit breaker
    • Total Posts : 6773
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/09 14:14:11 (permalink)
    0
    You definitely have a magic voltage divider - apart from dividing the voltage it seems to invert the RS232 signal, not even speaking about its bipolar-to-unipolar conversion ability LoLLoLLoL
    #3
    davekw7x
    Entropy++
    • Total Posts : 1791
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/11 11:52:59 (permalink)
    0
    asmith34
    I have an explorer 16/32 board with a PIC24FJ1024GB610...

    I haven't inspected your code (very much) but I think I see your main problem:

    Assuming that your voltage divider is applying the signals shown in your pictures to your UART Rx pin (level-shifted and attenuated so that they go from 0V to 3.3V), you must realize that to get the values needed for the UART receiver, they must be inverted.
     
    Idle is logic 1, then for each byte:
    • Start Bit   : 0
    • Data  Bits  : Actual Data Bit Values, LSB first
    • Stop  Bit   : 1

    Use some kind of inverter between the RS232 output from the scanner and tne UART RX pin.   (I use a homebrew RS232-to-Logic converter built with a FT232 chip to take care of level shifting and inverting).

    Here's a suggestion: Before getting into all of the application code development, make a simple project with a while(1){} loop that reads data from the UART Rx pin and echoes that data back to the UART Tx pin.  Don't do anythng fancy.  Just continuously read a byte and write a byte.

    In main():
     
        // Initialize everything here

        while (1)
        {
            // Remember: We are not trying to implement a robust, practical
            // application.  We just want to get stuff into and out of
            // the UART!
            if (U1STAbits.URXDA) {
                U1TXREG = U1RXREG;
            }
        }


    Use the Explorer 16/32 Serial Micro-B connector with a cable a USB port on your PC.  Use a terminal emulator (putty, minicom or whatever) set at the same bit rate as your scanner RS232 connection uses.

    Explorer 16/32 UART onnections for the "Serial" USB port are
    •     P50_TXB  goes to RF5/RP17 of the PIC24F1024GB610 on the PIM.  This is UART Tx
    •     P49_RXB  goes to RF4/RP10 of the PIC24F1024GB610 on the PIM.  This is UART Tx

    (Tested on my Explorer 16/32 with PIC24FJ1024GB610 PIM.)

    If you can't get this to work, then post your complete code.  Maybe someone can help you get to the bottom of things.

    Regards,

    Dave
    Footnote:

    I note that the comment in the following statement is incorrect.
    RPINR18bits.U1RXR = 13; //set RX pin RP8

    Your pin, RB2 is, indeed, RP13, so the assignment is correct (I think), but incorrect comments are HUGE time-wasters when debugging.  Especially when you are getting help from others.



    post edited by davekw7x - 2019/04/11 12:05:13

    Sometimes I just can't help myself...
    #4
    MBedder
    Circuit breaker
    • Total Posts : 6773
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/11 12:07:08 (permalink)
    5 (1)
    davekw7xUse some kind of inverter between the RS232 output from the scanner and tne UART RX pin
    RTFM makes a real magic pulling the material things from nothing - such as the URXINV bit which allows avoiding using the invertor LoL


    #5
    davekw7x
    Entropy++
    • Total Posts : 1791
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/11 13:01:25 (permalink)
    0
    MBedder
    RTFM

    In fact, I have read the manual.
    I still recommend that UART functionality be verified using Explorer hardware (and no register-implemented inversion).  And I recommend a more robust hardware solution than some kind of voltage divider to do the level shifting.  By using a conventional RS232-to-logic device, the same UART setup used for the test can be used in the final application.
     
    Regards,

    Dave

    Sometimes I just can't help myself...
    #6
    Aussie Susan
    Super Member
    • Total Posts : 3608
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/11 21:08:59 (permalink)
    5 (1)
    I suspect that your logic is wrong. In the main loop you check URXDA which will go high on the first character received. You then enter a 'for loop to read up to 15 characters by calling the 'getU1()' function repeatedly (until you read in the '\r' character). However all 'getU1()' does is to read from the Rx buffer.
    What you need to do is something like (pseudo-code - not real C code):

    char rx[15];
    int i = 0;
    int readingFlag;
    ...
    readingFlag = 1;
    while(readingFlag)
    {
        if( URXDA)  // See if a character is ready
       {
            rx[i] = RXREG;  // Read the character
            if( '\r' == rx[i++])
            { readingFlag = 0; }  // Stop readng when we get a carriage return
            if( 15 <= i)
            { readingFlag = 0; }  // STop reading when the would exceed the buffer
        }
    }
    // process the buffer from index 0 to index i-1

    Susan
    #7
    asmith34
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/01/17 14:04:17
    • Location: 0
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/15 11:21:47 (permalink)
    0
    I'm now using a MAX232 device to interface between my barcode scanner and my microcontroller to get a non inverted signal similar to before. After a bit of port swapping I was able to use pin 49 as suggested to get my PIC24 to pick up the signal from the barcode scanner. I'm now able to pull the trigger of the scanner, scan a barcode, and have the code trigger text to be displayed on the lcd of the explorer board but my original logic loop seems to only output a string of x's of varying lengths no matter what barcode I scan. I'm now wondering if I'm trying to read the data with the most significant bit first while the signal coming in is least significant bit first or if the characters are overlapping in the internal UART buffer of the PIC24FJ1024GB610 since it only has a 4 character buffer.
    #8
    Aussie Susan
    Super Member
    • Total Posts : 3608
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/15 20:03:13 (permalink)
    0
    I have not heard of any UART that is NOT LSB first in the data packet.
    None of the code in this thread (and my pseudo code is included here) does any error checking. If the BAUD rates are out by more than 1-2% then you can get framing errors. If you have not set up the format correctly (number of bits, parity etc.) then you will get framing errors as well.
    Once you have an error then you really should handle it or you will not get valid data after that.
    Have you implemented my suggested changes to your logic?
    Susan
    #9
    asmith34
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2019/01/17 14:04:17
    • Location: 0
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/20 11:48:21 (permalink)
    0
    Hey Susan, after a bit of work I was able to implement your logic suggestions and now have the ability to read the first 5 characters of a barcode and have it display on the explorer LCD. My new issue is that after the first five characters display correctly the rest of the characters displayed are just the second character repeated. For example the barcode TBA2379093 would display as TBA23BBBBB and this occurs for any barcode. What confuses me is why it picks up and repeats the second character instead of just displaying nothing or displaying something different altogether. Any suggestions as to why this may be?
    #10
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/04/20 12:47:44 (permalink)
    0
    Please show EXACTLY the code you are now using.
     

    Nearly there...
    #11
    Gouthami c v
    Starting Member
    • Total Posts : 40
    • Reward points : 0
    • Joined: 2019/07/11 01:55:44
    • Location: 0
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/08/07 02:31:44 (permalink)
    0
    Hi every one,
    I have one doubt how to consider RB2(pin6) and RB7(pin11) are RxD & TxD respectively, in PIC24F04KA201 for UART configuration.
     
     
     
     
     
    Thanking you.
     
    #12
    ric
    Super Member
    • Total Posts : 23211
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC24 UART not reading data. Please help 2019/08/07 02:36:29 (permalink)
    5 (1)
    You just asked exactly the same question at https://www.microchip.com/forums/FindPost/1108099
    Please do NOT post multiple copies of questions.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    Gouthami c v
    Starting Member
    • Total Posts : 40
    • Reward points : 0
    • Joined: 2019/07/11 01:55:44
    • Location: 0
    • Status: offline
    Re: PIC24 UART not reading data. Please help 2019/08/07 02:39:28 (permalink)
    0
    OK i am sorry
    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5