Skip to content

Commit 837d684

Browse files
author
simon
committed
Adding examples for setting a timer, and for monitoring the INT pin.
1 parent 1b3c750 commit 837d684

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Sets a timer for 10 seconds, and watches a pin (MONITOR_PIN) for
3+
* its interrupt signal.
4+
*
5+
* The RTC's INT line is pulled down when the timer goes off and the
6+
* interrupt is active.
7+
*/
8+
#include <Wire.h>
9+
#include "RTClib.h"
10+
11+
#define MONITOR_PIN 5
12+
13+
RTC_PCF8523 rtc;
14+
Pcf8523TimerState state;
15+
16+
void setup () {
17+
Serial.begin(9600);
18+
if (! rtc.begin()) {
19+
Serial.println("Couldn't find RTC");
20+
while (1);
21+
}
22+
23+
pinMode(MONITOR_PIN, INPUT_PULLUP);
24+
25+
/*
26+
struct type signatures:
27+
28+
typedef struct {
29+
bool irupt_flag; // whether the timer has gone off
30+
bool irupt_enabled; // whether the flag state is tied to the interrupt pin state
31+
} Pcf8523IruptState;
32+
33+
typedef struct {
34+
bool enabled; // whether the timer is running
35+
uint8_t value; // the current value of the timer
36+
Pcf8523FrequencyDivision freq; // the clock divider used
37+
Pcf8523IruptState irupt_state; // the timer's interrupt state
38+
} Pcf8523TimerState;
39+
*/
40+
41+
state.enabled = true;
42+
state.value = 10;
43+
state.freq = PCF8523_Freq_second;
44+
state.irupt_state.irupt_flag = false;
45+
state.irupt_state.irupt_enabled = true;
46+
47+
rtc.write_timer(PCF8523_Timer_Countdown_B, &state);
48+
}
49+
50+
void loop () {
51+
rtc.read_timer(PCF8523_Timer_Countdown_B, &state);
52+
Serial.print("timer value: ");
53+
Serial.print(state.value, DEC);
54+
Serial.print(", enabled: ");
55+
Serial.print(state.enabled ? "yes": "no");
56+
Serial.print(", freq: ");
57+
Serial.print(state.freq, DEC);
58+
Serial.println();
59+
Serial.print("irupt flag: ");
60+
Serial.print(state.irupt_state.irupt_flag, DEC);
61+
Serial.print(", enabled: ");
62+
Serial.print(state.irupt_state.irupt_enabled, DEC);
63+
Serial.println();
64+
65+
Serial.print("Interrupt pin: ");
66+
Serial.println(digitalRead(MONITOR_PIN) ? "HIGH": "LOW");
67+
68+
Serial.println();
69+
delay(1000);
70+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Sets a timer for 10 seconds on start, and monitors the status/flag
3+
* values in a loop.
4+
*/
5+
#include <Wire.h>
6+
#include "RTClib.h"
7+
8+
RTC_PCF8523 rtc;
9+
Pcf8523TimerState state;
10+
11+
void setup () {
12+
Serial.begin(9600);
13+
if (! rtc.begin()) {
14+
Serial.println("Couldn't find RTC");
15+
while (1);
16+
}
17+
18+
/*
19+
struct type signatures:
20+
21+
typedef struct {
22+
bool irupt_flag; // whether the timer has gone off
23+
bool irupt_enabled; // whether the flag state is tied to the interrupt pin state
24+
} Pcf8523IruptState;
25+
26+
typedef struct {
27+
bool enabled; // whether the timer is running
28+
uint8_t value; // the current value of the timer
29+
Pcf8523FrequencyDivision freq; // the clock divider used
30+
Pcf8523IruptState irupt_state; // the timer's interrupt state
31+
} Pcf8523TimerState;
32+
*/
33+
34+
state.enabled = true;
35+
state.value = 10;
36+
state.freq = PCF8523_Freq_second;
37+
state.irupt_state.irupt_flag = false;
38+
state.irupt_state.irupt_enabled = true;
39+
40+
rtc.write_timer(PCF8523_Timer_Countdown_A, &state);
41+
}
42+
43+
void loop () {
44+
rtc.read_timer(PCF8523_Timer_Countdown_A, &state);
45+
46+
Serial.print("timer value: ");
47+
Serial.print(state.value, DEC);
48+
Serial.print(", enabled: ");
49+
Serial.print(state.enabled ? "yes": "no");
50+
Serial.print(", freq: ");
51+
Serial.print(state.freq, DEC);
52+
Serial.println();
53+
54+
Serial.print("irupt flag: ");
55+
Serial.print(state.irupt_state.irupt_flag, DEC);
56+
Serial.print(", enabled: ");
57+
Serial.print(state.irupt_state.irupt_enabled, DEC);
58+
Serial.println();
59+
60+
Serial.println();
61+
delay(1000);
62+
}

0 commit comments

Comments
 (0)