A Real Time Clock with a PIC


This project is intended to be a Real Time Clock (RTC). It is based on a Microchip 18f14k22 PIC microcontroller and communicates with a host with I2C.

Schematics and Hardware

As the schematics show, there is nothing more than a microcontroller, a watch crystal, a 3.0V battery and 2 switching diodes.

Connector conn3 connects the board to the main board and supplies the 3.3V power / ground and the I2C SDA, SCL wires.

Connector conn1, conn2 can be used respectively to connect a serial line and an "in circuit programmer".

picture of the board.


The main task of the software is to make +1 on a 32 bits unsigned int variable at a one second rate, with or without main power. The timer 1 is used as a 16 bits counter to divide the 32768 Hz provided by the timer 1 internal oscillator. It is preloaded at 0x8000, get a clock pulse every 1/32768 second and overload every second that provide an interrupt to do the task.

When working in normal mode (normal power), the system has to answer to the I2C requests sent by the host. This is done by the I2C driver which is called by interrupt at each state of I2C internal hardware.

The software also has to manage power. This is done by switching to input the port pin used for the blinking led and by checking its logic level. If the level is 0, the processor enters power down idle mode and is driven by low frequency timer1 oscillator (32768 Hz). The processor wake up at each interrupt provided by timer 1 and recheck power. When the power level comes back to its normal level, the processor enters its normal mode and switch its clock to the internal oscillator (8 Mhz).

The Pic bootloader program [2] is embedded in the software and let you re-flash the application from the serial line.



The software is developed on a Linux system with SDCC - Small Device C Compiler, see SDCC for download and documentation.

SDCC itself requires the gputils package for gpasm and gplink, the assembler and linker for PIC software, see gputils package.

Schematics are created with gschem from the gEDA suite see [3]. You could need it to edit, print, view or, why not, create other schematics.

Compiling the firmware

You need to have the tools installed, and next run the following commands.

tar zxvf picrtc-yyyymmdd.tar.gz
cd picrtc-yyyymmdd


  1. [1] Controller using this RTC.
  2. [2] An Pic Avr bootloader program.
  3. [3] my gEDA symbols.
  4. [4] you can find the datasheet of the 18f14k24 processor on the Microchip Site.


The Picrtc software is licensed under the terms of the GNU General Public License as published by the Free Software Foundation. See the file "COPYING" in the download directory.


A special thanks to Jan Panteltje and Spehro Pefhany for publishing a note about the hidden bit 7 DEBUG flag in configuration CONFIG4L of the 18f14k22 PIC microcontroller. Live is easier when the bit is forced to 1 !