Notes About Working with Various Arduino & Netduino Microcontroller Boards

Saturday, June 23, 2012

Using Chronodot Square Wave to Count Seconds


I was a bit dense when I first tried to use the Chronodot v2.1 real-time clock's square wave function to count seconds. I finally came up with some very simple code that got me started. There are more complex examples out there, but I couldn't see the forest for the trees when trying to make sense of them. The sample below just counts the seconds and prints them to the serial monitor.

For some reason, I can't get this to work on the Arduino Leonardo (even with connection to SDA and SCL pins on Leonardo).
Connections between the Chronodot and  the Arduino Uno are:
SCL --> A5
SDA --> A4
SQW --> D2
VCC --> 5V
GND --> GND

Here's the sketch:

#include <Wire.h>

#define intPin   2
#define intNum   0
#define ds3231   0x68
#define ctrlReg  0x0E
// bits 4 & 3 in ctrlByte set to 0 = 1Hz
#define ctrlByte 0b00000000 

volatile unsigned int c = 0;

void setup()
{
  pinMode(intPin, INPUT);
  //Use built-in pull-up resistor on D2
  digitalWrite(intPin, HIGH);
  attachInterrupt(intNum, handleInterrupt, FALLING);
  Wire.begin();
  Serial.begin(115200);
  Wire.beginTransmission(ds3231); 
  Wire.write(ctrlReg); 
  Wire.write(ctrlByte);
  Wire.endTransmission();
}

void handleInterrupt() {
  Serial.println(++c);
}

void loop() { ; }

1 comment: