Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created November 1, 2015 23:15
Show Gist options
  • Save Hermann-SW/5b487ece9d62cf669caf to your computer and use it in GitHub Desktop.
Save Hermann-SW/5b487ece9d62cf669caf to your computer and use it in GitHub Desktop.
Measuring >40k interrupts per second by DS3231 32K and 8kHz enabled SQW lines on pins 2+3 of Arduino
// needed for RTC.set(_, _), see bottom as well
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
// from http://www.pjrc.com/teensy/td_libs_DS1307RTC.html
long lcnt=0;
void fcnt(void) { ++lcnt; }
void setup(void) {
Serial.begin(57600);
/*
enable 8kHz SQW square wave (is disabled on DSC3231 powerup)
http://datasheets.maximintegrated.com/en/ds/DS3231.pdf#page=13
*/
RTC.set(0x0E, 0x18);
// do count interrupts from DS3231 pin 32K
attachInterrupt(digitalPinToInterrupt(2), fcnt, FALLING);
// and count interrupts from DS3231 pin SQW
attachInterrupt(digitalPinToInterrupt(3), fcnt, FALLING);
}
void loop(void) {
// output interrupts per second
Serial.println(1000000.0*lcnt/micros());
delay(500);
}
/*
added to DS1307RTC.h:
static bool set(uint8_t reg, uint8_t val);
and to DS1307RTC.cpp:
bool DS1307RTC::set(uint8_t reg, uint8_t val)
{
Wire.beginTransmission(DS1307_CTRL_ID);
Wire.write(reg); // reset register pointer
Wire.write(val) ;
if (Wire.endTransmission() != 0) {
return false;
}
return true;
}
*/
@Hermann-SW
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment