Pages: [1] 2   Go Down
Author Topic: Triggering Interrupts with Real Time Clock  (Read 6715 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would I use a RTC (such as the DS1307) to trigger an interrupt? The overall goal is to put the processor to sleep and then wake it after an hour or so. I looked at using a watchdog timer, but it only goes up to 8 seconds and is reportedly unreliable.

Other info:
Data logging kit w/ RTC - http://www.adafruit.com/products/243
Fourm post that alludes to using an RTC as an interrupt - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1291420085
Watchdog tutorial - http://interface.khm.de/index.php/lab/experiments/sleep_watchdog_battery/
RTClib - https://github.com/adafruit/RTClib

To clarify, I understand how interrupts work; I just don't know how to implement one using the RTC. I don't get how the interrupt is supposed to trigger if the RTC has to be called to get the time (The code uses
Code:
DateTime now = RTC.now();
to get a snapshot of the current time).

tl;dr How do you trigger an interrupt using an RTC such as the DS1307?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26538
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I looked at using a watchdog timer, but it only goes up to 8 seconds and is reportedly unreliable.
I know about the eight second limit, but I've never heard of an unreliable watchdog - what would be the point?

Eight second periods can be stacked, one after the other.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 49
Posts: 1678
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some RTCs (e.g. the DS3234) have programmable alarms. You can set the alarm so that it fires when the current time matches a specified number of seconds,minutes,hours. So, for example, you could have the clock fire the alarm at 5 minutes and 32 seconds after every hour. The alarm itself is just a logic level on one of the pins. You hook that pin up to pin 2 or 3 on the Arduino and then you can attach an interrupt routine which will be called when the alarm goes off.

Pete
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does someone know if you could feed the square wave output of the DS1307 into one of the timer/counters and have it trigger an interrupt that will wake the processor?  It would be a simple matter to set to DS1307 to provide a 1 Hz pulse stream.  Then count the pulses.  I just don't know if the counter could then wake the processor.
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, one way or another but I don't see the point. I assume this is to save battery power. Sleeping for 8 seconds seems to me to save more power than sleeping for 1 second. This is because it takes time to wake (say, 65 mS). So you are waking up for 8 x 65 mS rather than 1 x 65 mS.

Your only real issue is knowing the time accurately, right? So use the watchdog, and when you wake, power up the RTC chip, read the time, and see if your hour is up. I describe this technique here:

http://www.gammon.com.au/forum/?id=11497
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your only real issue is knowing the time accurately, right? So use the watchdog, and when you wake, power up the RTC chip, read the time, and see if your hour is up.

The RTC chip has it's own power source (coin battery) so I think I'll just set an alarm and use that to trigger an interrupt. Thanks for the responses and I'll try to remember to post code here once I get it working.
Logged

Offline Offline
Edison Member
*
Karma: 49
Posts: 1678
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The RTC chip has it's own power source (coin battery) so I think I'll just set an alarm and use that to trigger an interrupt
I don't think the coin battery (3V) can be used to drive the clock output (TTL is nominally 5V). It only keeps the clock running when the normal supply voltage disappears.

Pete

 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The RTC chip has it's own power source (coin battery) so I think I'll just set an alarm and use that to trigger an interrupt
I don't think the coin battery (3V) can be used to drive the clock output (TTL is nominally 5V). It only keeps the clock running when the normal supply voltage disappears.

Pete

 

Hmmmm that's a good point. The problem right now is that I'm trying to write code while my parts are ordered. I think the best solution is to wake every 8 seconds and check the clock, as suggested by Nick Gammon.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4093
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Which RTC are you using?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which RTC are you using?

The DS1307 that is mounted on the Data Logging Shield from Adafruit: http://www.adafruit.com/products/243
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4093
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To address an earlier question, SQW/OUT on the DS1307 is open-drain, so with a pullup resistor to 5V it can drive an MCU input or other logic operating at 5V.

But the lowest frequency for SQW/OUT is 1Hz, so from a power saving standpoint, I'd favor the watchdog interrupt with an 8 sec period slightly. This would only be as accurate as the system clock, but judging from your initial post ("an hour or so"), maybe that's good enough and an external RTC wouldn't be needed.

Another approach would be to use the 1Hz output from the RTC to clock Timer1 or Timer2 externally, this would provide better accuracy than a resonator-controlled system clock. With Timer1 being 16 bits, the MCU could sleep continuously for an hour (actually could sleep for quite a bit longer). With Timer2, it would have to wake about every 4 minutes. But as with the watchdog, the wake periods can be extremely brief if there is nothing to do.

Yet another approach could dispense with the external RTC completely, clocking Timer2 from an external 32.768kHz crystal and implementing an RTC in software. This necessitates using the internal RC oscillator for the system clock, which can work very well for many situations.

Lots of ways to achieve the same thing!

Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4093
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The DS1307 that is mounted on the Data Logging Shield from Adafruit: http:

Ummmm. There's little point in sleeping the MCU on a standard Arduino board. The voltage regulator and other peripheral circuitry draw as much or more current than the ATmega328P and continue to do so even if the ATmega is sleeping.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My question was about feeding the 1Hz clock stream into one of the counters
Quote
It would be a simple matter to set to DS1307 to provide a 1 Hz pulse stream.  Then count the pulses.
The ideas was to get the processor to count the pulses in its sleep.  It could then wake-up on overflow or threshold.
So an 8-bit counter, like counter 2, would overflow every 256 seconds or 4 1/4 minutes.
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did some more reading and I found some more data but I don't know that it is more information

From the datasheet
Quote
17.3 External Clock Source
An external clock source applied to the T1/T0 pin can be used as Timer/Counter clock (clkT1/clkT0). The T1/T0 pin is sampled once every system clock cycle by the pin synchronization logic.
This makes is sound like you can not "Power Down" but you may be able to go into "Stand-By" where the clock keeps running.
T0 is used to clock the Timer 0 and since that is used for "delay()", "millis()" and such.  I don't think that is a good option.

Quote
14.3.3 Alternate Functions of Port D
PD5
        T1 (Timer/Counter 1 External Counter Input)
        OC0B (Timer/Counter0 Output Compare Match B Output)
        PCINT21 (Pin Change Interrupt 21)
I don't know which pin that is on the Arduino.
Quote
• T1/OC0B/PCINT21 – Port D, Bit 5
T1, Timer/Counter1 counter source.
OC0B, Output Compare Match output: The PD5 pin can serve as an external output for the Timer/Counter0 Compare Match B. The PD5 pin has to be configured as an output (DDD5 set (one)) to serve this function. The OC0B pin is also the output pin for the PWM mode timer function.
PCINT21: Pin Change Interrupt source 21. The PD5 pin can serve as an external interrupt source.

Quote
16.11.2 TCCR1B – Timer/Counter1 Control Register B
• Bit 2:0 – CS12:0: Clock Select
The three Clock Select bits select the clock source to be used by the Timer/Counter, see Figure 16-10 and Figure 16-11.
It looks like you can select the external clock just like you would select a prescaled clock.

Now, since Counter 1 is a 16 bit counter, you should be able to set a threshold of up to 65536 seconds or over 18 hours.

There may be a lot of reason this won't work.  I may be totally wrong in my reading of the datasheet, and I am still learning but I thought I would like learn why this wouldn't work.
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know which pin that is on the Arduino.

T1 is D5. I list them in here:

http://www.gammon.com.au/forum/?id=11504
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1] 2   Go Up
Jump to: