Hey all,
I'm in the middle of writing a library to read the time from a TI BQ32000 I2C real-time clock. I've successfully read and written to the registers, but I can't figure out a simple way to convert the read values to a readable time format.
Let's just deal with the seconds register for now, as once I figure out how to handle this, I can apply the same to all other registers.
The seconds register on the BQ32000 is set up the same way as the DS1307, the register bits are:
|ctrl| | 10 seconds | | 1 second |
D7 D6 D5 D4 D3 D2 D1 D0
In this form, 59 would be (D7 is always 0): 0-101-1001 where 101 represents "5", and 1001 represents "9", thus 59. I am able to read the register values just fine, however I am a little stuck at how to convert them to actual seconds. What I mean is, if I read this value straight from the clock and convert it to decimal, I will obviously not get a correct value for seconds as 01011001 is 89 in decimal, not 59.
I thought of mapping each and every value from 0-000-0000 to 0-101-1001 to it's equivalent seconds value, but that seems like the wrong way to do it. Then I figured if I could read the register bit by bit, storing each bit in its own variable and using that to calculate the seconds, but even that seemed a little "bit" unnecessarily complex.
Here's the code I'm using to test the chip, although I don't think it's necessary to solve this problem.
#include <Wire.h>
#define CLOCK_ADDRESS B01101000
byte secs;
void setup()
{
Serial.begin(9600); //Start serial comms
Wire.begin(); //Start I2C bus
}
void loop()
{
Wire.beginTransmission(CLOCK_ADDRESS);
Wire.send(0x00); //Set pointer to register 0 (seconds)
Wire.endTransmission();
Wire.requestFrom(CLOCK_ADDRESS, 1);
secs = Wire.receive();
Serial.println(secs, BIN);
delay(1000);
}
Thanks!
P.S. I'm writing my own library to get a better understanding of how to use the I2C bus. I hate having to choose my parts based on what libraries are available, so I'm learning how to write my own.