PIC18F46K22 with DS1307 RTC and Nokia 5110 LCD | mikroC Projects

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 printed on Nokia 5110 (3310) monochrome LCD (84×48 pixel resolution) and they may be set with two push buttons connected to the PIC18F46K22 MCU.
The compiler used in this project is MikroElektronika mikroC PRO for PIC.

To see how to interface PIC18F46K22 MCU with Nokia 5110 LCD using mikroC compiler, visit this post:
Interfacing PIC MCU with Nokia 5110 LCD | mikroC Projects

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • Nokia 5110 LCD screen
  • DS1307 RTC              —->    datasheet
  • 32.768 kHz crystal oscillator
  • AMS1117 3V3 voltage regulator
  • 10 uF capacitor
  • 100 nF ceramic capacitor
  • 2 x 10k ohm resistor
  • 5 x 3.3k ohm resistor
  • 5 x 2.2k ohm resistor
  • 2 x push button
  • 3V coin cell battery
  • 5V power source
  • Breadboard
  • Jumper wires

PIC18F46K22 microcontroller with DS1307 RTC and Nokia 5110 LCD

PIC18F46K22 with Nokia 5110 LCD and DS1307 circuit:
The following image shows project hardware circuit schematic diagram.

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). SDA1 and SCL1 pins of the PIC18F46K22 MCU are respectively connected to SDA and SCL pins of the DS1307 RTC.

PIC18F46K22 DS1307 RTC Nokia 5110 3310 LCD

All the grounded terminals are connected together.

The Nokia 5110 LCD module which is shown in the circuit diagram has 8 pins (from left to right): RST (reset), CE (chip enable), DC (or D/C: data/command), Din (data in), Clk (clock), VCC (3.3V), BL (back light) and Gnd (ground).

The Nokia 5110 LCD works with 3.3V only (power supply and control lines). The LCD module is supplied with 3.3V which comes from the AMS1117 3V3 voltage regulator, this regulator steps down the 5V into 3.3V (supplies the LCD controller PCD8544 with regulated 3V3).

All PIC18F46K22 microcontroller output pins are 5V, connecting a 5V pin to the Nokia 5110 LCD may damage its controller circuit!
To connect the PIC18F46K22 to the LCD module, I used voltage divider for each line which means there are 5 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.

So, the Nokia 5110 LCD pins are connected to PIC18F46K22 MCU as follows (each one through voltage divider):
RST (reset) pin is connected to pin RD0 (#19),
CE (chip enable) pin is connected to pin RD1 (#20),
DC (data/command) pin is connected to pin RD2 (#21),
DIN (data in)  pin is connected to pin RD3 (#22),
CLK (clock) pin is connected to pin RD4 (#27),

VCC and BL are connected to AMS1117 3V3 regulator output pin and GND is connected to circuit ground (0V).

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

PIC18F46K22 with Nokia 5110 LCD and DS1307 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 project C code, a driver for the Nokia 5110 LCD is required, its full name (with extension) is NOKIA5110.C, download link is the one below:
Nokia 5110 LCD library for mikroC compiler

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 LCD pins with the microcontroller are defined in the C code as shown below:

The drivers of the Nokia 5110 LCD 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 parameter): this function is for setting the real time clock, returns the edited parameter.

Rest of code is described through comments.

Full mikroC code:


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