/* Read register 0 (second count) of DS3231
*
* This program reads register at address 0 in the DS3231, the second count,
* repetitively and dump the data to the LEDs. Since the second count uses
* binary coded decimal format, the LEDs will count up from 0 to 9 in binary
* and wrap around to 0.
*
* This program uses Wire library. For details of libraries:
* http://energia.nu/reference/libraries/
*
* Energia 1.6.10E18
*/
#include
#define SLAVE_ADDR 0x68
const int LED[] = {3, 4, 19, 38};
void setup() {
Wire.begin();
// configure input output pins for LEDs
for (int i = 0; i < 4; i++)
pinMode(LED[i], OUTPUT);
}
void loop() {
unsigned char seconds;
I2C_byteRead(SLAVE_ADDR, 0, &seconds);
digitalGroupWrite(LED, 4, seconds);
delay(200);
}
void I2C_byteRead(int slaveAddr, unsigned int memAddr, unsigned char *data)
{
Wire.beginTransmission(slaveAddr);
Wire.write(memAddr);
Wire.endTransmission();
Wire.requestFrom(slaveAddr, 1);
if (Wire.available())
*data = Wire.read();
}
// write to a group of output pins
void digitalGroupWrite(const int* group, int length, unsigned int data)
{
for (int i = 0; i < length; i++)
digitalWrite(group[i], data & (1 << i) ? HIGH : LOW);
}