PIC12F1822 MCU with DS1307/DS3231 and I2C LCD | MPLAB Projects

This MPLAB project shows how to build a simple real time clock using PIC12F1822 microcontroller and DS3231 RTC chip (or DS1307 RTC) where time and date are displayed on 16×2 LCD. The DS3231 as well as the 16×2 LCD are connected to the same I2C bus with the PIC12F1822 microcontroller. The LCD is provided with PCF8574 I/O expander which makes it an I2C LCD. This project works also with DFRobot I2C LCD displays.
The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

Last time I made an interfacing of PIC12F1822 and I2C LCD, project link is below:
Interfacing I2C LCD with PIC microcontroller | MPLAB Projects

In this project the DS3231 (DS1307) and the I2C LCD share the same I2C bus which means the SDA lines of the two devices are connected together and the SCL lines also, this connection minimizes number of pins used.
Time and date are displayed on the 16×2 LCD and they can be set with two push buttons connected to the PIC12F1822 microcontroller. With two pins for the I2C bus and two other pins for the push buttons, the microcontroller will use 4 pins (if internal oscillator is used).

PIC12F1822 with DS3231 RTC board and I2C LCD

Hardware Required:
The components required for this project are listed below.

  • PIC12F1822 microcontroller
  • DS3231 (or DS1307) board
  • 16×2 LCD screen
  • PCF8574 I/O expander (or PCF8574A)   —->   PCF8574 datasheet
  • 2 x pushbutton
  • 5 x 10k ohm resistor
  • 330 ohm resistor
  • 10k ohm variable resistor or potentiometer
  • 3V coin cell battery
  • 5V source
  • Breadboard
  • Jumper wires

The Circuit:
The following image shows project circuit diagram with DS3231 board.

PIC12F1822 DS3231 I2C LCD circuit

(All grounded terminals are connected together)

The PIC12F1822 microcontroller has one hardware I2C module with SDA on pin RA2 (#5) and SCL on pin RA1 (#6).

The I2C LCD and the DS3231 (or DS1307) board share the same I2C bus which means the SDA line of the I2C LCD (presented by PCF8574) and the DS3231 RTC chip are connected together with RA2 pin of the PIC12F1822 MCU, the SCL line of the I2C LCD and the SCL line of the DS3231 are connected together with RA1 pin of the PIC12F1822 MCU.

The two push buttons in the circuit are used to set time and date of the real time clock, button 1 (B1) is connected to RA5 pin (#2) and button 2 (B2) is connected to RA4 pin (#3) of the PIC12F1822 MCU.

PIC12F1822 internal oscillator is used (@8MHz) and MCLR pin (RA3) is configured as an input pin.

The C Code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05.

RAM and ROM usage:
When optimization level is 1 the percentage of memory usage is:
RAM used: 39 bytes (30%)
ROM used: 1725 words (84%)

and with optimization level = 0:
RAM used: 39 bytes (30%)
ROM used: 2009 words (98%)

To be able to compile the C code, a small I2C LCD library for MPLAB XC8 compiler is required which can be downloaded from the following link:
I2C LCD driver for MPLAB XC8 compiler

after the download, add the library file (I2C_LCD.c) to project folder.

The I2C LCD driver file is included with the line:
#include “I2C_LCD.c”

The hardware I2C module of the PIC12F1822 is initialized with a clock frequency of 100KHz (100000Hz):

The I2C LCD is initialized with an I2C address of 0x4E:

The two buttons are connected to RA4 and RA5, they are defined as:

Functions used in the C code:
The RTC chip (DS1307 or DS3231) works with BCD format only, to convert BCD to decimal and vise versa I used the 2 functions below. Before displaying (after reading from RTC IC), the data have to be converted from BCD to decimal, and before writing to the RTC IC (after editing the parameters) the data have to be converted from decimal to BCD:
uint8_t bcd_to_decimal (uint8_t number);
uint8_t decimal_to_bcd (uint8_t number);
Each function returns the converted value of the variable number.

void RTC_display() : displays time and date, before printing time and date on the screen, they are converted from BCD format to decimal format using the function bcd_to_decimal(uint8_t number) .

uint8_t edit(uint8_t x, uint8_t y, uint8_t parameter) : I used this function to edit time and date parameters (minutes, hours, date, month and year). I used a variable named i to distinguish between the parameters:
i = 0, 1 : hours and minutes respectively
i = 2, 3, 4: date, month and year respectively

After the edit of time and date, the data have to be converted back to BCD format using the function decimal_to_bcd(char number) and written to the RTC chip.

void delay() : this small function works as a delay except that it can be interrupted by buttons B1 (connected to RA5) and B2 (connected to RA4). When called and without pressing any button the total time is approximately 250 milliseconds. With this function we can see the blinking of the selected parameter with a frequency of 2 Hz. So a delay of 250ms comes after the print of the selected parameter and after that delay, 2 spaces are printed which make the parameter disappears from the screen and another 250ms delay comes after the print of the 2 spaces.

In this function I used Timer1 module (16-bit timer) for the 250 ms delay. Timer1 module is configured to increment by 1 every 4 microseconds (1 tick per 4 us). That means Timer1 input clock frequency is 250kHz. The configuration is shown below:

For the calculation of Timer1 clock frequency I used the following function:
Frequency = Fosc / ( 4 x prescaler) = 8MHz / ( 4 x 8) = 0.25MHz = 250kHz.

So, to get a delay of 250ms we need Timer1 to increment 62500 times (62500 ticks):
250ms x 1000 / 4  = 62500 ticks

Timer1 registers are TMR1H and TMR1L.

The microcontroller used in this example is PIC12F1822, configuration words are:


  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • CLKOUT function is disabled, I/O function on the CLKOUT pin
  • Brown-out Reset (BOR) enabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on CLKIN pin
  • Low voltage programming disabled
  • In-Circuit Debugger disabled
  • Brown-out Reset voltage (Vbor), low trip point selected
  • Stack Overflow or Underflow will cause a Reset
  • 4xPLL disabled
  • Flash Program Memory Self Write disabled

MPLAB XC8 code:

Hardware circuit result of this project should be as shown in the following video where DS3231 board is used:

and Proteus simulation should be as shown in the video below (using DS1307):

Proteus simulation file download:
PIC12F1822 + DS1307 + I2C LCD

Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

2 thoughts on “PIC12F1822 MCU with DS1307/DS3231 and I2C LCD | MPLAB Projects”

  1. ROM used: 2009 words (98%)?

    i currently have 49% of used rom, if you want i can send you a patch tool for xc8/xc16/xc32 to get a healthier size of used rom!

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top