App note: Software real time clock and calendar.

Here is an app note from Microchip that provides information on how to implement a real time clock and calendar on the PIC16f18xx series of microcontrollers.

It uses an external 32.768KHz crystal that drives the Timer1 resource of the uC. The neat thing about a 32.768Khz clock source is that it can be divided by counters to output exactly 1 sec. An added bonus is that the Timer1 can work during sleep mode, thus saving power.

This design uses the I2C protocol to interface with other devices. Microchip provided source code for PIC16F18XX and PIC16F19XX devices.

Join the Conversation

1 Comment

  1. Strange …

    I think the best way to implement a calendar is to make a counter that just counts unix time (seconds since 1970)

    After that, you make all your calculations in unix time, which is much more straightforward, and eventually, if the GUI needs a string, you convert it to a string.

    This approach avoids many problems, glitches on interrupts, what not. If you send your timestamps to another micro, you can just send one int (32 or 64). The timer counter is a 15 bit value, you can concatenate it easily with the seconds to get a consistent value, etc…

    Implementing minute, hour, second, day, month etc counters is legacy, and should not be done any more, even on small micros.

Leave a comment

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.