-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding examples for setting a timer, and for monitoring the INT pin.
- Loading branch information
simon
committed
Oct 7, 2019
1 parent
1b3c750
commit 837d684
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Sets a timer for 10 seconds, and watches a pin (MONITOR_PIN) for | ||
* its interrupt signal. | ||
* | ||
* The RTC's INT line is pulled down when the timer goes off and the | ||
* interrupt is active. | ||
*/ | ||
#include <Wire.h> | ||
#include "RTClib.h" | ||
|
||
#define MONITOR_PIN 5 | ||
|
||
RTC_PCF8523 rtc; | ||
Pcf8523TimerState state; | ||
|
||
void setup () { | ||
Serial.begin(9600); | ||
if (! rtc.begin()) { | ||
Serial.println("Couldn't find RTC"); | ||
while (1); | ||
} | ||
|
||
pinMode(MONITOR_PIN, INPUT_PULLUP); | ||
|
||
/* | ||
struct type signatures: | ||
typedef struct { | ||
bool irupt_flag; // whether the timer has gone off | ||
bool irupt_enabled; // whether the flag state is tied to the interrupt pin state | ||
} Pcf8523IruptState; | ||
typedef struct { | ||
bool enabled; // whether the timer is running | ||
uint8_t value; // the current value of the timer | ||
Pcf8523FrequencyDivision freq; // the clock divider used | ||
Pcf8523IruptState irupt_state; // the timer's interrupt state | ||
} Pcf8523TimerState; | ||
*/ | ||
|
||
state.enabled = true; | ||
state.value = 10; | ||
state.freq = PCF8523_Freq_second; | ||
state.irupt_state.irupt_flag = false; | ||
state.irupt_state.irupt_enabled = true; | ||
|
||
rtc.write_timer(PCF8523_Timer_Countdown_B, &state); | ||
} | ||
|
||
void loop () { | ||
rtc.read_timer(PCF8523_Timer_Countdown_B, &state); | ||
Serial.print("timer value: "); | ||
Serial.print(state.value, DEC); | ||
Serial.print(", enabled: "); | ||
Serial.print(state.enabled ? "yes": "no"); | ||
Serial.print(", freq: "); | ||
Serial.print(state.freq, DEC); | ||
Serial.println(); | ||
Serial.print("irupt flag: "); | ||
Serial.print(state.irupt_state.irupt_flag, DEC); | ||
Serial.print(", enabled: "); | ||
Serial.print(state.irupt_state.irupt_enabled, DEC); | ||
Serial.println(); | ||
|
||
Serial.print("Interrupt pin: "); | ||
Serial.println(digitalRead(MONITOR_PIN) ? "HIGH": "LOW"); | ||
|
||
Serial.println(); | ||
delay(1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Sets a timer for 10 seconds on start, and monitors the status/flag | ||
* values in a loop. | ||
*/ | ||
#include <Wire.h> | ||
#include "RTClib.h" | ||
|
||
RTC_PCF8523 rtc; | ||
Pcf8523TimerState state; | ||
|
||
void setup () { | ||
Serial.begin(9600); | ||
if (! rtc.begin()) { | ||
Serial.println("Couldn't find RTC"); | ||
while (1); | ||
} | ||
|
||
/* | ||
struct type signatures: | ||
typedef struct { | ||
bool irupt_flag; // whether the timer has gone off | ||
bool irupt_enabled; // whether the flag state is tied to the interrupt pin state | ||
} Pcf8523IruptState; | ||
typedef struct { | ||
bool enabled; // whether the timer is running | ||
uint8_t value; // the current value of the timer | ||
Pcf8523FrequencyDivision freq; // the clock divider used | ||
Pcf8523IruptState irupt_state; // the timer's interrupt state | ||
} Pcf8523TimerState; | ||
*/ | ||
|
||
state.enabled = true; | ||
state.value = 10; | ||
state.freq = PCF8523_Freq_second; | ||
state.irupt_state.irupt_flag = false; | ||
state.irupt_state.irupt_enabled = true; | ||
|
||
rtc.write_timer(PCF8523_Timer_Countdown_A, &state); | ||
} | ||
|
||
void loop () { | ||
rtc.read_timer(PCF8523_Timer_Countdown_A, &state); | ||
|
||
Serial.print("timer value: "); | ||
Serial.print(state.value, DEC); | ||
Serial.print(", enabled: "); | ||
Serial.print(state.enabled ? "yes": "no"); | ||
Serial.print(", freq: "); | ||
Serial.print(state.freq, DEC); | ||
Serial.println(); | ||
|
||
Serial.print("irupt flag: "); | ||
Serial.print(state.irupt_state.irupt_flag, DEC); | ||
Serial.print(", enabled: "); | ||
Serial.print(state.irupt_state.irupt_enabled, DEC); | ||
Serial.println(); | ||
|
||
Serial.println(); | ||
delay(1000); | ||
} |