Real time clock/calendar using PIC16F887 and DS3231 – CCS C

Real time clock using PIC16F887 and DS3231

The datasheet of DS3231 RTC (real time clock) says that it is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperature compensated crystal oscillator (TCXO) and crystal. The DS3231 is much better than the DS1307 which means that it is a very good choice for persons who sell real time clock products.
The DS3231 RTC comes with an internal oscillator which means there is no need for a 32.768KHz crystal oscillator.

This topic shows the interfacing of the DS3231 RTC with PIC16F887 microcontroller with full adjustment of time and date parameters.
Like the DS1307, the DS3231 uses I2C protocol which uses two lines SCL and SDA. The PIC16F887 has a hardware I2C module where the SCL and SDA pins are mapped to RC3 and RC4 respectively.

Related Projects:
Interfacing DS1307/DS3231 RTC with PIC MCU | MPLAB Projects

Hardware Required:

  • PIC16F887 microcontroller
  • DS3231 (or DS3232) RTC — Datasheet
  • 16×2 LCD screen
  • 2 x push button
  • 10K ohm variable resistor
  • 2 x 10K ohm resistors
  • 0.1µF capacitor (decoupling capacitor needed for the DS3231)
  • 3V coin cell battery
  • 5V voltage source
  • Protoboard
  • Jumper wires

Interfacing DS3231 with PIC16F887 microcontroller circuit:

PIC16F887 DS3231 circuit

As we can see in the circuit there is a 16×2 LCD to display time and date, this LCD is connected to PORTD. The SCL and SDA pins of the DS3231 are connected to SCL (RC3) and SDA (RC4) pins of the PIC16F887 microcontroller. Two pull-up resistors of 10K are needed for the SCL and SDA lines, if these two resistors are not connected to whole circuit will not work at all and the LCD may not display any thing.
The two buttons which are connected to pins RB0 and RB1, these buttons are used to set the time as well as the date as shown in the videos below.
The 3V cell battery is used as a backup to keep time and date running in case of main power failure. The circuit can work without this battery but its pin (#14) has to be grounded.
The internal oscillator of the PIC16F887 is used and MCLR pin function is disabled.

Interfacing DS3231 with PIC16F887 microcontroller CCS C code:
The C code below is for CCS PIC C compiler (tested with version 5.051).
Please read the DS3231 datasheet to understand the code.
In CCS C it is easy to initialize the I2C module of the PIC microcontroller using the following line:
#use I2C(master, I2C1, FAST = 100000)
Where:
master: set the microcontroller to the master mode
I2C1: use first I2C module (PIC16F887 has only one module)
FAST = 100000 : set the speed to 100KHz

The DS3231 works with BCD format only and to convert the BCD to decimal and vise versa I used the following lines (example for minute variable):
minute = (minute >> 4) * 10 + (minute & 0x0F);                                  // Convert BCD to decimal
minute = ((minute / 10) << 4) + (minute % 10);                                     // Convert decimal to BCD
The full code is as the one below.

The following video shows the interfacing of PIC16F887 with the DS3231 in real hardware circuit:

And the following video shows Proteus simulation:

PIC16F887 and DS3231 RTC Proteus simulation file download:
Download


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

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