• AVR Freaks

Hot!Clearing the EUSART write

Author
fsonnichsen
Super Member
  • Total Posts : 291
  • Reward points : 0
  • Joined: 2009/08/13 11:00:11
  • Location: 0
  • Status: offline
2020/07/13 07:23:43 (permalink)
0

Clearing the EUSART write

PIC18LF26K22
I usually get a stray byte in the EUSART upon startup and then things run fine after that. I am not running in interrupt mode--I let the MCC code do blocking.  Looking at the generated code I don't see a "clear write" command but on more inspection perhaps it is not possible to clear the buffer--it has already happened on startup and we must just discard the bytes.
  Is this correct or is there a way to clear the output buffer before startup?
Thanks
Fritz
#1

12 Replies Related Threads

    kiymik
    Junior Member
    • Total Posts : 94
    • Reward points : 0
    • Joined: 2005/02/05 01:30:10
    • Status: offline
    Re: Clearing the EUSART write 2020/07/13 09:24:57 (permalink)
    0
    I'm using 18F26K22 (not LF) with a lot of EUART usage, didn't face such situation. Can you explain more what is happening ? May be there is but I missed. And if possible related part of your code.
     
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11936
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Clearing the EUSART write 2020/07/13 10:09:57 (permalink)
    0
    It sounds like you're missing a pullup on the RX pin.
    #3
    crosland
    Super Member
    • Total Posts : 2017
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Clearing the EUSART write 2020/07/13 10:39:07 (permalink)
    +1 (1)
    I have seen crap coming out of the UART at startup. The way I fix it is to not enable the Tx pin as output until some time after reset.
    #4
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Clearing the EUSART write 2020/07/13 13:35:15 (permalink)
    0
    jtemples
    It sounds like you're missing a pullup on the RX pin.

    I think they are talking about TX.
    In that case, you have to look at what happens to that pin in your initialisation code.
    WHEN do you set it to be an output?
    WHAT is the state of the LAT register at that time?
    WHEN do you enable the USART?
     
    Best advice, make sure the LAT bit is 1 before you clear the TRIS bit.
    Make sure you don't try to send any data in less that 10 baud rate clock cycles after the pin has been pulled high.
     
     

    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
    fsonnichsen
    Super Member
    • Total Posts : 291
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: Clearing the EUSART write 2020/07/13 13:42:59 (permalink)
    0
    Thanks for some tips.
     
    The code is attached. 
    Pull ups do not help here.
    I tried toggling the TX in but that did not help.
     
    When the very 1st write occurs, I get a double quote at the beginning of the phrase. After that all works fine.
    Usually this does not happen-but it happens here. I have seen it before--Setting the TX to "disable" does not help either (e.g. RCSTA1 = 0x10 instead of RCSTA1 = 0x90). With other processors/compilers I would always delay the "open" macro but there is none for the PIC / MCC generated code.
     
    Thanks
    Fritz
    #6
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Clearing the EUSART write 2020/07/13 13:46:28 (permalink)
    +1 (1)
    Does MCC let you set the initial level (high or low) of the TX pin?
    You should set it to high. Right now that code is setting all pins low on power up.
     
     

    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!
    #7
    fsonnichsen
    Super Member
    • Total Posts : 291
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: Clearing the EUSART write 2020/07/13 18:27:47 (permalink)
    0
    Thanks RIC. Now that you mention it I seem to recall something like that for the EUSART in PIC18 (but not 24). The SYSINIT MCC startup etc takes hold of a lot of this stuff-but not enough to do it right--and unless I am programming this device a lot I forget the additional incantations over the years. Not sure why the pin setting is not taken care of in the init.
       At any rate I will try it tomorrow and let you know what happens
    Thanks
    fritz
    #8
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Clearing the EUSART write 2020/07/13 18:46:02 (permalink)
    +1 (1)
    crosland
    I have seen crap coming out of the UART at startup. The way I fix it is to not enable the Tx pin as output until some time after reset.

    The USART peripheral forces the pin to be an output as soon as you enable the USART and set the TXEN bit.
    TRIS is ignored after that.
    Setting it high and out at least one character time before the USART starts sending is the best way to avoid rubbish on startup.
     

    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!
    #9
    fsonnichsen
    Super Member
    • Total Posts : 291
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: Clearing the EUSART write 2020/07/14 11:47:38 (permalink)
    +1 (1)
    OK RIC. Got into the lab today and coded in the MCC PIN considerations and you were right. The extra (") sign at start up disappears when I use "Start High" on the startup for the EUSART PINs.  Something I just simply forget when coding thru MCC. Which they could put it as an option in the initialize. 
      I have attached the MCC gui for those that want it.
     
    Thanks again!
    Fritz

    Attached Image(s)

    #10
    fsonnichsen
    Super Member
    • Total Posts : 291
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: Clearing the EUSART write 2020/07/14 11:47:43 (permalink)
    0
    OK RIC. Got into the lab today and coded in the MCC PIN considerations and you were right. The extra (") sign at start up disappears when I use "Start High" on the startup for the EUSART PINs.  Something I just simply forget when coding thru MCC. Wish they could put it as an option in the initialize. 
      I have attached the MCC gui for those that want it.
     
    Thanks again!
    Fritz
    post edited by fsonnichsen - 2020/07/15 08:47:59
    #11
    crosland
    Super Member
    • Total Posts : 2017
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: Clearing the EUSART write 2020/07/15 12:09:50 (permalink)
    0
    ric
    crosland
    I have seen crap coming out of the UART at startup. The way I fix it is to not enable the Tx pin as output until some time after reset.

    The USART peripheral forces the pin to be an output as soon as you enable the USART and set the TXEN bit.
    TRIS is ignored after that.
    Setting it high and out at least one character time before the USART starts sending is the best way to avoid rubbish on startup.

     
    Sorry, I posted that without checking my code (it's a few years ago). What I actually did was set the pin as input before enabling the UART, a delay before enabling did not work. Weak pullups were enabled. No idea, now, why that worked. It was connected to the GPIO UART pins on a Raspberry Pi. The R-Pi Rx input may have a pull-up, which would proxy for setting it high.
     
     
    #12
    Jerry Messina
    Super Member
    • Total Posts : 540
    • Reward points : 0
    • Joined: 2003/11/07 12:35:12
    • Status: offline
    Re: Clearing the EUSART write 2020/07/16 02:51:30 (permalink)
    +1 (1)
    I have seen crap coming out of the UART at startup. The way I fix it is to not enable the Tx pin as output until some time after reset

    I typically add an external pullup to the TX output pin. That way it's high (idle) as soon as power is applied and stays that way until the UART is initialized.
     
    You can try playing around with the pin initialization sequence (set as output high, int pull ups, etc) but depending on your hardware and config, all of those methods can leave a big gap between the time reset starts and the pin gets setup. For example, if you use the internal powerup timer you may have the TX pin floating for a hundred msecs or more.
     
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5