Some time ago, I received a MAXIM Analog Essentials Collection, a set of modules containing a number of the company's interface chips. The form-factor is called a Pmod, or "Peripheral Module," a standard created by Digilent to provide an easy interface to FPGA development boards. While I originally got the modules to use with my FPGA projects, my microcontroller expertise exceeds my FPGA expertise and I'm not ready to test the modules with programmable logic. But that's no reason I can't use individual modules with an MCU, so I pulled out the real-time clock (RTC) module to connect to my ARM mbed.
Some of the MAXIM Pmods make a lot of sense for use with an FPGA, but not so much for use with a well-equipped MCU. But I don't have a RTC built into any of my MCU boards. The MAXIM RTC board uses a MAXIM DS3231 RTC chip and just needs only a small battery and some software to get it "ticking" with an ARM mbed board.
The RTC chip provides an I2C port, but the chip's documentation wasn't clear about how to use it. While searching the Internet for more information, I ran across a thorough library, written by Remi Cormier and hosted on the mbed website at www.mbed.org. It took about five minutes to get the I2C port running and a few more minutes to set the time.
Here's my hardware setup:
The RTC module requires +3.3V and ground, along with the SDA and SCL communication signals. The mbed doesn't have built-in I2C pull up resistors, so I add them. The software proved equally easy to get running, thanks to the work by Remi. To start, I created a new program in the free mbed compiler, then imported the DS3221 library into my program named "test."
This import won't change main.cpp; it just gives you access to the library functions. If you want to take a shortcut like I did, the file DS3231.h contains an complete mbed example program in comments at the top of the file.
I just copied and pasted the example code into my main code, and replaced the sample LED-control code the mbed compiler always puts in a new program.
After fiddling with the code and setting the date and time, I added code to read and display the date and time once a minute on my mbed terminal window.
The RTC module has several handy features that I didn't use: two alarms and a square wave output. These capabilities let you trigger an interrupt in your host MCU based on a specific time and date or at specific intervals. Whether I get the FPGA component of my robot running soon, or drive it with just an MCU for a while, the RTC module will be a part of it. How often do you put an RTC in a design and what do you typically use it for?