PIC18F46K22 with SSD1306 OLED and DS1307 RTC | mikroC Projetcts

This tutorial shows how to make a simple real time clock & calendar using PIC18F46K22 microcontroller and DS1307 RTC chip.
In this project time and date are displayed on SSD1306 OLED screen (128×64 pixel) and they could be set with two push buttons connected to the PIC18F46K22.
MikroC PRO for PIC compiler is used in this project.

In this project the SSD1306 OLED is configured to work in I2C mode, make sure that your display is configured to work in I2C mode, some displays need jumper placing or some soldering.

To see how to interface PIC18F46K22 MCU with SSD1306 OLED display (I2C mode), take a look at the following project:
Interfacing PIC18F46K22 with SSD1306 OLED display | mikroC Projects

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • SSD1306 OLED display
  • DS1307 RTC    —->    datasheet
  • 32.768 kHz crystal oscillator
  • 2 x 10k ohm resistor
  • 2 x push button
  • 3V coin cell battery
  • 5V power source
  • Breadboard
  • Jumper wires

PIC18F46K22 with SSD1306 OLED and DS1307 RTC circuit:
The following image shows project circuit schematic diagram.

SSD1306 OLED DS1307 PIC18F46K22 circuit

All the grounded terminals are connected together.

The PIC18F46K22 microcontroller has 2 hardware I2C modules (MSSP1 and MSSP2 modules).
In this project I2C1 module is used with SDA1 on pin RC4 (#23) and SCL1 on pin RC3 (#18). The SDA1 pin of the MCU is connected to the SDA pin of the display and the SCL1 pin of the MCU is connected to the SCL pin of the display.
The reset pin of the display is connected to pin RD4 (#27) of the microcontroller.

The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the device is 0x7A.

The SSD1306 OLED and the DS1307 RTC share the same I2C bus, SCL and SDA pins of the DS1307 RTC are also connected to SCL1 and SDA1 pins of the PIC18F46K22.

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).

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

PIC18F46K22 with SSD1306 OLED and DS1307 RTC C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.

To be able to compile the C code below with no error, a driver for the SSD1306 OLED display is required, its full name (with extension) is SSD1306OLED.C, download link is the one below:
SSD1306 OLED mikroC library

for more information about this driver, visit the following post:
SSD1306 OLED display library for mikroC compiler | mikroC Projects

Also, another library for the DS1307 RTC is required, its full name (with extension) is: DS1307.C, download link is the one below:
DS1307 mikroC library

and for more details about it, visit this topic:
DS1307 RTC Library for mikroC Compiler | mikroC Projects

after the download of the 2 library files, add both of them to mikroC project folder.

The connection of the two push buttons and the SSD1306 OED reset pin are defined in the C code as shown below:

The drivers of the SSD1306 OLED and the DS1307 RTC are included in the code using the 2 lines below:

Functions used in the code:
char debounce (): this function is for button B1 debounce, returns 1 if button is debounced.

void dow_print(): displays day of the week (Monday, Tuesday …) on the LCD.

void rtc_print(): displays time, date and day of the week on the LCD. This function calls the previous one for displaying the day of the week.

void wait(): this function is just a delay of 500 ms except that it can be interrupted by the two push buttons (B1 and B2). In this function Timer0 module is used to count the 500 milliseconds, it is used as 16-bit timer with prescaler = 16 (==> 1 tick every 8 microseconds ==> 62500 x 8 = 500000 us = 500 ms).

char edit(char x_pos, char y_pos, char parameter): this function is for setting the real time clock, returns the edited parameter.

Rest of code is described through comments.

Full mikroC code:

The following picture shows a protoboard circuit of the project:

PIC18F46K22 with DS1307 RTC and SSD1306 OLED display


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