Menu

How to put 12F629 to sleep for 5 minutes

Help
2008-05-02
2013-05-30
  • Nobody/Anonymous

    Hi, I want to put my 12F629 to sleep for 5 minutes to save battery power. Any ideas how to do that ?

    ASM SLEEP just puts it to sleep. But I cannot set the time.

     
    • Nobody/Anonymous

      Have not done that personally, but reading Microchip AN580 http://ww1.microchip.com/downloads/en/AppNotes/00580c.pdf may give you some ideas.

      One scenario, per the APPNote, would be to set up the 12f629 with an external 32.768 khz crystal and use the Timer1 interrupt feature.  Setup the Timer1 prescaler along with the Timer1, peripheral, and general interrrupts.  Then start the Timer1 and put the pic to sleep.  In the interrupt service routine, clear the flags, and run a counter.  When the counter equals your delay have it run your code.

      Kent

       
    • Nobody/Anonymous

      picbasic has a function Sleep X 'Sleep for X minute

      See http://www.melabs.com/resources/pbcmanual/5/5-34.htm

      Would be nice if this can be done in GCBASIC (without an external crystal).

       
    • kent_twt4

      kent_twt4 - 2008-05-03

      Here is an example for putting a pic to sleep for approximately 30 seconds, awake from sleep, blink an led, then go back to sleep.  Hope that helps?

      Kent

      'This is a program to put a device to sleep and re-awake
      'using the WDT.  The loop instructions and calibration
      'not taken into effect (i.e. very rough timing results).

      #chip 12f675,4
      #config    WDT_ON, _INTRC_OSC_NOCLKOUT
      #define Led GPIO.5
      dir GPIO.5 out

      Main:
      SET LED ON
      Wait 1 sec
      SET LED OFF
      interval = 13  'interval is total wait in seconds divided by 2.3
      SleepTime(interval)
      goto Main

      sub SleepTime(interval as Word)
        GIE = 0  'clear the interrupt register so that the program may continue
        OPTION_REG = b'00001111'  'prescaler assigned to WDT with 1 to 128 prescaler
        count = 0
        backtosleep:
        clrwdt 'clear the wdt before sleep
        asm sleep
        count = count + 1
        If count < interval then goto backtosleep
      end sub

       
    • Nobody/Anonymous

      Kent, how accurate would it be if the interval in total would be max 2 hours. In the picbasic example the wdt timer is periodically calibrated against the internal clock. Also is there not a lot of energy wasted every 2.3 seconds it awakes ?  Thanks.

       
    • kent_twt4

      kent_twt4 - 2008-05-05

      Not very accurate, because its an internal rc circuit that is affected by temperature, number of instructions executed in the interrupt, and roundoff errors.  I noticed just after a few minutes that the wdt was off few seconds from the "calculated" value.  I would adjust the "interval" value to equal your time out value the best you can. 

      Unless the project is specified, its hard to tell what's the best way to approach the accuracy issue.  If precise timing is required, just don't think the wdt is the way to go, recalibration or not.  The wdt's are used for smoke alarms, white goods, and such, where accuracy is not an issue.

      Not much energy wasted making a handful of instructions.  Take a look at the low power features in the data sheet for making an energy budget.  Some of the newer pics (starting with the 12f683), have a 32khz internal osc.  With a post scaler, they can go to 268 seconds between wdt wake ups.

       
    • Nobody/Anonymous

      Precise timing for me is max 1-2% difference. I will have a look at the 32khz pics. Can you explain wat a pre/post scaler does ?

       
    • Nobody/Anonymous

      Hi Kent,

      It's a while ago but I haven't had much time for the project I am working on. I am looking for a person who can finish the programming. Do you know some one or can you do it for me. If so, maybe we can agree on a price.

      Thanks,

      Louis Banens

       
    • kent_twt4

      kent_twt4 - 2008-08-04

      Seems like all the ingredients have been put forth in recent posts on how to put a Pic to 'sleep', using 'on interrupt', and Hugh's excellent example on setting TIMR1 prescale and overflows.  Just a matter of mixing them up, and baking them for your 5 minute timeout project.

      I can always be contacted thru my signature email.

       
  • Anobium

    Anobium - 2011-12-26

    Kent,

    I am trying to get this work. The code is as the 4th post but I cannot get the device to wake up.  What is the issue?  Most grateful.

    #chip 12f675,4
    #config    WDT_ON, _INTRC_OSC_NOCLKOUT

    #define Led GPIO.2
    dir GPIO.2 out

    Main:
    SET LED ON
    Wait 1 sec
    SET LED OFF
    interval = 2  'interval is total wait in seconds divided by 2.3
    SleepTime(interval)
    goto Main

    sub SleepTime(interval as Word)
      GIE = 0  'clear the interrupt register so that the program may continue
      OPTION_REG = b'00001111'  'prescaler assigned to WDT with 1 to 128 prescaler
      count = 0
      backtosleep:
      clrwdt 'clear the wdt before sleep
      asm sleep
      count = count + 1
      If count < interval then goto backtosleep
    end sub

     
  • Anobium

    Anobium - 2011-12-27

    Kent,

    Apologies for the poor post.

    The code compiles and loads ok.  I have transferred the the pic to breadboard and following happens.

    LED on with a current load of approx 13ma then after 1 sec down to 0.13ma - the pic does not return from sleep - its seems (I have a scope and I can confirm the pic does not activate).

    Any thoughts?

     
  • kent_twt4

    kent_twt4 - 2011-12-27

    @evanvennn
    Not in a position to try the code.  I am not liking the assembly output though for the "If count < interval then goto backtosleep".  Try a "#define interval 2" in instead of interval = 2, and report back.  I kind of expect the same answer, so perhaps someone with access to a 12f675 can help.

     
  • Anobium

    Anobium - 2011-12-27

    #define fixed it!

    Sorry, I should have noticed when I copied the code from the site initially.

    Why does the compiler not pick up an error? :-)

    Working code.

    'This is a program to put a device to sleep and re-awake 
    'using the WDT.  The loop instructions and calibration
    'not taken into effect (i.e. very rough timing results).
    #chip 12f675,4
    #config WDT = ON
    #config Osc = int
    #define Led GPIO.1
    #define Led GPIO.2
    dir GPIO.2 out 
    Main:
    SET LED ON 
    Wait 1 sec 
    SET LED OFF
    #define interval = 2  
    'interval is total wait in seconds divided by 2.3
    SleepTime(interval)
    goto Main
    sub SleepTime(interval as Word)
      GIE = 0  'clear the interrupt register so that the program may continue
      OPTION_REG = b'00001111'  'prescaler assigned to WDT with 1 to 128 prescaler
      count = 0
      backtosleep:
      clrwdt 'clear the wdt before sleep 
      asm sleep
      count = count + 1
      If count < interval then goto backtosleep
    end sub
    
     
  • kent_twt4

    kent_twt4 - 2011-12-27

    This is an old post and since then, Hugh did a substantial rewrite about a year ago, and added integers to the mix too!  I haven't done a thorough investigation, but I suspect the "SYSCOMPLESSTHAN16" routine could use some work.

    By defining interval as a constant, GCBasic  skipped using the SYSCOMPLESSTHAN16 routine.  Another route would have been to change the subroutine header, which should now be "sub SleepTime(interval)".  You really shouldn't have to #define interval, but in this case, it helped diagnose a glitch.

    Feedback like this continues to make GCBasic a more complete compiler.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.