Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC SLeep function

Status
Not open for further replies.

2camjohn

Member
Hi

I am considering using the sleep function of a PIC12f629 to save power during a period of user inactivity in a battery powered appliance.

My problem is I need to know how long the device has been left inactive, which would obviously involve using a timer to count this time period.



Is it possible to use the sleep function of the pic and at the same time keep a record of how long the pic has been 'asleep'?

If not is there any other way of measuring this sleep period without using alot of power?

Thanks
John
 
You can leave the internal oscillator running to wake the PIC at regular intervals to do counting, but it is not a very accurate measure of time since its freq varies with temp. You can put a 3.2768 kHz watch crystal as the second oscillator and use its interrupts to wake the PIC which is highly accurate. These do consume more power than just plain sleep.

You can get an external RTC- realtime clock- device. Usually they can be programmed to provide an external interrupt to the PIC at a particular interval.
 
2camjohn said:
How inaccurate is leaving the oscillator running?
If i wanted to increment at ten minute intervals, and if it incremented between 8 and 12 minutes then that would be fine.

Read the spec for your particular part. It is probably fine for that degree of accuracy provided the temp doesn't vary to extremes. Calibrating for that particular part's speed may also increase accuracy.
 
2camjohn said:
If i wanted to increment at ten minute intervals, and if it incremented between 8 and 12 minutes then that would be fine.

As already suggested, the watchdog timer isn't very accurate, but it should be OK for that degree of accuracy.

You will need to implement a counter to count the number of watchdog 'wakeups', as the maximum period is quite small, but this is simple to do.
 
2camjohn said:
Ill get the WDT working and test the accuracy myself.

If you want some sample code, let me know - I've got some code I wrote for a 16LF628 using the watchdog timer to periodically transmit an IR code. It's a 'red dot' remover for Sky Digital.
 
2camjohn said:
Thanks for the help fellas.

Ill get the WDT working and test the accuracy myself.

Cheers

I think the terminology we're using is wrong. You need the internal osc, not the WDT. The internal osc can be configured to create an interrupt periodically which can wake the PIC from sleep. The WDT is an optional error recovery feature which will reset (not interrupt) the PIC if its counter is not cleared by software periodically. It's related in that the WDT's counter is driven by the internal osc, but you don't use the WDT to wake from sleep.
 
Oznog said:
2camjohn said:
Thanks for the help fellas.

Ill get the WDT working and test the accuracy myself.

Cheers

I think the terminology we're using is wrong. You need the internal osc, not the WDT. The internal osc can be configured to create an interrupt periodically which can wake the PIC from sleep. The WDT is an optional error recovery feature which will reset (not interrupt) the PIC if its counter is not cleared by software periodically. It's related in that the WDT's counter is driven by the internal osc, but you don't use the WDT to wake from sleep.
I disagree, I've used the watchdog to wake up from sleep periodically, and it works fine. I've used it in a commercial product that has sold more than 10,000 units, so I guess it works OK. I don't know what PIC your talking about, but the watchdog I use does NOT run from the internal clock, it is a separate RC oscillator, that's why it's so temperature and voltage dependent.
 
Oznog said:
I think the terminology we're using is wrong. You need the internal osc, not the WDT. The internal osc can be configured to create an interrupt periodically which can wake the PIC from sleep. The WDT is an optional error recovery feature which will reset (not interrupt) the PIC if its counter is not cleared by software periodically. It's related in that the WDT's counter is driven by the internal osc, but you don't use the WDT to wake from sleep.

It's you that's wrong Oznog, in sleep mode the internal oscillator is shut down, so it can't generate an interrupt as it's not running. However, in sleep mode the WDT still runs, it's a totally seperate RC oscillator. It's common practice to use the WDT in this way, you can also wake from sleep using 'interrupt on change on PortB', this is commonly used for keyboard applications.

The WDT also works differently on different processors, on some it generates a reset, and you check for it in your reset routines, on others the program continues from where the sleep command was issued - the 16F628 does this.
 
Nigel Goodwin said:
2camjohn said:
Ill get the WDT working and test the accuracy myself.

If you want some sample code, let me know - I've got some code I wrote for a 16LF628 using the watchdog timer to periodically transmit an IR code. It's a 'red dot' remover for Sky Digital.


Thanks I would appreciate that alot.

Although I am using a HLL it seems it does not support SLEEP very well, so I will have to write this bit in assembler if i end up keeping it.


Thanks
John
 
2camjohn said:
Thanks I would appreciate that alot.

Although I am using a HLL it seems it does not support SLEEP very well, so I will have to write this bit in assembler if i end up keeping it.

OK, here's the code I was using.
 

Attachments

  • skyrem.zip
    1.9 KB · Views: 124
If I understand your code correctly you sleep the pic at every opportunity and it wakes by the buttons or WDT timer in order to transmit.

I think I can easily integrate that with my code, except with mine the sleep will only happen after a few minutes of inactivity.



What I still dont understand is how to stop the WDT from generating a reset if the WDT times out while the PIC is still awake.

This is quite likely with my device and I dont want it to RESET every few seconds!!!
 
2camjohn said:
If I understand your code correctly you sleep the pic at every opportunity and it wakes by the buttons or WDT timer in order to transmit.

Just from the WDT, it checks a DIL switch every time it transmitts, and resets the number of WDT events from the DIL switch reading.

If you've got Sky Digital you've probably noticed the annoying 'red dot' in the top right corner of the screen, you can press 'backup' to get rid of it. This code simply sends a 'backup' code periodically, you can set the number of WDT events it takes with the DIL switch.

I think I can easily integrate that with my code, except with mine the sleep will only happen after a few minutes of inactivity.

What I still dont understand is how to stop the WDT from generating a reset if the WDT times out while the PIC is still awake.

This is quite likely with my device and I dont want it to RESET every few seconds!!!

That's the whole point of the WDT, it's to reset your code if it locks up, you need to add a 'clrwdt' instruction periodically in your code, which resets the WDT and prevents it timing out. If the code gets locked in an endless loop it will then reset when the WDT times out, obviously you shouldn't have a 'clrwdt' inside such a loop.
 
2camjohn said:
It makes sense now you put it like that.


I just need to clear the WDT every time my timer does something(which is definately more frequent than the WDT at full prescalar), that way sleeping the pic will stop the timer and let the WDT take over.

Does that sound correct?

Yes, it does.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top