Can anyone tell me why nap & sleep won't work with the 18F452?


Closed Thread
Results 1 to 6 of 6
  1. #1
    jessey's Avatar
    jessey Guest

    Default Can anyone tell me why nap & sleep won't work with the 18F452?

    Hello Again,

    I'm trying to switch over another program I wrote for a 16F877 to an 18F452. It's a moisture meter circuit that runs on batteries. After compiling for the 452 then running the program, the program was running ever so slowly and locking up. After commenting out 95% of the program I got it down to a few counters and displaying them to the Lcd, then I noticed that when I changed the NAP & SLEEP commands to a PAUSE, then it worked good and the programs speed was normal. I've spent more than a few hours going through the archives but couldn't find any discussions about it. Can anyone here explain what's going on with using NAP's and SLEEP commands with the 18F452's? I've gone through the 452 manual as well and can't find any reference that say's these commands won't work for the 452.

    I did find an interesting read about using the LP oscillator mode to save power while in sleep and will try and absorb that information in the next little while but in the meantime can anyone explain to me why the nap & pause commands won't work as expected?

    Thanks
    jessey

  2. #2
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi Jessey,
    I don't know if this is the same for the 18F452, but here is some info I had saved from a while back:

    ====


    QUOTE:
    The PBP default WDT postscaler is set to 128 in the 18F1220.INC device family header file.

    __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H

    The 18F1220 nominal WDT timeout period is ~4mS. If you accept the default postcaler value of 128 this gives you roughly 4mS * 128 = 512mS. That's why you're seeing roughly 1/4 the sleep period when switching from 16F to 18F.

    PBP can't change the WDT postscaler on the 18F since this is set in the config word. To make sleep work roughly the same on the 18F, just set the WDT postscaler to 512 which gives you 512 * ~4mS = ~2.048S nominal WDT timeout period.

    Set your WDT postscaler to 512.

    __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H

    The 16F WDT timeout period is 18mS. PBP sets OPTION_REG for prescaler assignment to WDT with a rate of 128. This gives ~18mS * 128 = 2.304S.

    With _WDTPS_512_2H this will get you pretty close to the same sleep time periods with the 18F part. If you need to increase this time period, just increase the postscaler.

  3. #3
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default It would be nice if I could just select 512 for the postscaler.

    Thanks Arch,

    I looked in the manual and it says in section 19.2.2 under WTD POSTSCALER:

    The WDT has a postscaler that can extend the WDT Reset period. The postscaler is selected at the time of the device programming, by the value written to the CONFIG2H configuration register. Below are the settings that I have pasted into my program and 128 is the highest setting that I can set for the postscaler.

    @ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    ;Configuration Byte 2H Options
    '_WDT_ON_2H EQU H'FF' ; Watch Dog Timer enable
    '_WDT_OFF_2H EQU H'FE'
    '_WDTPS_128_2H EQU H'FF' ; Watch Dog Timer PostScaler count
    '_WDTPS_64_2H EQU H'FD'
    '_WDTPS_32_2H EQU H'FB'
    '_WDTPS_16_2H EQU H'F9'
    '_WDTPS_8_2H EQU H'F7'
    '_WDTPS_4_2H EQU H'F5'
    '_WDTPS_2_2H EQU H'F3'
    '_WDTPS_1_2H EQU H'F1'

    I don't quite understand the manual and really hope there is a work around for this. With the 16F877 I got the current draw down to an average of about 0.185 milliamps in sleep which I thought was pretty good. It would be nice to see what others have done with the 18F452's but so far I couldn't find any discussions about doing this. Maybe I should concider using a diferent chip that's better suited for battery operation. Do you have any ideas?

    Thanks
    jessey

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


    Did you find this post helpful? Yes | No

    Default

    SLEEP should work fine on the 18F452. The WDT on this one has a typical time-out period of 18mS without the postcaler.

    With the postscaler set to 128 you have 128 * 18mS for ~2.304S before wake-up from SLEEP.

    It will definitely lock-up if you use SLEEP or NAP, and WDT isn't enabled, or you're not setting WDTCON.SWDTEN.

    Using NAP with the 18F, the Period argument isn't used, so it just sleeps for whatever the typical WDT time-out period is * the postscaler.

    With the postscaler set to 128, SLEEP 4 should give you roughly 4.6S.

    Here are a few that I've tested on the 18F452.
    Code:
    ' Config fuse settings in PBP 18f452.INC 
    '  __CONFIG    _CONFIG1H, _OSCS_OFF_1H & _XT_OSC_1H
    '  __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    '  __CONFIG    _CONFIG4L, _LVP_OFF_4L
            
        DEFINE OSC 4
        X VAR BYTE
        
        PORTD = 1
        TRISD = 0
    
    Main:  ' Use one at a time to see the difference
        GOTO SL1
        'GOTO SL2
        'GOTO SL3
        'GOTO SL4
    
    SL1:   ' Gives roughly 4.6 seconds / compiles to 130 bytes
        PORTD = PORTD ^ 1   ' Toggle D0
        SLEEP 4             ' ~4.6 seconds sleep
        GOTO SL1
        
    SL2:   ' Gives roughly 4.6 seconds / compiles to 60 bytes
        PORTD = PORTD ^ 1   ' Toggle D0
        X = 2       ' 2 x ~2.3 seconds = ~4.6 seconds sleep
        WHILE X
         NAP 1
         X = X - 1
        WEND
        GOTO SL2
        
    SL3:    ' Gives roughly 4.6 seconds / compiles to 46 bytes
        PORTD = PORTD ^ 1   ' Toggle D0
        X = 2       ' 2 x ~2.3 seconds = ~4.6 seconds sleep
        WHILE X
         @ SLEEP
         X = X - 1
        WEND
        GOTO SL3
    
       ' Gives roughly 4.6 seconds / compiles to 38 bytes
    ASM
    SL4
        MOVLW 1
        XORWF LATD,F
        MOVLW 2       ; 2 x ~2.3 seconds = ~4.6 seconds sleep
        SLEEP
        DECFSZ WREG,F
        GOTO $-4
        GOTO SL4
    ENDASM
        
        End
    Last edited by Bruce; - 4th February 2006 at 20:50.
    Regards,

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

  5. #5
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce,

    I think I understand what was happening now. After implementing your code example I found that the NAP's were giving me the same pause period as the sleep command. I was using a bunch of nap's in my mainloop and that's why my program slowed down so much. I do need shorter delays in sleep than the 2.3 seconds @ 128 so I guess I'll have to lower the WTD timer postscaler to the smallest delay that I anticipate using then use multiple commands for the required longer delays that I need. I hope that doesn't increase my current draw but I guess I'll know soon enough.

    I changed the sleep times just to see what I'd get and I found that a setting of sleep 5 would give me approximately 6 seconds and then sleep 6 would give the same results. Then sleep 7 gave me a pause time of approx. 8 seconds and sleep 8 & 9 yielded the same. Is there any rhythm or rime to that? I guess I'll have to check the times for whatever values I'm using to be sure. Also I read somewhere that the sleep times will change with temperature as well.

    I've never seen code like your While X Wend code, I compiled a For..Next to compare and the for..next uses 2 bytes more to compile, is that why you used it? Another question, why did you use PORTD = PORTD ^ 1 in that code snip? I couldn't understand what it was doing in there and also if you could tell me, what does PORTD = 1 and TRISD = 0 do in pbp code? I'll be interested to hear your reply on that.

    Thanks Again
    jessey

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


    Did you find this post helpful? Yes | No

    Default

    Hi Jessey,

    Also I read somewhere that the sleep times will change with temperature as well.
    SLEEP uses the PIC WDT (watchdog timer) that's clocked by an internal RC oscillator. RC oscillators are affected by temperature, so timing can/will vary like the wind depending on temperature.

    Look in the Electrical Characteristics section of the data sheet (Table 22-7 in mine for the 18F452). You'll notice the WDT time-out period can be anywhere from 7mS min to 33mS max, so you should never expect precise time periods when using the WDT.

    I've never seen code like your While X Wend code, I compiled a For..Next to compare and the for..next uses 2 bytes more to compile, is that why you used it?
    Not really. I just used it as a quick example. There are several ways to get similar results. I just prefer using WHILE..WEND.

    Another question, why did you use PORTD = PORTD ^ 1 in that code snip? I couldn't understand what it was doing in there and also if you could tell me, what does PORTD = 1 and TRISD = 0 do in pbp code?
    I used PORTD because I used a LAB X1 board for the test, and it has an LED bargraph on PORTD.

    PORTD = 1 is the sames as PORTD = %00000001. This just writes a 1 to the port to control the LED on PORTD.0.

    TRISD = 0 simply makes all of the PORTD pins outputs, but all this stuff is covered in the PIC data sheet.

    PORTD = PORTD ^ 1 is Exclusive ORing PORTD with 1 & toggling the LED on & off. The LED was used for a visual timing indicator.
    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. Sleep & nap
    By keymuu in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 4th December 2008, 18:45
  3. Char. LCD and 18F452 on 20 MHz not work
    By samettin in forum General
    Replies: 11
    Last Post: - 28th July 2008, 09:59
  4. How do I get DATA @ to work with a 18F452?
    By jessey in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 2nd February 2006, 10:35
  5. Why doesn't my code for 18f452 work on 18f252?
    By senojlr in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd December 2005, 02:42

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