Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTC.setPeriodicCallback can't manage it 256 times a second #451

Open
alistairgeorge6 opened this issue Feb 26, 2025 · 0 comments
Open

RTC.setPeriodicCallback can't manage it 256 times a second #451

alistairgeorge6 opened this issue Feb 26, 2025 · 0 comments

Comments

@alistairgeorge6
Copy link

RTC.setPeriodicCallback works as advertised up to Period::N128_TIMES_EVERY_SEC, but N256_TIMES_EVERY_SEC gives the same callback rate as N128_TIMES_EVERY_SEC. Sample here shows the issue.

#include "RTC.h"

void setup() {
  Serial.begin(9600);
  while(!Serial)
    ; // do nothing
  Serial.println("started");
  RTC.begin();
  RTCTime start = RTCTime(0UL);
  RTC.setTime(start); // have to set a time, doesn't matter what
}

volatile unsigned long rate = 0;

void on_timer() {
  static unsigned long count = 0;
  static unsigned long last_secs = 0;
  unsigned long secs = millis() / 1000;
  if (secs != last_secs) {
    rate = count;
    count = 0;
    last_secs = secs;
  }
  ++count;
}

void run_test(Period period) {
  if (!RTC.setPeriodicCallback(on_timer, period)) {
    Serial.println("ERROR: periodic callback not set");
  }
  delay(1500); // ignore first result
  for (unsigned int i = 0; i < 5; ++i) {
    delay(1000);
    Serial.print("rate = ");
    Serial.println(rate);
  }
}
void loop() { 
  Serial.println("Expect 64 per sec");
  run_test(Period::N64_TIMES_EVERY_SEC);
  Serial.println("Expect 128 per sec");
  run_test(Period::N128_TIMES_EVERY_SEC);
  Serial.println("Expect 256 per sec");
  run_test(Period::N256_TIMES_EVERY_SEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant