Real time clock with PIC16F877A, SSD1306 OLED and DS1307

This post shows how to build a real time clock using PIC16F877A microcontroller and DS1307 RTC chip where time and date are displayed on an OLED display with SSD1306 controller (128×64 Pixel). With the help of a 3V coin cell battery the DS1307 keeps the time running even if the main power source is cut. The compiler used in this project is CCS PIC C, hardware circuit diagram and Proteus simulation are below.

Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC16F877A with SSD1306 OLED display
Real time clock using PIC16F877A and DS1307 RTC – CCS C

The SSD1306 OLED used in this project is configured to work in I2C mode, some SSD1306 OLED boards may require a small hardware modifications (to select between SPI mode or I2C mode) such as soldering, placing jumpers …

To simplify project circuit, the SSD1306 display and the DS1307 RTC chip share the same I2C bus, always the PIC16F877A microcontroller talks with one device only because their I2C slave addresses are different.

Hardware Required:

  • PIC16F877A microcontroller
  • SSD1306 OLED display (128×64 Pixel)
  • DS1307 RTC  —  datasheet
  • 32.768KHz crystal oscillator
  • 8 MHz crystal oscillator
  • 2 x 22pF ceramic capacitor
  • 3 x 10k ohm resistor
  • 2 x push button
  • 3V coin cell battery
  • 5V power source
  • Breadboard
  • Jumper wires

The Circuit:
The following image shows project circuit diagram.

PIC16F877A DS1307 SSD1306 OLED display real time clock circuit

(All grounded terminals are connected together)

The two push buttons B1 and B2 are for setting time and date. Button B1 is connected to pin RB0 (#33) and B2 is connected to pin RB1 (#34). Internal pull-ups for PORTB pins are enabled in the code.

In this project the PIC16F877A microcontroller runs with crystal oscillator of 8MHz.

The Code:
The C code below is for CCS C compiler, it was tested with version 5.051.

To be able to compile the C code below with no error, a driver for the SSD1306 OLED display is required, it’s name is SSD1306.C, its download link is below:
SSD1306 OLED display driver

after the download, add the driver file to the project folder or CCS C compiler drivers folder.

The DS1307 RTC and the SSD1306 OLED display share the same I2C bus, DS1307 I2C address is 0xD0 and SSD1306 display I2C address is 0x7A.

CCS C code:

PIC16F877A + SSD1306 OLED + DS1307 RTC Proteus simulation:
The video below shows the simulation of the project with Proteus ISIS. Note that simulation circuit is not the same as actual hardware circuit, project hardware circuit is shown above.

Proteus simulation file download (for version 8.6 and higher):
PIC16F877A + SSD1306 OLED + DS1307

Leave a Reply

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