Arduino Real Time Clock – Using Microchip’s MCP79400

 

 

 

 

 

 

 

 

 

 

An Arduino real time clock is easy to implement using Microchip’s MCP79400.   A real-time clock is basically a device that counts seconds, hours, etc.  You can design an Arduino real time clock in microcontroller firmware, but there are latency issues that cause the time measurement to drift considerably over time.  For example, you could use the Arduino “delay(1000)” command and a simple counter to count seconds.

delay(1000);
seconds = seconds + 1;

Since the delay subroutine and counter increment won’t take exactly 1000ms your clock will count slow, and eventually drift off of an accurate measurement.  If your clock is off by 10uS you will lose about a second of accuracy every day, if it is off by 100us you lose a second every couple of hours.  When using  controllers like the Arduino and Parallax BASIC Stamp you don’t have instruction level control over timing, and often the overhead of creating a clock function becomes a burden on the rest of the design.

An Arduino real time clock using standard hours, minutes, seconds, and calendar information might be best designed around an external IC.  Microchip’s MCP79400, a real-time clock calendar (RTCC), can eliminate timing and overhead issues.    Dallas Semiconductor (I think now owned by Texas Instruments via Maxim Semi.) has always made nice RTCC parts.  Microchip’s part is similar and we’re adding it to a breakout board for resale.  I haven’t looked in detail to see if the Microchip part crosses directly with a Dallas part, but the register structure and I2C communication interface look very similar.

The MCP79400 has quite a bit of functionality built into a small low cost package.  This allows an Arduino rela time clock to pack in all of the avaialble features.  There are two alarm settings, calibration registers for fine-tuning accuracy, extra RAM locations for Arduino use, and a standard clock and calendar.  The code below has been implemented to enable the RTCC function and display the seconds count using an Arduino Uno Rev3.  It’s a simplified example but provides a good starting point for interfacing to the MCP79400.

#include <Wire.h>

// Set up a serial monitor port and initialize the MCP79400 to run.

void setup()
{
Wire.begin();                                         // join i2c bus (address optional for master)
Serial.begin(9600);                              // start serial for output

  Wire.beginTransmission(0x6f);   // transmit to device address 111 (0x6F)
Wire.write(0);                                     // point to internal register “0”
Wire.write(0x80);                              // Set highest bit to enable 32KHz clock
Wire.endTransmission(0);             // send bytes and stop transmission
delay(5);

  Wire.beginTransmission(0x6f);   // transmit to device address 111 (0x6F)
Wire.write(7);                                     // point to internal register “7”
Wire.write(0x40);                             // make MFP pin an output w/ 1HZ square wave
Wire.endTransmission(0);            // send bytes and stop transmission
delay(5);
}

// Main program loop reads MCP79400 registers associated with time/date.

void loop()
{
Wire.beginTransmission(0x6f);   // transmit to device address 111 (0x6F)
Wire.write(0);                                    // point to internal register “0”
Wire.endTransmission();              // send bytes and stop transmission

  Wire.requestFrom(0x6f,6);       // reads bytes from sequential addresses
byte Seconds = Wire.read();     // receive a byte
byte Minutes = Wire.read();     // receive a byte
byte Hours = Wire.read();       // receive a byte
byte Day = Wire.read();         // receive a byte
byte Date = Wire.read();        // receive a byte
byte Month = Wire.read();       // receive a byte
byte Year = Wire.read();        // receive a byte

//Convert BCD seconds to total seconds and send to serial monitor

Serial.print((((Seconds&0x70)>>4)*10)+(Seconds&0x0f));  // print seconds
Serial.println(” seconds”);
delay(1000);
}

It is also interesting to note that Microchip has been selling microcontrollers with the RTCC hardware built-in.  This really allows the designer access to the best of both solutions.  These controllers provide the RTCC hardware internal to the controller and are therefore lower cost (fewer parts for assembly – less PCB space).

 

Comments

  1. beautifulsmall says:

    NIce ,I2C R3 interface of Due doesnt, ack with 1.5.2 dev kit, , so reverted to Mega 2650, same R3 interface but all 5V, just saying thanks.some x and ” chars are in wierd font

Speak Your Mind

*