Introduction: JAL GPS Real Time Clock

On the Jallib forum, one of the members was looking for a solution to synchronize his PIC Microcontroller with some external clock. Several suggestions where given and one of them was to use the GPS time and GPS date using a GPS receiver.

So I created a JAL (Just Another Language) library for the NEO-6M GPS receiver to extract the GPS time and the GPS date from the received GPS data. I only implemented this feature and did not – yet - implement the extraction of the GPS location data.

If you develop a JAL library you also need to provide at least one sample file with it. The sample file that I created showed the GPS time and the GPS date on a PC using a serial connection. I thought it would be nice to make a real application with this GPS library so I created this JAL GPS Real Time Clock which synchronizes itself with the GPS time. For the display I used an SSD1306 that interfaces via an IIC bus with a PIC Microcontroller. You might think this is all straight forward but you need to do some extra work in order to make this a true Real Time Clock which I will explain further in this Instructable.

Step 1: Extracting the GPS Time and GPS Date From a GPS Receiver

The NEO-6M GPS Module uses a serial baudrate at 9600 baud to send all kind of GPS data. The transmission of this data is repeated frequently, about once every second. From this data, GPS location information can be extracted including the GPS time and GPS date. One of the strings, sent by the module starts with $GPRMC and contains the GPS time and GPS date information.

A typical $GPRMC string may look like this (the x is my GPS location data that I removed):

  • $GPRMC,180050.00,A,xxxx.xxxxx,N,xxxxx.xxxxx,E,0.151,,031121,,,A*7B

The elements of the string are separated by commas and the string ends with a carriage return and linefeed. The following information is part of this string:

  • 180050 representing the UTC time: 18 hours, 00 minutes, 50 seconds, the .00 are centiseconds but are not used by the GPS library
  • 031121 representing the date: 3rd of November 2021
  • 7B is a checksum which is the XOR of all characters between $ and *

This library looks for the $GPRMC string, extracts the GPS time and GPS date and verifies the checksum of the string. Two functions are provided by the library one for obtaining the GPS time and one for obtaining the GPS date. These functions return true if valid data was received. Since it takes time to retrieve the data from the GPS module and from the $GPRMC string, these functions can take several seconds to complete. This means that you cannot make a Real Time Clock using these functions. You could use a Real Time Clock (RTC) IC for this purpose but in this project I created this RTC in software.

Step 2: The Electronics

The electronics is quite simple, it consists of a PIC Microcontroller, an SSD1306 display and the NEO-6M GPS receiver. I built the electronics on a breadboard as shown in the picture.

The schematic diagram shows the design of this version. As already mentioned, you need the following main components for this project:

  • 1 PIC microcontroller like a 16F1823 but any PIC with sufficient RAM and ROM will do
  • 1 SSD1306 display. This version uses an IIC interface. There are versions with an SPI interface
  • 1 NEO-6M GPS receiver
  • 2 pull-up resistors of 4k7 for the IIC bus and 1 of 33k to pull the PIC reset input high

In order to make interfacing between the various components as simple as possible the whole circuit is running at 3.3 Volt. The PIC and the NEO-6M board can operate at 5 Volt – although the NEO-6M operates at 3.3 Volt - but I would not recommend it for the SSD1306 display. The SSD1306 would work on 5 Volt but it is then used outside of its specification, which will shorten the life span of the device or destroy it in the end.

Step 3: The Software

As mentioned earlier we need to make a Real Time Clock in software. For this I used Timer 1 of the PIC Microcontroller, which generates an interrupt when the timer overflows. When this overflow occurs, the time on the SSD1306 display needs to be updated via the IIC interface. This IIC transfer takes some time but must be completed before the next timer interrupt. Because of that I wanted the time between two timer interrupts to be as long as possible. I chose for a one second timing since we need that anyway to update the seconds on the display. In order to be able to do that with Timer 1 I had to lower the internal system clock of the PIC to 1 MHz. Since the NEO-6M module operates at a low baudrate of 9600 baud this still works. Communication at a baudrate of 115200 baud would not be possible when the PIC runs at 1 MHz.

So every second our Timer 1 updates the display, regardless of what the main loop is doing, and so regardless of how long it takes for the NEO-6M JAL library to process the GPS data. The main loop gets the GPS time and GPS date from the NEO-6M and uses that to synchronize the Timer 1 Real Time Clock. The clock time is synchronized once every minute and the clock date is synchronized once every hour. Because of this frequent synchronization, the internal system clock of the PIC is accurate enough for this application

The JAL source file of the application and the Intel Hex file for programming the PIC are attached.

Step 4: The Final Result

In this video the information as explained in this Instructable is presented again in a shorter format. The video also shows the operation of this JAL GPS Real Time Clock where you will see that it synchronizes with the GPS time and GPS date from the NEO-6M GPS Module after a serial connection is made between the PIC and the NEO-6M Module.

The initial value of the time of the clock is set to 00:00:00 (h:m:s) and the initial date to 2000-00-00 (yyyy-mm-dd). Note that the GPS date is only 2 digits, so 2000 is added to get the year 2021.

If you are interested in using the PIC microcontroller with JAL – a Pascal like programming language – visit the Just Another Language website.

Have fun making this Instructable and looking forward to you reactions and results.