Interfacing DS1307/DS3231 RTC with PIC MCU | MPLAB Projects

This post shows how to build a simple real time clock using PIC16F887 microcontroller and DS1307 (or DS3231) RTC chip. Time and date are displayed on 16×2 LCD screen and they can be set with two push buttons connected to the MCU.
The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

Related Projects:
Interfacing LCD with PIC microcontroller | MPLAB Projects

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

  • PIC16F887 microcontroller   —  datasheet
  • DS3231 (or DS1307) board   —  DS1307 datasheet   —  DS3231 datasheet
  • 16×2 LCD screen
  • 2 x pushbutton
  • 330 ohm resistor
  • 10k ohm variable resistor or potentiometer
  • 3V coin cell battery
  • 5V source
  • Breadboard
  • Jumper wires

Generally the DS1307 or DS3231 board contains the main chip (DS1307 or DS3231 IC), 2 pull-ups resistors and battery holder.

Interfacing DS1307/DS3231 RTC with PIC microcontroller circuit:
The following image shows circuit connection of PIC16F887 MCU, DS1307 RTC and 16×2 LCD screen.

PIC microcontroller with DS1307 RTC circuit - MPLAB XC8 DS1307

and the below image shows the same circuit but with DS3231 board instead of DS1307:

PIC microcontroller with DS3231 RTC circuit - MPLAB XC8 DS3231

(All grounded terminals are connected together)

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 RB0 pin (#33) and button 2 (B2) is connected to RB1 pin (#34) of the PIC16F887 MCU.

The 16×2 LCD screen is connected to the PIC16F887 microcontroller as follows:
RS —> RD0 pin
E  —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground)
VEE to the variable resistor (or potentiometer) output pin
VDD to +5V and A to +5V through 330 ohm resistor

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing DS1307/DS3231 RTC with PIC microcontroller 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.

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

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

I2C Functions:
The MPLAB XC8 compiler doesn’t have any I2C library for the PIC16F887 microcontroller which means we’ve to write own I2C function codes.
The PIC16F887 has one MSSP module which can work in I2C mode. For that I wrote some functions which allowed me to communicate with the RTC chip using the MSSP module:
void I2C_Init(uint32_t i2c_clk_freq) : initializes the MSSP module in I2C mode (master mode) with a clock frequency of i2c_clk_freq.
void I2C_Start() : this function sends a start signal to the I2C slave device.
void I2C_Repeated_Start() : sends a repeated start signal.
void I2C_Stop() : sends a stop signal.
void I2C_Write(uint8_t i2c_data) : writes data (i2c_data) to the I2C device.
uint8_t I2C_Read(uint8_t ack) : reads (& returns) data from I2C device, this function sends acknowledge pulse if ack = 1.

RTC Functions:
The RTC chip (DS1307 or DS3231) works with BCD format only, to convert the 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 them on the LCD, time and date data 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(uint8_t number) and written to the RTC chip.

void blink() : this small function works as a delay except that it can be interrupted by buttons B1 (connected to RB0) and B2 (connected to RB1). When called and without pressing any button the total time is 100 x 5ms = 500ms. With this function we can see the blinking of the selected parameter with a frequency of 1 Hz. So a delay of 500ms 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 500ms delay comes after the print of the 2 spaces.

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

Where:

  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • 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 RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Full MPLAB XC8 code:

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


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

8 thoughts on “Interfacing DS1307/DS3231 RTC with PIC MCU | MPLAB Projects”

  1. Why didn’t you use an I2C LCDs in that project like you did in the other project version in MIkroC? Is there any problem with I2C LCDs? Is there any problem with the library that you share on your site?

    I’m doing a project with that measures pressure and show the value on a LCD with a PIC16F877A and I’ve been searching for some library or example code for MPLab that show continuous data like you did in this project, but I couldn’t find some that works well.

  2. Dear Sir, can you please guide how can we improvise time based multiple relay operation in this code. Means we can be able to set multiple on & off time for relay. Thanks a lot for this tutorial.

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