Interfacing PIC18F46K22 microcontroller with DS1307 RTC

This post shows how to build a simple real time clock/calendar using PIC18F46K22 microcontroller and DS1307 RTC chip where time and date are displayed on 20×4 LCD screen and they could be set using two push buttons connected to the PIC18F46K22 microcontroller.
The compiler used in this project is CCS PIC C.

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • 20×4 LCD screen
  • DS1307 RTC    —->    datasheet
  • 32.768 kHz crystal oscillator
  • 2 x 10k ohm resistor
  • 330 ohm resistor
  • 2 x push button
  • 10k ohm variable resistor or potentiometer
  • 3V coin cell battery
  • 5V power source
  • Breadboard
  • Jumper wires

Interfacing PIC18F46K22 with DS1307 and 20×4 LCD circuit:
The following image shows project circuit schematic diagram.

PIC18F46K22 DS1307 RTC and 20x4 LCD with set buttons

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

SCL and SDA pins of DS1307 are respectively connected to PIC18F46K22 pins RC3 (#18) and RC4 (#23) which are hardware I2C module pins (MSSP1).

The 20×4 LCD screen is connected to the PIC18F46K22 microcontroller as follows:
RS —> RD0 pin
RW —> RD1 pin
E  —> RD2 pin
D4 —> RD3 pin
D5 —> RD4 pin
D6 —> RD5 pin
D7 —> RD6 pin
VSS, 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 PIC18F46K22 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing PIC18F46K22 with DS1307 and 20×4 LCD C code:
The following C code is for CCS C compiler, it was tested with version 5.051.

To be able to compile the C code below, a driver (library) for DS1307 RTC is required, its full name (with extension) is DS1307.C, download link is the one below:
DS1307 CCS C driver

After the download, add it to project folder or CCS C drivers folder (for example C:\Program Files (x86)\CCS\Drivers).

For more information about DS1307 driver for CCS C compiler, visit the following post:
DS1307 RTC driver for CCS C compiler

I2C Bus (using I2C1 module) is initialized in the code as shown below at clock frequency of 100kHz:

Functions used in the code:
int1 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).

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

Rest of code is described through comments.

Full CCS C code:

The following video shows Proteus simulation of this project (simulation circuit is not the same as real hardware circuit, project hardware circuit is shown above):

Proteus simulation file download:
PIC18F46K22 + DS1307 RTC + 20×4 LCD


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