Reading data from the registers of a DS3231 RTC module - a very simple I2C demo program

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 I2C i2c(D14, D15);                // Arduino compatible I2C pins
00003 const int addr = 0xD0;            // Left aligned DS3231 address: 0x68<<1
00004 
00005 int main()
00006 {
00007     char cmd[20];                 // data buffer
00008     printf("\r\nDS3231 I2C RTC readout demo\r\n");
00009     cmd[0] = 0x0E;                // Pointer to CONFIG register
00010     cmd[1] = 0x00;                // Data for CONFIG (enable clock)
00011     i2c.write(addr, cmd, 2);      // Send Address/command and two bytes
00012     while (1) {
00013         wait(1);
00014         cmd[0] = 0x00;            // Pointer to first register
00015         i2c.write(addr, cmd, 1);  // Write adress/command byte, then register address
00016         i2c.read(addr, cmd, 19);  // Read 19 bytes from the DS3231 chip
00017         for(int i=0; i<19; i++) {
00018             printf("%02x ",cmd[i]);
00019         }
00020         printf("\r\n");
00021     }
00022 }