Nap and sleep commands


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Default Nap and sleep commands

    I have a project that is running a 16f689 off batteries, therefore iam trying to keep battery consumption to and absolute minimum. Using internal osc @ 8 mhz

    i am currently using PBP sleep comand to put the micro to sleep and then wake up either after an interupt or the sleep period expiring. this works well and just as i expect. Trying to minimise my current consumption further i need to do another function that basicly flashes a led on for 50ms then off again aprox every second.

    I tried the code using "sleep 1" i.e flash the led then sleep for 1 second, however the granularity of the sleep command means my led flashes once every 2.3 seconds. to bring this time down closer to the 1 second mark i have tried to substitute "sleep 1" with "Nap 6" . This is where my problems begin. with the nap command the interupts or the nap time seem to go haywire, as the program wont come out of nap.. I have read that the period of nap sets the prescaler for the WDT , do i need to change some osc settings to get this to work correctly ? Is there a way to make sleep - sleep for less than 1 sec, time dosent need to be exact just less than what it is now.. thats what i thought Nap would do...

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


    Did you find this post helpful? Yes | No

    Default

    This was about a different chip but it should help with NAPing.

    http://www.picbasic.co.uk/forum/show...t=10203&page=1
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    The watchdog timer runs on the internal 31kHz oscillator so it doesn't matter what oscillator speed the CPU runs at.

    This PIC also has a WDTCON register you use to set the WDT period with. Take 1/31kHz (0.000032258) and multiply this times the prescaler value in WDTCON for the WDT time period.

    If you insert WDTCON = %00010101 this sets bit 0, which enables the WDT, and loads a prescale vaule of 1:32768. This gives you 32768 * 0.000032258 for about a 1.057 second wakeup period.

    This PIC allows you to set WDT to off in config, and turn it on/off using bit 0 in WDTCON, so you get even better power savings. I.E. you can enable the WDT only when you need it, then disable it when you don't to save power.

    This gives you much better power savings than using the PBP SLEEP command since it sleeps for the whole period, it doesn't wakeup & loop until the timeout period expires, and you only need to enable WDT just before you enter sleep mode.

    Here's an example:
    Code:
    @ __CONFIG _FCMEN_OFF & _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF
     
    DEFINE OSC 8
    DEFINE NO_CLRWDT 1   ' PBP doesn't clear WDT automatically
     
    LED VAR PORTB.6      ' LED on RB6
    LED = 0              ' LED off at POR
     
    TRISB.6 = 0          ' Make pin an output
    OPTION_REG = %10001000 ' Pull-ups off, 1:1 prescaler to WDT
    OSCCON = %01110000     ' 8MHz internal osc
     
    Main:
      HIGH LED           ' Turn on LED
      PAUSE 50           ' LED on for ~50mS
      LOW LED            ' LED off
      CLEARWDT
      WDTCON = %00010101 ' WDT enabled with a period of 32768 * (1/31kHz)
      @ SLEEP            ' for a sleep period of about 1.057 seconds
      @ NOP              ' Execute a NOP on WDT or interrupt wakeup
      WDTCON = 0         ' Disable WDT when not needed for power savings.
                         ' WDT is only turned on just before entering sleep
      GOTO Main          ' Loop forever
     
      END
    With brown-out reset and power-up timer disabled you save even more power, but check to make sure your application works properly with both these disabled.
    Last edited by Bruce; - 6th August 2010 at 17:13.
    Regards,

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

  4. #4
    Join Date
    Aug 2010
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce ,
    with a little playing around got that to work the way i need it to. The only thing to do now then is that this program has 2 "sleep" modes .
    1 that sleeps for 1 second which using code Bruce helped with works fine, but i had the other sleep mode which runns for around 8 hrs using a PBP "Sleep 28800" i.e sleep for 28800 seconds = 8 hrs . Obviously i am trying to save as much power during this sleep as possible, so rather than use the PBP sleep command is there a more efficent way of doing this long sleep using ASM Sleep, do i have to set my watchdog prescaler for as big as it goes and count how many times it wakes up ? i belive the largest prescale time i can set is 65536 which would equate to 2.11 seconds , dosent seem logical to have it wake up every 2 seconds advance a counter then sleep again..
    Am i better off just using the PBP sleep ? or am i missing something ?

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


    Did you find this post helpful? Yes | No

    Default

    i need to do another function that basicly flashes a led on for 50ms then off again aprox every second.
    This is what the code example above does.

    If you need it to sleep longer just set the prescaler for the watchdog timeout period in WDTCON to 65536, then set the 2nd prescaler in OPTION_REG to 1:128.

    Then it will sleep a lot longer, but it's not going to be able to blink your LED every 1 second. It will be sleeping for the entire 270 second period. If you need even longer, then you could let it sleep for ~270 seconds, wakeup, increment a variable, then go back to sleep.
    Regards,

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

  6. #6
    Join Date
    Jun 2005
    Location
    Surrey, England
    Posts
    35


    Did you find this post helpful? Yes | No

    Default Similar issue on 18F4550

    I am also trying to minimise power consumption on a 18F4550 which connects to a PC via USB, but for >24 hours I need it to sample the ADC and store to external I2C memory with lowest current on battery only (USB disconnected)- present setup uses 42 mA and only lasts 9 hours - not enough - I have tried to adapt your answer for the 18F4550:
    Code:
    Development:
    DEFINE NO_CLRWDT 1
                lcdout $FE, $C0, "USB Low Power   "
                
                T0CON.7 = 0     ' Disable USBservice interrupts TMR1 still running @ 25 Hz
    '            UCON.1 = 1     'UCON not defined in PBP 2.46
    @	clrf	UCON			; Disable USB & detach from bus
    @	clrf	UIE  			; Mask all USB interrupts
    @	movlw	DETACHED_STATE
    @	movwf	usb_device_state
    
    lowpower:   IF dSecs = 1 THEN  
                    dSecs = 0
                    lcdout $FE, $02, "Time ", dec2 Hours, ":", dec2 Minutes, ":", dec2 Seconds, "   "
                ENDIF
                ' Do all the other stuff here (read ADC, store to Mem) 
                CLEARWDT
    'CONFIG2H = %00011111 ' WDT enabled with a period of 32768 * (1/31kHz) - PO default - cant access easily
                WDTCON = 1
    @ SLEEP            ' for a sleep period of about 1.057 seconds
    @ NOP              ' Execute a NOP on WDT or interrupt wakeup
                WDTCON = 0         ' Disable WDT when not needed for power savings.
                goto lowpower
    But no joy - sleeps but never wakes up!!!
    I am (sadly) using PBP 2.46 (waiting for 2.60 to arrive) and am using DT_INTS with a 100 mSec interrupt to keep USBservice'd (on TMR0, turned off here) and a 40 mSec interrupt on TMR1 to update the clock (which I need))
    I have tried using TMR1 to wake from sleep but no joy there either. OSC 48

    Any help would seriously help my follicular challenge! TIA
    Peter
    ps I am running TMR1 at 25 Hz - the slowest I can go on the 18F4550 as max prescaler is 1:8 - Is this interfering with the WDT - I have never really got to grips with the WDT and I have read the manual but cant make head nor tail of it!

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