• AVR Freaks

Hot!How to wake-up from sleep on USART RX

Author
kazu0055
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2020/12/20 21:04:02
  • Location: 0
  • Status: offline
2021/02/24 05:14:29 (permalink)
0

How to wake-up from sleep on USART RX

I wrote a program to wake up from sleep with UART, but I can't read the data correctly.
The device used is PIC16F15313.
The code is
---------------------------------------------------------------------
while (1)
{
// Add your application code
BAUD1CONbits.WUE = 1; //Wake-up Enable
BAUD1CONbits.ABDEN = 1; //Auto-Baud Detect Enable

SLEEP();
NOP();


/****/
// if(BAUD1CONbits.WUE)
if(EUSART1_is_rx_ready())
{
for(i=0; i<8 ; i++){
rxData = EUSART1_Read();
}
}
// __delay_ms(10);

if(EUSART1_is_tx_ready())
{
for(i=0; i<8 ; i++){
EUSART1_Write(rxData);
}
}
EUSART1_Write(0x0d);
EUSART1_Write(0x0a);
---------------------------------------------------------------------
 
Please tell me the problem with the above code.
The project file is attached for reference.
 
#1

14 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3859
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/02/24 18:59:35 (permalink)
    +1 (1)
    Please:
    - use the code tags when you include code in the text - it stops you getting italics characters instead of array indexes for one thing
    - expand on what you mean by 'can't read the data correctly'
     
    As a wild guess, looking at the code for 'EUSART1_Read()', it will block waiting for a character to be received. You call this function 8 times but it may well be that the 'sleep' is exited after the first character is received and so you stall after that.
    On the other hand I note that you disable the GIE which means that the EUSART will NOT call its interrupt handler. However the EUSART1_Initialize() function sets the IE bit (which is needed for the device to come out of sleep). Therefore you may well be coming out of sleep but the UART is not processing any characters and the EUSART1_is_rx_ready() function is always saying 'nothing to see here'.
     
    However without knowing exactly what is happening, this is all just speculation on my part.
     
    I must admit that the more I see of MCC code, especially for the very straightforward modules such as the UART, the more I think that it just trips people up instead of helping. But I do come from 'the old school' and like to control (or at least understand) all of the code.
     
    Susan
    #2
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: online
    Re: How to wake-up from sleep on USART RX 2021/02/24 19:58:07 (permalink)
    +3 (3)
    Hi,
    Waking up from Sleep by UART interrupt,
    is Not expected to receive the first character correctly.
    When Microcontroller is asleep, then Oscillator is stopped, so UART clock is Not running either.
    UART will wake up, but may not receive characters correctly until oscillator is started and stable.
     
    Oscillator startup take different time, depending upon what oscillator is used,
    internal FRC oscillator is much faster to start, than any crystal oscillator.
     
    If there is a break signal before first character, there may be time for oscillator to start. 
    Or if there is delay between characters, following characters may be received correctly.
    If there is no delay between characters, there may be multiple wrong characters, before UART is able to synchronize.
     
        Mysil
    #3
    RISC
    Super Member
    • Total Posts : 6067
    • Reward points : 0
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/02/25 09:21:37 (permalink)
    +1 (1)
    Hi,
    Additionally to Mysil good recommendation, you can program the pin which you assign to UART RX to use IOC (Interrupt On Change). By doing that, the processor will wake up on the first UART start edge transition.
    Pay also attention that the internal oscillator has +/-2% max error (maximum error for UART communication) on frequency from 0 to 60C. 
    Regards
    post edited by RISC - 2021/02/25 12:47:27

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #4
    1and0
    Access is Denied
    • Total Posts : 12269
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/02/25 09:54:37 (permalink)
    +1 (1)
    As Mysil has said, there is oscillator startup time, which can take as much as 1024 oscillation cycles to stable, depending on the oscillator mode.
     
    Instead of full Sleep mode, you can use Idle mode where the CPU halted but the peripheral clocks continue to run.
    #5
    kazu0055
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2020/12/20 21:04:02
    • Location: 0
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/02/28 18:36:34 (permalink)
    0
    Hi All
    Thank you for your suggestions.
    By reviewing the program and transmitted data, I started to receive the correct data.
    1. The description position of "ABDEN" in the program has been changed as follows.
        
     for(i=0; i<8 ; i++){
         rxData = EUSART1_Read();
        if(i<1){
         BAUD1CONbits.ABDEN = 1; //Auto-Baud Detect Enable
        }
     
    2. The transmission data has been changed to "0x00 0x00 0x00 0x55 0x01 0x02 ..".
        The first 3 bytes are "the Break character"
        The 4th byte is baud rate measurement
     
    Thank You
    Regards K.Kanata
    #6
    ric
    Super Member
    • Total Posts : 30244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/02/28 20:10:29 (permalink)
    +2 (2)
    0x00 is not a "break" character.
    It is START bit, eight 0 bits, and a STOP bit, so nine zeroes and a one.
    A BREAK is at least ten zero bits.
     

    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
    kazu0055
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2020/12/20 21:04:02
    • Location: 0
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/03/01 00:59:08 (permalink)
    0
    Hi ric
    Thank you for your suggestion.
    I think of a way to send ten zero bits.
    I wish I could handle it with software such as Tera Term. .. .. ..
    #8
    ric
    Super Member
    • Total Posts : 30244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/03/01 01:27:34 (permalink)
    +1 (1)
    Are you trying to implement a LIN slave?
    If not, why are you using autobaud?
     

    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
    oliverb
    Super Member
    • Total Posts : 441
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/03/01 01:31:19 (permalink)
    +1 (1)
    Different terminals handle break differently. Putty lists it as a "Special command" in the right-click menu.
    Tera term might use Alt+B, or the keyboard "break" key might be supported?
     
    Anyway a serial "break" is a long period of zero, usually significantly longer than one character. Because it is so long it can be detected by a simple timing circuit.
     
    For this wake-from-sleep application I think the solution is just to do what OP is already doing and send a short sequence of nulls as sync characters.
     
    #10
    kazu0055
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2020/12/20 21:04:02
    • Location: 0
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/03/01 03:35:52 (permalink)
    0
    Hi ric
    I'm trying with Rx on UART.
    The autobaud is set because it was described in the note of section 33.3.5 of the manual (DS40001897A) of PIC16F15313.
    Is it not necessary to set autobaud on the UART?
    #11
    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: online
    Re: How to wake-up from sleep on USART RX 2021/03/01 04:09:43 (permalink)
    +1 (1)
    Hi,
    Autobaud detection, is for the case that UART receiver do Not know what frequency the transmitter will be going to use.
    But if the UART clock is Not already running with a stable frequency, autobaud logic may cause additional confusion.
     
    If you know what signaling bitrate is to be used, then it may be better to disable the autobaud feature,
    especially if the UART will wake up with unstable clock signal frequency.
     
        Mysil
    #12
    ric
    Super Member
    • Total Posts : 30244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/03/01 04:11:21 (permalink)
    +2 (2)
    As above.
    Autobaud is rarely used, EXCEPT in a few specific circumstance. i.e. it is not normal to use it.
    Usually you would calculate what value to load into BAUDCON to get the required bitrate.
     

    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
    kazu0055
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2020/12/20 21:04:02
    • Location: 0
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/03/01 15:45:44 (permalink)
    0
    Thank you
    I understand how to Autobaud .
    #14
    RISC
    Super Member
    • Total Posts : 6067
    • Reward points : 0
    • Status: offline
    Re: How to wake-up from sleep on USART RX 2021/03/01 16:34:03 (permalink)
    +1 (1)
    Hi,
    Autobaud is used a lot in LIN communication ("special UART" mode for automotive applications) but it can be used elsewhere for reliable UART communications
    The reason is to save external XTAL.
    Receiving a special pattern ( 0x55 ) on UART enable the clock to synchronize thanks to the succession of 0101 0101 which enable period measurement and setup of the internal baudrate divider :
    https://ww1.microchip.com...en/AppNotes/93069A.pdf
    Regards

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #15
    Jump to:
    © 2021 APG vNext Commercial Version 4.5