PDA

View Full Version : "SLEEP xx" command with LFINTOSC (31 kHz)



ybosco
- 15th May 2012, 18:03
I tried to use "SLEEP xx" command for PIC12F1822.

This controller uses 31 kHz LFINTOSC as a clock for WDT. And according to the datasheet the maximum prescaler number is 1:8388608, that is about 270 seconds.

But if I set "SLEEP 1" I get about 2 second sleep period. So if I set "SLEEP 21600" I get about 12 hour sleep period.

It's very useful but I don't understand why and don't understand how I can calibrate a sleep period. It's too long to wait for 12 hours to know if the period is shorter or longer.

Could somebody help with it?

Darrel Taylor
- 16th May 2012, 01:44
On the 16F1 "Enhanced Core" PIC's, the WDT powers up with a 2 second timeout (1:65536).
So 2 Seconds is the minimum default SLEEP Period.
When you specify 1 second, you'll get 2 instead.

If you specify 21600, you'll get 6 hours. (6hrs * 60mins * 60secs = 21600 seconds)
It will actually Wake-Up and go back to sleep 10800 times (every 2 seconds).

You can change the WDT prescaler to change the resolution, but then you have to scale the SLEEP value accordingly.

ybosco
- 16th May 2012, 06:12
Thanks!

Could you explain in more details how the command SLEEP works? Why 10800 times?
Because, 1/31 kHz * 65536 gives 2,1140645161290322580645161290323. And this number multiplied by 21600 gives more than 12.68 hours.

Could you explain also the same for PIC12F683? How about this one?

Darrel Taylor
- 16th May 2012, 18:39
When you say SLEEP 21600, you are specifying the number of seconds, not the number of WDT periods.
On 16F1's PBP simply divides the number you give it by 2 (21600 / 2 = 10800).

Times are approximate and uncalibrated.

On a 12F683, there are two prescalers.
The first one is controlled by WDTCON, with a power-on ratio of 1:512.
The second one is controlled by OPTION_REG, with a power-on ratio of 1:128.

Multiply them together to get the total prescaler ratio (512 * 128 = 65536).
So it's pretty much the same as the 12F1822, with the exception that PBP calculates the number of 2.3 second periods, even though the periods are 2.1 sec.

The reason for the difference is that most of the older PIC's like the 16F877A, have an 18ms WDT period with an OPTION_REG prescaler of 1:128. (0.018 * 128 = 2.304 sec.)
Since the 12F683 is really a standard 14-bit core, it is treated the same as the older chips.
It was not changed because the times are "approximate and uncalibrated.

In fact, if you look at the Electrical Characteristics for the WDT period on a 16F877A, it can be anywhere between 7 - 33 mS.
So the SLEEP command has never been intended to be "Accurate". It only gives an approximate time.

If you need "Accurate" timing while in sleep mode, you should use a 32768hz crystal on Timer1, and count the SLEEP/Wake-Up cycles manually.

ybosco
- 17th May 2012, 05:39
That's just I'd like to know. Thank you!

But I don't intend to use a crystal because of room and cost reasons. But I will make some sort of calibration. Now I know how it should be done.