long countdown timer, how to save power?


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Jerson View Post
    I do not agree with using an RTC unless your project dictates it. The option of using the WDT(watchdog timer) to wake you from sleep, keep time and sleep again is the best. I use it in my battery powered security system. Use the Prescalar and WDT to decide at what interval you want to wake.

    Of course, you will need to rewrite your code if you have written the timing loops using pause.
    At the moment my code has an interrupt every 1 second using a timer and keeps track that way. I assume that the timer does not continue while in sleep mode, so when you say "wake you from sleep, keep time and sleep again", how does it keep time? Just by adding a constant to a timer every time it wakes up? How accurate would you think that would be over a longer period of time?

    I'm hoping to get an accuracy of +/- 1 min in 30 days.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    The Timer1 oscillator will continue running while the chip is in sleep mode.
    With a 32768hz crystal, you'll get an interrupt every 2 seconds. (More accurate than the main crystal)

    When the PIC wakes up (at the normal OSC speed), you simply update the time then go back to sleep. Batterys will last months.

    You don't actually need to use interrupts. If the GIE bit is clear, it will just resume where it left off, so it never leaves the main loop.

    DT

  3. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    The Timer1 oscillator will continue running while the chip is in sleep mode.
    With a 32768hz crystal, you'll get an interrupt every 2 seconds. (More accurate than the main crystal)

    When the PIC wakes up (at the normal OSC speed), you simply update the time then go back to sleep. Batterys will last months.

    You don't actually need to use interrupts. If the GIE bit is clear, it will just resume where it left off, so it never leaves the main loop.

    DT
    How would you keep track of the time without using interrupts?

    This is the way I was thinking:

    Timer1 with 32.768kHz OSC who's interrupt routine increments the time variables.

    main:
    [ code to check time variables, update LCD, etc ]
    NAP
    goto main

    And have the WDT off so that it only wakes up when the Timer 1 interrupt triggers.

    Does that sounds like it should work?
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kamikaze47 View Post
    Does that sounds like it should work?
    Yep, that's one good reason why Timer1 works the way it does.
    There's a good explanation on DS39605F-page 107 of the datasheet along with some assembly code on how it all works.

    EDIT: adding a thought... I don't know what would take more power to run and/or be more accurate (a trade off here)...
    A self powered RTC with it's own internal clock that you read once in a great while with a PIC that's sleeping most of the time......OR.....
    A PIC that's woken up X times per second (or less) to update a number internally and put back to sleep...
    Last edited by skimask; - 26th June 2008 at 17:40.

  5. #5
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Any idea what Darrel was talking about when he was saying it could be done without using an interrupt?
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kamikaze47 View Post
    Any idea what Darrel was talking about when he was saying it could be done without using an interrupt?
    I think what he was talking about is that you can use the WDT to wake up the PIC here and there and check the interrupt flag bits for Timer1 manually rather than have Timer1 provide the wakeup. Then reset the interrupt flag bits and go back to sleep.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I think something like this would work. (untested)
    Code:
    <font color="#000000"><b>TMR1IF  </b><font color="#008000"><b>VAR </b></font><b>PIR1</b>.<font color="#800000"><b>0
    
    </b></font><font color="#0000FF"><b><i>;Countdown from
    </i></b></font><b>Years     </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>0
    </b></font><b>Days      </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>30
    </b></font><b>Hours     </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>4
    </b></font><b>Minutes   </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>12
    
    </b></font><b>TheCount  </b><font color="#008000"><b>VAR WORD</b></font>[<font color="#800000"><b>2</b></font>]
    
    <font color="#008000"><b>ASM  
    </b></font><font color="#000080">TotalSeconds = _Years*31536000 + _Days*86400 + _Hours*3600 + _Minutes*60
        MOVE?CW TotalSeconds &amp;&amp; 0xFFFF, _TheCount
        MOVE?CW TotalSeconds &gt;&gt; 16, _TheCount + 2
    </font><font color="#008000"><b>ENDASM
    
    </b></font><b>T1CON </b>= <font color="#800000"><b>%00001011                  </b></font><font color="#0000FF"><b><i>; T1 OSC ON, Timer ON
    
    </i></b></font><b>Main</b>:
        <b>TMR1IF </b>= <font color="#800000"><b>0                     </b></font><font color="#0000FF"><b><i>; clear the Timers IF flag
        </i></b></font>@ <font color="#008000"><b>SLEEP
        </b></font>@ <b>NOP                        
        TheCount </b>= <b>TheCount </b>- <font color="#800000"><b>2        </b></font><font color="#0000FF"><b><i>; subtract 2 seconds
        </i></b></font><font color="#008000"><b>IF </b></font><b>TheCount </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>THEN           </b></font><font color="#0000FF"><b><i>; if low Word is 0
            </i></b></font><font color="#008000"><b>IF </b></font><b>TheCount</b>(<font color="#800000"><b>1</b></font>) = <font color="#800000"><b>0 </b></font><font color="#008000"><b>THEN    </b></font><font color="#0000FF"><b><i>;   check high Word
                </i></b></font><font color="#008000"><b>HIGH </b></font><b>PORTB</b>.<font color="#800000"><b>0           </b></font><font color="#0000FF"><b><i>;   count complete, turn a pin on
                </i></b></font><font color="#008000"><b>STOP
            ENDIF  
            </b></font><b>TheCount</b>(<font color="#800000"><b>1</b></font>) = <b>TheCount</b>(<font color="#800000"><b>1</b></font>) - <font color="#800000"><b>1  </b></font><font color="#0000FF"><b><i>; not finished dec high Word
        </i></b></font><font color="#008000"><b>ENDIF
    GOTO </b></font><b>Main
    </b>
    Make sure the WDT, VREF and anything that draws current is turned off.

    HTH,
    &nbsp; DT

Similar Threads

  1. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  2. Battery powered applications
    By NavMicroSystems in forum Off Topic
    Replies: 7
    Last Post: - 22nd June 2009, 07:12
  3. Replies: 5
    Last Post: - 24th February 2009, 18:55
  4. dual 7-segment countdown timer code
    By dr.ragh in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 30th April 2007, 13:19
  5. Long Timer
    By Tissy in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 1st November 2005, 17:24

Members who have read this thread : 0

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