MCP7940 – RTC with Pi

As I’m building my own IQ house control system, I need to have RTC in my system. So started playing with MCP7940N and i2c interface. I’m using Raspberry Pi for my experiments with i2c/SPI.

Construction is pretty simple, just use MCP7840N Datasheet.

mcp7940n-schema

 

Then you can start checking with Pi. In my case, I’m using RevB, so my bus has number 1 and RTC got 0x6f address.

what next? Check address 0x00, if the onboard oscilator is running

0x00 means NO, it’s NOT running. So, turn it on

and check few times address 0x00

Increments! Great. That means – our oscilator is working = our clock is working too. Next step is to set current time and date. But wait. How are values stored and read/written? RTC uses BCD encoding for values, that means, eg. number 94 is stored as 9 and 4, both in 4 bits = 9 is stored as 1001 and 4 as 0100, in hexadecimal 0x94. Easy? Eg. day is using 6 bits, upper two for 0123 values, bottom bits for 0-9, so 31th will be stored as 0x31. For more details please read the PDF – RTCC Memory map. So back to setting the date & time:

Fine. But if we’re setting ’00’ as seconds – address 0x00 – why value 0x80? 7th bit = ST – onboard oscilator enabled.

I’ve written simple Python code to read from RTC and print out the value:

Just run by

Voila! :) RTC is up and running. I’d like to check tomorrow (resp. today) morning, if everything is still working correctly, and can create next module for my IQ house control system :)

6 thoughts on “MCP7940 – RTC with Pi

  1. Hello Tom,

    I bought the same RTC module for my raspberry pi but I cannot get it to work. I hope you can give me a hint. I run the setup script from the piface github repository (tried both master and dev_tp branches) and /dev/i2c-1 is created. But:

    # i2cdetect -y 1 //shows all “–”
    # i2cget -y 1 0x6f 0x00 b // Error: read failed (or read failed for i2cset)

    It seems like there is no connection at all with the RTC. Am I missing something?

    Thank you very much for your time.

  2. Thanks for the quick reply.

    # i2cdetect -y 0
    Error: Could not open file ‘/dev/i2c-0’ or ‘/dev/i2c/0’: No such file or directory

    The setup script creates /dev/i2c-1, but there is no communication using i2c tools. Did you use some setup besides the official script? Any other configuration/module?

  3. I did:

    if it’s working, then update your /etc/modules file and add these two lines into it:

    which RasPi revision do you have?

  4. I think I found the error. i2c-tools install was throwing an erro at the end:

    /run/udev or .udevdb or .udev presence implies active udev. Aborting MAKEDEV invocation.

    I don’t know what this means, but it seems to be the source of my problems. Wierd, it is a clean raspbian installation.

    Anyway, thank you, I appreciate your help. Congratulations on your blog :)

Leave a Reply

Your email address will not be published. Required fields are marked *

37 − = 32