• AVR Freaks

Helpful ReplyHot!PIC24F32KA302, RX on UART1 not reading correctly. Using MCC.

Author
Aethelstan
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/08/18 08:12:59
  • Location: 0
  • Status: offline
2019/04/19 09:24:38 (permalink)
0

PIC24F32KA302, RX on UART1 not reading correctly. Using MCC.

Hi, I am trying to load in some strings of data from a GPS unit. When reading the data via a USB UART or decoding it on my scope, the data is correct. When I try to read it into a buffer on the PIC, the data is gibberish, although it is consistent in parts where the message is also consistent. That leads me to think UART1 is receiving correctly and I am somehow creating the issue in my code. The rx_buffer is set at [50] which I think means it has 51 bytes, which should be big enough. My baud rate is 9,600, no parity, eight data bits and one stop bit. I know that UART1 itself is enabled as I am able to transmit data on it perfectly fine, in fact the data I am reading is in response to some I am sending. Has anyone got any pointers to help me in the right direction, please?
 
 while ((UART1_StatusGet() & UART1_RECEIVER_IDLE))
    {
    }

    i=0;
    while ((UART1_StatusGet() & UART1_RX_DATA_AVAILABLE))
    {
        rx_buffer[i] = UART1_Read();
        i++;


    }

Attached Image(s)

#1
KTrenholm
Super Member
  • Total Posts : 691
  • Reward points : 0
  • Joined: 2012/08/08 14:04:23
  • Location: Connecticut, USA
  • Status: offline
Re: PIC24F32KA302, RX on UART1 not reading correctly. Using MCC. 2019/04/19 11:31:30 (permalink) ☄ Helpfulby Aethelstan 2019/04/21 02:55:34
4 (1)
Granted I don't use MCC, but I do use this PIC.  Is this the only code in your main program loop?  Because you're basically just going to hang in that idle check while() loop forever until some data arrives.  I'd lean more toward something like:
 

 
 if ((UART1_StatusGet() & UART1_RECEIVER_IDLE) != 0){
    i=0;
} else {
    while ((UART1_StatusGet() & UART1_RX_DATA_AVAILABLE) != 0)
    {
        rx_buffer[i++] = UART1_Read();
    }
}
 

 
I'd also look into an alternate way to reset your incoming buffer index.   One way would be, if every transmission starts with a '$' character, use that to reset your index since you know it's the start of a new transmission:

 
uint8_t ch_in = 0;
while ((UART1_StatusGet() & UART1_RX_DATA_AVAILABLE) != 0)
    {
        ch_in = UART1_Read();
 
        if (ch_in == '$'){
            i = 0;
        }
 
        rx_buffer[i++] = ch_in;
    }
}
 

post edited by KTrenholm - 2019/04/19 11:33:29
#2
Aethelstan
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/08/18 08:12:59
  • Location: 0
  • Status: offline
Re: PIC24F32KA302, RX on UART1 not reading correctly. Using MCC. 2019/04/21 02:55:18 (permalink)
0
Thanks for the reply, it turned out I hadn't set up UART1 in MCC correctly for how I expected it to work. I hadn't turned on the UART interrupts and therefore was missing some of the MCC functionality. After enabling the interrupts, it has created a software input buffer and I am now able to read and parse the received messages. I'm not going to pretend I understand it, but working is good enough for today :)
#3
Aussie Susan
Super Member
  • Total Posts : 3532
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: PIC24F32KA302, RX on UART1 not reading correctly. Using MCC. 2019/04/22 19:36:22 (permalink)
4 (1)
By the way an array declaration of '[50]' sets aside 50 items that are indexed as '0' to '49'.
Susan
#4
Jump to:
© 2019 APG vNext Commercial Version 4.5