• AVR Freaks

Hot!UART2 output not as expected

Author
Kev2005
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2020/04/02 07:37:00
  • Location: 0
  • Status: offline
2020/06/03 07:02:58 (permalink)
2 (1)

UART2 output not as expected

I'm working on a dspic30f6013a and am writing a string to UART2:
 
#define START_SD01 "_ST_SD01"

WriteTextUART2(START_SD01);

// where:

void WriteTextUART2(char* a)
{
 putsUART2((unsigned int*)a);
}

// where putsUART2 is a lib function:

void putsUART2(unsigned int *buffer) __attribute__ ((section (".libperi")));

 
Listening on a serial port, I get the following:
 
248 120 60 255 128 120 60 0 120 60 15 120 60 255 128 120 60 0 0 120 0 128 120 0 127 192
 
which are 24 bytes instead of 16. I'm not flushing the TX buffer before hand. What's the recommended way about sending bytes out on uart?
post edited by Kev2005 - 2020/06/03 08:30:43
#1

18 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART2 output not as expected 2020/06/03 14:59:24 (permalink)
    3.67 (3)
    That looks like a baud rate mismatch.
    You have revealed nothing about what baud rate your PIC is using, or how you set it.
    Nor have you mentioned what you are "listening" to the serial port with, or what settings it is using.
    A simple first step is to get your PIC to send "0x55" over and over, and watch the TX pin with a scope to ensure you are getting the expected pattern.
     
     

    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!
    #2
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/06/04 00:01:23 (permalink)
    0
    ric
    That looks like a baud rate mismatch.
    You have revealed nothing about what baud rate your PIC is using, or how you set it.
    Nor have you mentioned what you are "listening" to the serial port with, or what settings it is using.
    A simple first step is to get your PIC to send "0x55" over and over, and watch the TX pin with a scope to ensure you are getting the expected pattern.
     



    Oops...
     

    #define SYS_FREQ (9216000L*16) 
    #define SYS_FCY (SYS_FREQ/4) 
     
    //RS232
    #define DESIRED_BAUDRATE (115200) 
     
     
     
    void initUART2()
    {
     
        U2BRG = ((((SYS_FCY/DESIRED_BAUDRATE)/8)-1)/2);
     
        U2MODE = 0b1000000000000000;
     
        U2STA = 0b1000010000000000;
     
        IFS1bits.U2RXIF = 0;
        IEC1bits.U2RXIE = 1;
    }
     

     
    The client is a C# app with a br of 115200:
     

        public long openPort(string port_name)
        {
          try
         {
           _serialPort = new SerialPort();
           _serialPort.PortName = port_name;
           _serialPort.BaudRate = 115200;
           _serialPort.Parity = 0;
           _serialPort.DataBits = 8;
           _serialPort.Handshake = Handshake.None;
           _serialPort.ReadTimeout = 500;
           _serialPort.WriteTimeout = 500;
     
     
     
           _serialPort.Open();
          }catch(SystemException){
            MessageBox.Show("Error Opening Port");
          }
          return 1;
          }

     
     
     
     
    post edited by Kev2005 - 2020/06/04 01:23:10
    #3
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/06/05 02:49:14 (permalink)
    1 (1)
    UP!
    #4
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART2 output not as expected 2020/06/05 02:57:38 (permalink)
    5 (1)
    So did you try observing the TX pin with a scope yet?
     
    "The client is a C# app".
    On what? A PC?
    How is your PIC actually connected to the PC?
    i.e. is the PC using a USB/RS232 adaptoor?
    Is there a MAX232 style buffer in the PIC serial signals?
     
     

    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!
    #5
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/06/08 02:49:18 (permalink)
    0
    ric
    So did you try observing the TX pin with a scope yet?
     

     
    The mcu cannot be monitored directly; I only know that I can write to Tx.
     
    ric
    On what? A PC?

     
    Yep.
     
    ric
    How is your PIC actually connected to the PC?

     
    Serial usb; writing to "COM1" using ds30Loader suite. 
     
    ric
    i.e. is the PC using a USB/RS232 adaptoor?
    Is there a MAX232 style buffer in the PIC serial signals?

     
    Yep (x2)
     



    #6
    nice
    Super Member
    • Total Posts : 1092
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: UART2 output not as expected 2020/06/11 18:22:18 (permalink)
    5 (1)
    Kev2005

    #define SYS_FREQ (9216000L*16)

    If you’re operating your dsIPIC30F6013A with a 9.216 MHz oscillator @ 16x PLL, the resulting SYS_FREQ of 147.456 MHz is much higher than any dsPIC30F allows (120 MHz). And if your dsPIC30F is actually running slower than your definition of SYS_FREQ implies, the calculated value for U2BRG is wrong. In both cases it’s rather unlikely that the UART does work as intended.


     
    #7
    dan1138
    Super Member
    • Total Posts : 3620
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/06/11 19:01:39 (permalink)
    4.5 (2)
    As nice has so kindly pointed out to are running your dsPIC30F3013A with too fast an external oscillator to use the x16 PLL mode.
     
    This table shows how fast the external oscillator can be for the PLL modes:

     
    If you need to use "standard" baud rates and "standard" crystal frequencies then the best choice is a 7.3728MHz crystal and the x16 PLL mode. This will net out to a 29.4912 MIPS instruction rate with a UART baud rate divisor of 16 for 115200 bits per second.
    post edited by dan1138 - 2020/06/12 08:24:58

    Attached Image(s)

    #8
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/06/30 00:51:15 (permalink)
    0
    nice
    Kev2005

     
    #define SYS_FREQ (9216000L*16)
     

    If you’re operating your dsIPIC30F6013A with a 9.216 MHz oscillator @ 16x PLL, the resulting SYS_FREQ of 147.456 MHz is much higher than any dsPIC30F allows (120 MHz). And if your dsPIC30F is actually running slower than your definition of SYS_FREQ implies, the calculated value for U2BRG is wrong. In both cases it’s rather unlikely that the UART does work as intended.





    Looking at page 494 from:
     
    http://ww1.microchip.com/downloads/en/devicedoc/70046d.pdf
     
    UxBRG is determined as:
     
    UxBRG = Fcy / (16 * Baudrate) - 1
     
    In my case Fcy = 9.216 MHz, Baudrate = 115200 giving
     
    UxBRG = 9216000 / (16 * 115200) - 1 = 4? (no PLL)
     
    PLL x4 (as in code) =>
    UxBRG = (4 * 9216000) / (16 * 115200) - 1 = 36.864.000 / 1.843.552‬ - 1 = 19.99 - 1 = 18.99 = ~19
     
    Code output:
     
    U2BRG = ((((SYS_FCY/DESIRED_BAUDRATE)/8)-1)/2)
    U2BRG = ((((36.864.000/115200)/8)-1)/2) = 19.5
     
    Both values are rounded to 19 (unsigned long). 
     
    Why is it incorrect? 
     
     
    #9
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/06/30 00:57:53 (permalink)
    0
    dan1138
    As nice has so kindly pointed out to are running your dsPIC30F3013A with too fast an external oscillator to use the x16 PLL mode.
     
    This table shows how fast the external oscillator can be for the PLL modes:

     
    If you need to use "standard" baud rates and "standard" crystal frequencies then the best choice is a 7.3728MHz crystal and the x16 PLL mode. This will net out to a 29.4912 MIPS instruction rate with a UART baud rate divisor of 16 for 115200 bits per second.




    I'm currently using 9.216 MHz with PLL x4, baudrate 115200:
     
    UxBRG = Fcy / (16 * Baudrate) - 1
     
    giving:
     
    UxBRG = (4 * 9216000) / (16 * 115200) - 1 = 36.864.000 / 1.843.552‬ - 1 = 19.99 - 1 = 18.99 = ~19
     
    Will this yield the desired, 115200 baudrate?
     
     
     
     
    #10
    nice
    Super Member
    • Total Posts : 1092
    • Reward points : 0
    • Joined: 2004/09/18 11:42:25
    • Location: Germany
    • Status: offline
    Re: UART2 output not as expected 2020/06/30 16:35:10 (permalink)
    5 (1)
    Your wrong assumption is that a 9.216 MHz oscillator with 4x PLL would result in an Fcy of 36.864 MHz. The actual Fcy is 9.216 MHz. As I’ve previously assumed, your UxBRG setting is wrong.
    #11
    dan1138
    Super Member
    • Total Posts : 3620
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/06/30 17:19:31 (permalink)
    5 (2)
    Kev2005
    I'm currently using 9.216 MHz with PLL x4, baudrate 115200:
     
    UxBRG = Fcy / (16 * Baudrate) - 1
     
    giving:
     
    UxBRG = (4 * 9216000) / (16 * 115200) - 1 = 36.864.000 / 1.843.552‬ - 1 = 19.99 - 1 = 18.99 = ~19
     
    Will this yield the desired, 115200 baudrate?



    Let me recap what I understand what you have said:
     
    1 - You are using a dsPIC30F3013A
    2 - You have an external oscillator of 9.216MHz
    3 - The x4 PLL is enabled
    4 - The required baud rate is 115200
     
    The system oscillator frequency(FSYS) when using the PLL is (OSC)*(PLL) or 9.216*4 or 36.864MHz.
     
    The instruction cycle frequency(FCYC) is defined as the system oscillator frequency(FSYS) divided by 4.
     
    This results in FCYC = 36.864/4 or 9.216MHz.
     
    The baud rate count register setting is defined as: FCYC/(16 * <required baud rate>)-1
     
    In your case this would be (FCYC/1843200)-1 or (9216000/1843200)-1 or 4
     
    With an external oscillator of 9.216MHz the dsPIC30F3013A will also work with an x8 PLL and this will result in a baud rate count register setting of 8.
    #12
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/07/01 00:55:18 (permalink)
    0
    dan1138
    Kev2005
    I'm currently using 9.216 MHz with PLL x4, baudrate 115200:
     
    UxBRG = Fcy / (16 * Baudrate) - 1
     
    giving:
     
    UxBRG = (4 * 9216000) / (16 * 115200) - 1 = 36.864.000 / 1.843.552‬ - 1 = 19.99 - 1 = 18.99 = ~19
     
    Will this yield the desired, 115200 baudrate?



    Let me recap what I understand what you have said:
     
    1 - You are using a dsPIC30F3013A
    2 - You have an external oscillator of 9.216MHz
    3 - The x4 PLL is enabled
    4 - The required baud rate is 115200
     
    The system oscillator frequency(FSYS) when using the PLL is (OSC)*(PLL) or 9.216*4 or 36.864MHz.
     
    The instruction cycle frequency(FCYC) is defined as the system oscillator frequency(FSYS) divided by 4.
     
    This results in FCYC = 36.864/4 or 9.216MHz.
     
    The baud rate count register setting is defined as: FCYC/(16 * <required baud rate>)-1
     
    In your case this would be (FCYC/1843200)-1 or (9216000/1843200)-1 or 4
     
    With an external oscillator of 9.216MHz the dsPIC30F3013A will also work with an x8 PLL and this will result in a baud rate count register setting of 8.




    Thanks! It worked; characters are being sent as expected, the only issue being that the UART2 buffer is not being emptied prior to send; sending 0x55 and then 0x56 again produces:
     
    0x55 on the first send and 
    0x55 0x56 even though only 0x56 was sent. 
     
     
    #13
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART2 output not as expected 2020/07/01 01:52:36 (permalink)
    5 (1)
    Please show the exact code you did this test with.
     

    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!
    #14
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/07/01 03:26:28 (permalink)
    0
    ric
    Please show the exact code you did this test with.
     





     
    void startButtonHandler() {
    ...
    while(U1STAbits.UTXBF==1);
    WriteUART2(START_SD01);
    while(U1STAbits.TRMT==0);
    }
     
    void stopButtonHandler() {
    ...
    while(U1STAbits.UTXBF==1);
    WriteUART2(STOP_SD01);
    while(U1STAbits.TRMT==0);
    }
     
    int main (void) {
    ...
    while (1) {
    ClrWdt();
    uint8_t nMsgId = MSG_ID_INVALID;
    uint16_t wParam = 0;

    if( getMsg( &nMsgId, &wParam ))
    {
    switch (nMsgId) {
    case START_BUTTON_PRESSED:
    startButtonHandler();
    break;

    case STOP_BUTTON_PRESSED:
    stopButtonHandler();
    break;
     
    default:
    break;
    }
    }

    return 0;
    }
     

     
    The user may push the start/stop buttons at any time. Tried also with U1STAbits; no luck. 
     
     
    post edited by Kev2005 - 2020/07/01 04:03:33
    #15
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART2 output not as expected 2020/07/01 03:29:24 (permalink)
    5 (1)
    Why are you checking the status for UART1, but sending the data to UART2?
     

    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!
    #16
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/07/01 03:49:03 (permalink)
    0
    ric
    Why are you checking the status for UART1, but sending the data to UART2?
     



    Tried both:
     

    while(U2STAbits.UTXBF==1);
    WriteUART2(STOP_SD01);
    while(U2STAbits.TRMT==0);

     
    Result was the same, regardless. 
    #17
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: UART2 output not as expected 2020/07/01 04:01:24 (permalink)
    5 (1)
    So what value is STOP_SD01 ?
    You said you are sending 0x55 and 0x56, but this code is only sending the same value regardless of which function you call.

    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!
    #18
    Kev2005
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2020/04/02 07:37:00
    • Location: 0
    • Status: offline
    Re: UART2 output not as expected 2020/07/01 04:05:26 (permalink)
    0
    ric
    So what value is STOP_SD01 ?
    You said you are sending 0x55 and 0x56, but this code is only sending the same value regardless of which function you call.




    #define START_SD01 0x55
    #define STOP_SD01 0x56

     
    Edited the code; two ints are sent when two buttons are pressed. 
     
     
     
    #19
    Jump to:
    © 2020 APG vNext Commercial Version 4.5