Sleep & nap


Closed Thread
Results 1 to 25 of 25

Thread: Sleep & nap

  1. #1

    Default Sleep & nap

    Hi!

    Using picbasicpro 2,50b and having
    DEFINE WDT_OFF ; one of the first statements
    and then later

    Both SLEEP 1
    and
    NAP 1
    hangs, get stucked, bog down

    Is the define wrong somehow or what do I miss here?
    Never used these commands before. Tried also
    @ DEVICE pic18F4550 WDT_OFF
    but that produced error 122.
    Tried some other things also but DEFINE WDT_OFF was the only one (I found) that compiled without errors.

    I'm missing something, but what?

    Thank's for all hints

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    From the manual
    Code:
    The Watchdog Timer is used in conjunction with the SLEEP and NAP
    instructions to wake the PICmicro MCU after a certain period of time.
    You need to enable the WDT. How about trying this
    @device 18F4550, WDT_ON

    I do not know if this will work as I've not checked

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Also
    Code:
    @device 18F4550, WDT_ON
    is PM syntax.

    Must use MPASM for 18Fs
    Code:
    @ __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
    A good late night read.
    http://www.picbasic.co.uk/forum/showthread.php?t=543
    Dave
    Always wear safety glasses while programming.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    Also
    Code:
    @device 18F4550, WDT_ON
    is PM syntax.

    Must use MPASM for 18Fs
    Code:
    @ __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
    A good late night read.
    http://www.picbasic.co.uk/forum/showthread.php?t=543
    Thank you Dave and Jerson!

    Of course, it seems to me that I can't read
    That is also clearly written in the manual "...If the Watchdog Timer is not enabled, ther processor will sleep forever..., page 105", so I did not see the small word "not"...

    However, changing that to WDT_ON does not produce the wanted result.
    Added also WDTPS 128 but again without success.

    Is there something else involved here?

    That "543" is not solving this issue? It is showing that there can be many things that can go wrong... when you are not extremely familiar with this all...

  5. #5
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    there can be many things that can go wrong
    That is when the fun starts

    Can you post your whole code?
    The actual problem might be someplace else.
    Dave
    Always wear safety glasses while programming.

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    That is when the fun starts

    Can you post your whole code?
    The actual problem might be someplace else.
    Here, the fun has alredy started

    Code:
    DEFINE LOADER_USED 1
    DEFINE RESET_ORG 1000h      ' For Microchip USB Bootloader
    DEFINE INTERRUPT_ORG 1008h  ' For Microchip USB Bootloader
    define WDT_ON
    define WDTPS 128 
    
    DEFINE LCD_DREG       PORTD	' Set LCD Data port
    DEFINE LCD_DBIT       4		' Set starting Data bit (0 or 4 if 4-bit bus)
    DEFINE LCD_RSREG      PORTC	' Set LCD Register Select port
    DEFINE LCD_RSBIT      2		' Set LCD Register Select bit
    DEFINE LCD_EREG       PORTC	' Set LCD Enable port
    DEFINE LCD_EBIT       1		' Set LCD Enable bit
    DEFINE LCD_BITS       4		' Set LCD bus size (4 or 8 bits)
    DEFINE LCD_LINES      4		' Set number of lines on LCD
    DEFINE LCD_COMMANDUS  2000	' Set command delay time in us, 2000 us = 2ms
    DEFINE LCD_DATAUS     50	' Set data delay time in us, 50 us = 0,05ms
    
    powerOut     var PORTE.0
    onOffPressed var PORTD.2
    b var byte
    clear
    
    TRISE = %00001000   
    TRISC = %10110000   
    TRISD = %00001100
    powerOut = 1
       
    pause 1
    lcdout $FE, 1
    pause 1
    
    pstart:
      powerout = not onoffpressed
      lcdout $FE,2, "b=", dec b
      pause 100
      b = b + 1
      'nap 5
    goto pstart
    So, I can see b living with nap or sleep commented, but it hangs when not commented.

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default

    Hi,

    From " The Manual" $ 5.46


    The 16-bit
    core devices, including the 17Cxxx and 18Xxxxx parts use a postscaler
    set at programming time to configure the Watchdog timeout period.

    The compiler will disregard the Period set in the NAP instruction for the 16-
    bit core devices.
    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    From " The Manual" $ 5.46

    Alain
    TRUE!

    But SLEEP should work, shouldn't it?
    The problem is somewhere else, but where?

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Question

    Hi,

    You want a ~1 sec PAUSE ...

    use PAUSE 1000 !!!

    a PAUSE can be "interrupted" so, there are no problem to use it ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    If you're using the Microchip USB loader, it may have WDT disabled in config.

    Try inserting WDTCON.0=1 before any NAP or SLEEP commands to enable WDT.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  11. #11


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    If you're using the Microchip USB loader, it may have WDT disabled in config.

    Try inserting WDTCON.0=1 before any NAP or SLEEP commands to enable WDT.
    One would have think that would work, but it did not
    What might be wrong?

    WDTCON.0=1
    sleep 1
    lcdout $FE, $C0, "ok"

    Can't still se the "ok"

  12. #12
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default

    Can't still see the "ok"

    Could the problem come from the LCD setup ???

    Code:
    pause 1
    lcdout $FE, 1
    pause 1
    Generally, we allow 500 ms min for the LCD Power on ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  13. #13
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    How long are you waiting? If the WDT poscaler in config is set to 32,768 it's going to take around 131 seconds before it wakes-up.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  14. #14


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    How long are you waiting? If the WDT poscaler in config is set to 32,768 it's going to take around 131 seconds before it wakes-up.
    the WDTPS is 128 as in #9, and actually it takes several seconds before that sleep instrucion is reached in the demo code (#9) as well as in the "big" code where I spotted this phenomena/error/failure at first.

    I have never seen sleep working, yet...
    but I would really like to see it

  15. #15
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    How are you setting the WDT postscaler to 128?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  16. #16


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    How are you setting the WDT postscaler to 128?
    Like this:
    define WDT_ON
    define WDTPS 128

    Is that ok?

  17. #17
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    The watchdog timer postscaler & on/off options are configuration options. These are set at
    program time.

    PBP allows you to DEFINE pretty much anything without throwing an error, but that doesn't
    mean your defines are doing anything.

    If you're using the older version Microchip USB boot-loader firmware, these are set in the C
    code.

    Code:
    #pragma config WDT      = OFF
    #pragma config WDTPS    = 32768
    You can open the loader .HEX file with your programmer, change WDTPS from 32768 to 128
    then save the .HEX file & re-program your 4550 with the modified loader firmware. Then you
    will have a postscaler of 128. I would leave WDT=OFF, and use the software enable option
    WDTCON.0=1.

    You'll also want to use DEFINE OSC 48 so PBP will use the correct timing. Without defining
    the osc speed PBP defaults to 4MHz.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  18. #18


    Did you find this post helpful? Yes | No

    Default

    Thank you Bruce!

    Quote Originally Posted by Bruce View Post
    The watchdog timer postscaler & on/off options are configuration options. These are set at program time.

    PBP allows you to DEFINE pretty much anything without throwing an error, but that doesn't mean your defines are doing anything.
    So, this means that you can not override them later with your running program?
    If you're using the older version Microchip USB boot-loader firmware, these are set in the C code.
    The date for the "USB Device - HID - HID Bootloader - C18 - PIC18F4450.hex" is 19.6.2008. I opened the zip-file maybe 1,5-2 month ago.
    Code:
    #pragma config WDT      = OFF
    #pragma config WDTPS    = 32768
    You can open the loader .HEX file with your programmer, change WDTPS from 32768 to 128 then save the .HEX file & re-program your 4550 with the modified loader firmware. Then you will have a postscaler of 128. I would leave WDT=OFF, and use the software enable option WDTCON.0=1.
    I don't grasp this. How can I see or found WDTPS in a .HEX file?

    You'll also want to use DEFINE OSC 48 so PBP will use the correct timing. Without defining the osc speed PBP defaults to 4MHz.
    My program has DEFINE OSC 48, so that can not be the reason for this sleep-error.

  19. #19
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    So, this means that you can not override them later with your running program?
    Some 18F parts allow you to change config settings at run-time, but I wouldn't advise trying this until you're a bit more familiar with how to set them & what they all do. If you're using a boot-loader, you'll need to know every setting the loader expects, or you may end up doing a lot of loader firmware re-programming. If you search here for modifying config settings on-the-fly you'll find a couple routines showing you how.

    [The date for the "USB Device - HID - HID Bootloader - C18 - PIC18F4450.hex" is 19.6.2008. I opened the zip-file maybe 1,5-2 month ago.
    You probably have the latest version then.

    I don't grasp this. How can I see or found WDTPS in a .HEX file?
    Easy. Open the loader .HEX file with your programmers software. Change it, save the .HEX file, and re-program the loader into your PIC.

    My program has DEFINE OSC 48, so that can not be the reason for this sleep-error.
    OK. It wasn't shown in the example you posted. Note this won't have anything to do with the WDT or sleep error. Only timing for PBP commands.

    If your programmers software doesn't allow you to read a .HEX file and make changes to config settings, then you can re-compile the loader firmware with the free version of C18.

    Or I can send you a modified version .HEX file if you need it.

    What USB board are you using?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  20. #20


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    Some 18F parts allow you to change config settings at run-time, but I wouldn't advise trying this until you're a bit more familiar with how to set them & what they all do. If you're using a boot-loader, you'll need to know every setting the loader expects, or you may end up doing a lot of loader firmware re-programming. If you search here for modifying config settings on-the-fly you'll find a couple routines showing you how.

    You probably have the latest version then.

    Easy. Open the loader .HEX file with your programmers software. Change it, save the .HEX file, and re-program the loader into your PIC.

    OK. It wasn't shown in the example you posted. Note this won't have anything to do with the WDT or sleep error. Only timing for PBP commands.

    If your programmers software doesn't allow you to read a .HEX file and make changes to config settings, then you can re-compile the loader firmware with the free version of C18.

    Or I can send you a modified version .HEX file if you need it.

    YESSSSS!

    With meProg of course you can change such things, of course. I have just used for "so long time" now only the bootloader so I forgot the meProg.

    Sleep works and NAP works also even that the manual does not confirm that.

    What USB board are you using?
    What is USB board (melabs U2?) or what ? or 4550? or...

  21. #21
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    What is USB board (melabs U2?) or what ? or 4550? or...
    Just curious if you were using a USB development board.

    Did you get it working?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  22. #22


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    Just curious if you were using a USB development board.

    Did you get it working?
    Yep ... it works THANK YOU Bruce

    Both SLEEP and NAP work on 18F4550 !!!! at least on my PIC18F4550 !

    The fault was in the bootloader: WDTPS was too big, 128 is ok

    Thank you all for your help !

    Would a USB development board be good for... what?

  23. #23
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Glad you got it working. It probably was before, but you had to wait a heck of a long time before it would wake up..;o}

    Would a USB development board be good for... what?
    Development boards, like say the Microchip USB dev board & meLabs USB dev boards are handy for testing things quickly without having to build your own boards or hand-wire stuff.

    Definitely not necessary, but handy if you're lazy like me..;o}

    Now that you have the sleep/nap/WDT stuff down pat, here's a fix for your interrupts.

    This isn't what you want;
    DEFINE INTERRUPT_ORG 1008h ' For Microchip USB Bootloader

    It looks like you're using the newer version USB loader, so your interrupts are re-mapped to locations 0x1008 for high priority, and 0x1018 for low priority.

    What you need to do with PBP to make sure it points to these re-mapped vectors goes like this;

    DEFINE RESET_ORG 1000h ' For Microchip USB Bootloader
    DEFINE INTHAND high_isr ' high-pri int vector
    DEFINE INTLHAND low_isr ' low-pri int vector

    Note that you don't need the DEFINE LOADER_USED 1.

    DEFINE RESET_ORG 1000h tells PBP to start compiling all code at 1000h.

    DEFINE INTHAND high_isr. When PBP see this it inserts a ORG RESET_ORG + 8 followed by a goto INTHAND, which is your high_isr routine.

    DEFINE INTLHAND low_isr. Now PBP inserts a ORG RESET_ORG + 18h followed by a goto INTLHAND, which is your low_isr routine.

    So you don't need to specifiy the address.

    Note that your interrupt routines will need to be assembler. Something like this;
    Code:
    asm
    high_isr
      ; do stuff here
      RETFIE FAST
    
    low_isr
      ; do stuff here
      RETFIE
    endasm
    Now your PBP .asm interrupts can work directly with the Microchip USB loader re-mapped int vectors.
    Last edited by Bruce; - 3rd December 2008 at 23:54.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  24. #24


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    Glad you got it working. It probably was before, but you had to wait a heck of a long time before it would wake up..;o}


    Development boards, like say the Microchip USB dev board & meLabs USB dev boards are handy for testing things quickly without having to build your own boards or hand-wire stuff.

    Definitely not necessary, but handy if you're lazy like me..;o}

    Now that you have the sleep/nap/WDT stuff down pat, here's a fix for your interrupts.

    This isn't what you want;
    DEFINE INTERRUPT_ORG 1008h ' For Microchip USB Bootloader

    It looks like you're using the newer version USB loader, so your interrupts are re-mapped to locations 0x1008 for high priority, and 0x1018 for low priority.

    What you need to do with PBP to make sure it points to these re-mapped vectors goes like this;

    DEFINE RESET_ORG 1000h ' For Microchip USB Bootloader
    DEFINE INTHAND high_isr ' high-pri int vector
    DEFINE INTLHAND low_isr ' low-pri int vector

    Note that you don't need the DEFINE LOADER_USED 1.

    DEFINE RESET_ORG 1000h tells PBP to start compiling all code at 1000h.

    DEFINE INTHAND high_isr. When PBP see this it inserts a ORG RESET_ORG + 8 followed by a goto INTHAND, which is your high_isr routine.

    DEFINE INTLHAND low_isr. Now PBP inserts a ORG RESET_ORG + 18h followed by a goto INTLHAND, which is your low_isr routine.

    So you don't need to specifiy the address.

    Note that your interrupt routines will need to be assembler. Something like this;
    Code:
    asm
    high_isr
      ; do stuff here
      RETFIE FAST
    
    low_isr
      ; do stuff here
      RETFIE
    endasm
    Now your PBP .asm interrupts can work directly with the Microchip USB loader re-mapped int vectors.
    Thank you Bruce, thank you indeed ... you are really a kind soul !

    By the way,
    can one find somewhere a list of DEFINEs that can be overwritten (changed afterwards whenever you want, when your program runs) and/or those that can not be changed afterwards, like the WTDPS and ... ?

    You "mail" was really educational and clear ... THANK YOU

  25. #25
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    DEFINEs cannot be changed at run-time. These are used by the compiler at compile time
    only.

    WDTPS options are set in the PIC configuration register. These you can change at run-time
    on some 18F parts.

    This thread shows two examples;
    http://www.picbasic.co.uk/forum/show...ghlight=config

    Just bear in mind that changing some configuration settings at run-time may result in major
    problems - so be careful.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  2. Battery powered applications
    By NavMicroSystems in forum Off Topic
    Replies: 7
    Last Post: - 22nd June 2009, 07:12
  3. 16F628A current high during sleep
    By Rubicon in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 9th October 2006, 10:21
  4. Wierd sleep issue
    By orca in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th March 2006, 22:06
  5. Replies: 5
    Last Post: - 5th February 2006, 16:51

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts