VGA Clock with PIC18F46K22 MCU and DS3231 RTC

This PIC project shows how to make real time clock & calendar (RTCC) using PIC18F46K22 microcontroller and DS3231 RTCC module. Time & date are displayed on VGA monitor and they may be set with two push buttons connected to the PIC18F46K22 MCU.
As an addition to time and date read/set, the PIC18F46K22 MCU works as a VGA driver by generating the required signals with resolution of 160×96 pixel and 1-bit color (black and white).
This project builds a VGA clock.

CCS C compiler is used in this project.
VGA: Video Graphics Array.

To see how to interface the PIC18F46K22 MCU with VGA monitor, visit this post:
Interfacing PIC18F46K22 MCU with VGA Monitor – Graphics Test Example

The DS3231 RTC has built-in temperature sensor with resolution of 0.25 °C and accuracy of ±3 °C. Chip temperature also will be printed on the VGA monitor.

Hardware required:

  • PIC18F46K22 microcontroller
  • VGA monitor
  • DS3231 board   —->  DS3231 datasheet
  • 2 x push button
  • 2 x 68 ohm resistor
  • 220 ohm resistor
  • VGA connector
  • 3V coin cell battery
  • 5V source
  • Breadboard
  • Jumper wires

VGA clock with PIC18F46K22 MCU and DS3231 RTC

VGA Clock with PIC18F46K22 and DS3231 circuit:
The following image shows project circuit 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 DS3231 RTC board.

PIC18F46K22 VGA monitor DS3231 RTC circuit

All the grounded terminals are connected together, don’t forget the negative terminal of the 5V source!

HSYNC (horizontal synchronization) pin of the VGA connector (pin #13) is connected to PIC18F46K22 microcontroller pin RC2 (#17) through 68 ohm resistor.
VSYNC (vertical synchronization) pin of the VGA connector (#14) is connected to PIC18F46K22 microcontroller pin RC0 (#15) through 68 ohm resistor.

Color pins (R: red, G: green, B: blue) of the VGA connector (respectively #1, #2 and #3) are connected together in order to get white and black colors, they are connected to PIC18F46K22 microcontroller pin RD7 (#30) through 220 ohm resistor.

Pins 5, 6, 7, 8 and 10 of the VGA connector are connected to ground.

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

VGA Clock with PIC18F46K22 and DS3231 code:
The C code below is for CCS C compiler, it was tested with version 5.083.

The following CCS C code requires 3 libraries:
The first library is for generating the required VGA signals (VSYNC, HSYNC and video). Its full name (with extension) is VGA.c, download link is below:
VGA Library for CCS C compiler

The 2nd one is graphics library. This library helps us to easily print texts and draw shapes, its full name is GFX_Library.c, download link is the one below:
Graphics library for CCS C compiler:

The third library is a driver for the DS3231 RTC, its full name is: DS3231.c:
DS3231 CCS C driver

After the download of the 3 library files, add all of them to CCS C project folder.

The previous 3 libraries are included in the C code as shown below:

The VGA display is used with resolution of 160×96 pixel, the height is set using this line (the width is always fixed to 160 pixel):

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

void rtc_print(): displays time, date and day of the week on the screen. This function calls the previous one for displaying 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 = 128 (==> 1 tick every 8 microseconds ==> 62500 x 8 = 500000 us = 500 ms).

int8 edit(char 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 my hardware circuit test:

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