View previous topic :: View next topic |
Author |
Message |
Axl Guest
|
Can a pic be used as an RTC while sleeping? |
Posted: Mon Jul 28, 2008 2:53 pm |
|
|
Can a PIC be used as a real-time clock (well I know it can) but where It can be put to sleep and maintain its count/data without drawing a whole lot of current from a watch battery? If so are there any examples/code. I could use an external RTC but I don't have any room on my PCB.
Thanks |
|
|
Heath
Joined: 21 Dec 2007 Posts: 41
|
|
Posted: Mon Jul 28, 2008 4:18 pm |
|
|
I believe on some of the PIC24s the RTCC (timer0) stays active. You could use that as a RTC. Check the documentation for your PIC. it will tell you what is active. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 28, 2008 4:42 pm |
|
|
This post has part of the information you will need. It shows how to
setup the interrupts to allow Timer1 to wake up the PIC from sleep,
when using an external 32.768 KHz watch crystal to run Timer1.
http://www.ccsinfo.com/forum/viewtopic.php?t=28158&start=8 |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Thu Mar 19, 2009 3:18 pm |
|
|
PCM programmer, on 18F4620 do i need to enable the peripheral interrupts in order to wake up from sleep?
Timer1 is clocked from external 32.768 KHz
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 19, 2009 4:53 pm |
|
|
No, because if you look at the interrupt logic diagram in the 18F4620
data sheet, it shows the gate for "Wakeup if in idle or sleep modes"
and that gate bypasses the PEIE bit. Look in the upper right corner
of this diagram:
Quote: | FIGURE 10-1: PIC18 INTERRUPT LOGIC |
The following test program worked (with vs. 4.089).
Code: | #include <18F4620.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//===============================
void main()
{
printf("Start\n\r");
delay_ms(3); // Allow last two chars to be sent
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_CLK_OUT);
set_timer1(0);
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
sleep();
delay_ms(2);
printf("Woke up from sleep\n\r");
while(1);
} |
-------------
Edit:
Changed the PROTECT fuse to NOPROTECT. There is no need for code
protection in a test program.
Last edited by PCM programmer on Sun Aug 16, 2009 12:21 pm; edited 1 time in total |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Fri Mar 20, 2009 1:48 am |
|
|
Thanks, PCM programmer.
My PIC needs to go to sleep (NOT IDLE) and the RTC should be still running while sleeping just becasue i have a software calendar running (Time & Date). So ideally the PIC will be interrupted and will wake up every second to update the calendar. I have the following TMR1 isr (dont worry about the variables):
Code: |
#int_TIMER1 //Real Time Clock
void TIMER1_isr()
{
//bit_clear(T1CON,7);
Bit_Set(TMR1H,7);
//Bit_Set(T1CON,7);
Sflg=true;
IncrementTime();
if (!(--TTL))
{
CurrentMode = SLEEP_MODE;
}
}
|
T1CON,7 is always 0
I have noticed that the PIC is getting interrupted even when sleeping but i'm not sure if it's every second since my time is a bit slower than my hand watch. I have lost 30 mins during 8 hours .. so I have a drift.
Could this be due to sleep mode?
Please note that when the micro is up and running i'm able to see the Time/Date on LCD and the seconds update seems synchronized with my hand watch (i know this is not a way to test it)
Thanks |
|
|
lsimaster
Joined: 19 Mar 2009 Posts: 25
|
timer accuracy |
Posted: Fri Mar 20, 2009 8:03 am |
|
|
The internal oscillator is not as accurate as an external crystal as it is based on a RC time constant. Most of the parts have the ability to calibrate this but that is another technical problem. Also you have to consider the power consumption relative to your backup strategy. In my experience none of this works out if you need accuracy or must maintain time in the absence of power. A part like the DS1302 is far superior in this respect as it will maintain accuracy with the proper crystal circuit and will run for a year or more with a supercap or lithium battery backup. |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Fri Mar 20, 2009 8:25 am |
|
|
Thanks lsimaster, but Timer 1 is clocked from and external XT: 32.768 KHz
Thanks again! |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Mar 20, 2009 9:54 am |
|
|
Running the RTC from 32kHz crystal involves SEC_IDLE rather than SLEEP mode, see the Power-Managed Modes chapter in device datssheet.
In sleep mode, all oscillators are stopped. Generally, the power consumption of a PIC even in SLEEP mode is considerably higher than a usual RTC chip as DS1307, this may be reason enough not to use these PIC internal RTC options. It's the same with PIC24 devices, they have a dedicated RTC unit but no separate power supply, you can't run it from a lithium battery for 10 or 20 years as you can with DS1307. |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
|
Posted: Fri Mar 20, 2009 10:00 am |
|
|
Thanks FvM,
The CPU is clocked from internal OSC: 8MHz and not from TMR1 OSC.
TMR1 is clocked from external XT (32.768KHz). I need to keep the 1 second interrupt running even in sleep mode since i have a calendar that needs to be updated!
Thanks again. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Mar 20, 2009 10:09 am |
|
|
A small sidestep, but why are so many people using the DS1307 or DS1305? These chips are more expensive (€3.05) than newer alternatives. Some examples:
-Seiko S35390A (0.25uA @ 3V)
- NXP PCF8563 (0.5uA @ 3V, I2C, €1.67)
- TI M41T81 (0.6uA @ 3V, I2C, many options, €1.60
- TI M41T81S is identical but has integrated crystal, €2.03
- ST Microelectronics M41T56C64 (0.45uA, I2C, internal crystal, 8k EEPROM)
|
|
|
Ttelmah Guest
|
|
Posted: Fri Mar 20, 2009 10:37 am |
|
|
It is perhaps surprising how long you can go.
I have a unit based on the 16F628A, which runs a basic RTC (waking once every two seconds for the 'tick'), and a series of more 'power hungry' functions, run for a few seconds a day.
Some comments apply:
Don't use an interrupt handler. For my 'tick', I want the chip awake as little as possible. My code involves less than 30 cycles 'awake'. Using an interrupt handler, _tripled_ this.
My unit, draws just fractionally under 2uA 'asleep', and 500uA awake in the clock handler. It wakes normally for an average of 15uSec/sec, for the clock. The 'power hungry' part, is a total of just over ten seconds a day (worst case 30), at just over 2mA. The total 'average', works out at just under 4uA. I run this off a 500mA Lithium cell, and originally specified battery replacement at a 5 year interval. However we have had four of the units running 'in parallel', on one battery, 'on test', and these are still running, 6 years after the original build.
Generally:
Time 'awake', is the real killer.
To get PIC's down to their specified consumption, you _must_ remember to not allow any pins to float.
No matter how low the consumption of the chip, it is usually other things that drink the most. Voltage regulators, other peripherals etc.. You need to turn these off, and think about the implications of doing so (lines floating etc.).
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Mar 20, 2009 11:46 am |
|
|
DS1307 is also < 1.50 € in small quantities, but it's good to mention alternatives. In this discussion DS1307 may be considered as a placeholder.
I didn't expect an external crystal oscillator, cause the effort isn't lower than for a RTC chip to my opinion, but there may be specific reasons to use it. It's not easy to compete with RTC chip's low supply current, however.
An exactly timed wakeup can be easily provided by an RTC chip with interrupt output. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 20, 2009 11:58 am |
|
|
Quote: | I have lost 30 mins during 8 hours .. so I have a drift. |
Post a test program that shows the problem. Make it just as short as
possible. No unnecessary lines of code at all. But it must be complete
and compilable. (No need to post the LCD driver). |
|
|
PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
[RESOLVED] |
Posted: Sat Mar 21, 2009 1:03 am |
|
|
FIRST, Thanks to all
The drift problem is solved now. Timer1 OSC was configured to run in low power mode (LPT1OSC). That was causing a random drift when the CPU is in sleep mode. Now it's configured in high power mode (NOLPT1OSC). It was running overnight and it seems OK till now.
Please note that my whole system is battery operated (3V lithium). The final product needs to be as cheap as possible that's why I didn't go for external RTCs like DS1307 (which I'm so familiar with).
In case the high power mode of TMR1 OSC will be consuming much more current I can switch to 2x AAA bats. (I'll be checking this issue next)
Thanks again |
|
|
|