As an Amazon Associate we earn from qualifying purchases.
A microcontroller does not know the time of day by itself. When it loses power, any counter in its memory starts again from zero. A real time clock (RTC) chip solves this. It counts seconds, minutes, hours, the date and the year on its own, and a small backup battery keeps it running when the main supply is off.
This project connects a BQ32000 RTC, a small SMD chip, to an MSP430G2553 LaunchPad over I2C. The LaunchPad reads the time and date once per second and sends them to a PC over the serial port, where you can watch them in the X-CTU serial monitor.
MORE ATHow an RTC works with a microcontroller
An RTC is a small chip with its own oscillator, usually driven by a 32.768 kHz watch crystal. It keeps the time in a set of registers. The microcontroller never has to count time itself. It only reads those registers when it needs the time, and writes them once to set the clock.
The BQ32000 from Texas Instruments is an I2C RTC. It stores the time in BCD (binary coded decimal) format, so a value of 0x15 means 15, not 21. The code below converts BCD to normal decimal numbers before printing them.
The BQ32000 and the better known DS1307 both respond at I2C address 0x68 and use the same register layout for time and date. That is why the same sketch works with either chip. Check the datasheet of the chip you use for details the sketch does not cover.
What you need
- MSP430 LaunchPad with the MSP430G2553
- BQ32000 RTC IC (on a breakout board if you do not want to solder the SMD package) with its 32.768 kHz crystal
- Backup battery for the RTC
- Two pull-up resistors for the I2C lines, unless your breakout board already has them
- A PC with the Energia IDE and a serial terminal such as X-CTU
- Jumper wires and a breadboard
Connections
On the MSP430G2553 the hardware I2C pins are P1.6 for SCL and P1.7 for SDA. Connect them to the SCL and SDA pins of the RTC, and join the grounds of the two boards. Both I2C lines need pull-up resistors to the supply, because I2C devices only pull the lines low.
On some LaunchPad revisions P1.6 is also connected to an on-board LED through a jumper. If the clock does not respond, check that this jumper does not load the SCL line.
The circuit diagrams below show the wiring.
OUTPUT ON X-CTU SERIAL MONITOR
CIRCUIT
How the program works
The sketch uses the Wire library, which Energia provides for the MSP430. It does four things:
- Setup: starts I2C and the serial port at 9600 baud, then writes the starting time to the RTC.
- Reading: every second it sets the register pointer to 0 and requests 7 bytes: seconds, minutes, hours, weekday, date, month and year.
- Conversion: each byte goes through bcdToDec(), which turns the BCD value into a normal number.
- Output: the date and time are printed over the serial port in the form day-month-year and hour:minute:second.
The setTime() function writes the values 12 seconds, 20 minutes, 15 hours, weekday 7, date 10, month 11 and year 15, all in BCD. Change these to the current time before you upload.
PROGRAM
//Here I am interfacing a real time clock (BQ32000 or DS1307) with the MSP430G2553 LaunchPad (Energia) and sending the time to the PC via serial port
//connect SDA---> P1_7, SCL---> P1_6
#include <Wire.h>
const int DS1307 = 0x68; // Address of DS1307 AND BQ32000 ARE SAME- see data sheets
byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;
void setup()
{
Wire.begin();
Serial.begin(9600);
delay(100);
setTime();
Serial.print("HHH");
}
void loop()
{
readTime();
delay(1000);
}
byte bcdToDec(byte val)
{
int msbdec=val/16*10; //gets decimal msb of BCD value(4 bits)
int lsbdec = val%16;//gets decimal lsb of BCD value(4 bits)
int total=msbdec+lsbdec;
return (total);
//return ((val/16*10) + (val%16));
}
void readTime()
{
Wire.beginTransmission(DS1307);
Wire.write(byte(0));
Wire.endTransmission();
Wire.requestFrom(DS1307, 7);
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read());
weekday = bcdToDec(Wire.read());
monthday = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
Serial.print(monthday);
Serial.write('-');
Serial.print(month );
Serial.write('-');
Serial.print(year);
Serial.write('-');
Serial.write('-');
Serial.print(hour);
Serial.write(':');
Serial.print(minute);
Serial.write(':');
Serial.print(second);
Serial.write(0xd);
}
void setTime() {
// The following codes transmits the data to the RTC
Wire.beginTransmission(DS1307);
Wire.write(byte(0)); //starts sending data writing
Wire.write(0x12);//second
Wire.write(0x20); //minute
Wire.write(0x15); //hour
Wire.write(0x7); //weekday
Wire.write(0x10);//monthday
Wire.write(0x11);//month
Wire.write(0x15);//year
Wire.write(byte(0));
Wire.endTransmission();
// Ends transmission of data
}
Tips for reliable results
- Set the time only once. As written, the sketch calls setTime() on every start, so the clock returns to the preset time after each reset. Once the time is correct, upload a version without that call. The RTC then keeps the right time on its battery.
- Check the pull-ups. If the serial monitor shows zeros or nonsense, missing pull-up resistors on SDA and SCL are a common cause.
- Check the crystal. The RTC needs its 32.768 kHz crystal, placed close to the chip, to count correctly.
- Single digits. The program prints numbers without leading zeros, so 5 minutes appears as 5, not 05. Add a small check before printing if you want a tidier format.
NOTE
THIS PROGRAM WORK WITH RTC IC DS1307 (SUPPLY MUST BE 1.25*BATTERY VOLTAGE)
In other words, the same code also runs on a DS1307. Check the supply and battery voltage requirements in the datasheet of the chip you use before powering it.
Related projects
If you prefer another board, see the same chip used as BQ32000 with an Arduino. For a different RTC and microcontroller pair, there is a simulation of the DS1307 with a PIC18F4550. If you want to show the time on a display, try a seven segment display clock built around the DS1307. If you are new to the LaunchPad, programming it with Energia is the simplest way to start.
FAQ
Can I use a DS1307 instead of the BQ32000?
Yes. Both chips use I2C address 0x68 and the same time and date registers, so this sketch works with either. Check the supply and battery requirements of the chip you choose.
Does the MSP430G2553 have a built-in RTC?
No. It has timers that can count time while it is powered, but it cannot keep the date and time through a power loss. An external RTC with a backup battery can.
Why are the values stored in BCD?
RTC chips store each digit of a value in 4 bits. A value such as 0x15 therefore means 15. The bcdToDec() function converts it to a normal number before printing.
Why does the time reset when I restart the board?
The sketch writes the preset time to the RTC in setup(). Remove the setTime() call after the first upload, and the RTC will keep the correct time.