Feed on
Posts

## Building a LED clock by using the Microchip MCP7940M Real-time Clock

As part of the Mini Temp Logger design I need to look at a better way of keeping time other than using the watchdog timer as it’s fairly inaccurate. I stumbled across the Microchip MCP7940M Real-time clock controllable by I2C, you can set alarms, a clock output (likely what I might use for my project), ability to trim the oscillator in 1PPM increments (129PPM -/+ range), low power consumption at 1.2uA and the price was \$1.

Choosing capacitors

This RTC recommends the use of 6-9 pF 32.768KHz crystals, the most common/cheap ones are 12.5pF and it still does work with them but it won’t be as accurate as the 6-9pF ones (and for me it sometimes stopped working), it took me a while to realise this as I was reading the preliminary datasheet for a while.

The first thing we need to do is calculate the capacitors we’ll require for our crystal, there is a standard formula which we follow for this, Cstray is usually between 2-5pF so lets assume ours will be half way at 3.5pF. I went with a low cost (at the time) EuroQuartz MH32768L crystal that has a CL of 6pF. The best match for our formula is 6pF for CX1 and CX2: (6 pF * 6 pF)/(6 pF + 6 pF) + 3.5 pF = 6.5pF which is close to our CL of 6 but you may have to vary the capacitors depending on your testing.

Interfacing

I built a simple PCB for the RTC (doesn’t look the best as I built it about 6 months ago when I didn’t use to sand the PCB) but there isn’t much to it. We just need a crystal with 2 caps, a cap for the chip and then just I2C resistor pull ups. Let’s interface with it, I’ll start off using the Arduino and we’ll move to an ATmega328 later on.

We have the memory mapping available to us from the datasheet, the first thing to do is need to turn on the oscillator by turning on the ST bit in the RTCSEC register.

By default the time starts at 00:00:00, to read out the date/time you need to access a few registers.

Notice how the bits are arranged in the registers, it isn’t quite a seconds to binary mapping, normally 35 seconds would translate to 100011 but instead they have 3 bits to keep track the 10’s in the seconds (11 in our case) and 4 bits for the 1’s in the seconds (101 in our case). It’s a little strange how they have it that way because doing a second to binary mapping would actually save them 1 bit. We can also write the date/time to the same registers that we read them out from.

Testing, Trim and Multifunction Pin

Now it’s time for testing, depending on your PCB you may have to vary the capacitors slightly, for a RTC it’s best to leave it for a day or two to see how far it varies from real time. You can try and use a scope to measure the frequency to see if you are near the frequency but you need to take account that the scope probe will add an extra capacitance, for mine it’s 15pF. As you can see the voltage is very low which I guess is how they keep it low current.

Once the right capacitors have been found, we can trim the oscillator to make it even more accurate and have it run for a few days or weeks. There are 2 different trimming options available to us, either trim once cycle per minute or once cycle per second, we’ll use once per minute as in my testing I’ve only found it drifting a second or two every few days so we don’t need too much trim.

We set the sign bit to add or subtract cycles, for me I needed to add cycles as it was running slower and then we set the cycles to add/remove, I needed 32 more cycles so I send the number 160. You’ll need to leave it for at least a 1 week to really test how much more trimming you need.

With the multi-function pin, the square wave clock output is what I’ll need to look at for my Mini Temp Logger project so it could wake up the AVR every second. It’s an open-drain pin so we’ll stick in a 10K resistor to VCC and tap off the pin to our AVR. We divide the clock down from 32.768KHz to 1Hz by leaving the SQWFS set to 00 and enabling the output by setting SQWEN to 1, now we’ll receive a pulse 1 every second.