Skip to content

Commit

Permalink
Adding examples for setting a timer, and for monitoring the INT pin.
Browse files Browse the repository at this point in the history
  • Loading branch information
simon committed Oct 7, 2019
1 parent 1b3c750 commit 837d684
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
70 changes: 70 additions & 0 deletions examples/pcf8523_monitor_timer/pcf8523_monitor_timer.ino
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);
}
62 changes: 62 additions & 0 deletions examples/pcf8523_set_timer/pcf8523_set_timer.ino
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);
}

1 comment on commit 837d684

@colindgrant
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @simzes, I was delighted to find this enhancement! I wanted to let you know (if you did not already) that Adafruit requested just some minor changes. I think this contribution is very close to being accepted into the upstream RTCLib. Any chance you could address the review comments (adafruit#124) so that your work is available to the greater community right from the Arduino Library Manager?

Nice work regardless, thank you!

Please sign in to comment.