After interfacing the PIC16F887 microcontroller with NEO-6M GPS module, now I’m going to show how to build a GPS clock with local time where time and date are displayed on 20×4 LCD screen.
In this project the microcontroller receives time and date data from the GPS module (the GPS module receives data from satellites) and print it on the 20×4 LCD. The compiler used in this project is mikroElektronika mikroC PRO for PIC. — GPS: Global Positioning System —
Related Projects:
Last PIC16F887 project, I made a simple interfacing of this microcontroller with NEO-6M GPS module in order to get position, altitude, time (UTC), date, speed, number of satellites and course, project page link is below. — UTC: Universal Time Coordinated —
Interfacing PIC microcontroller with GPS module | mikroC Projects
Hardware Required:
To build this project, the following components are required.
- PIC16F887 microcontroller
- NEO-6M GPS module
- 20×4 LCD screen
- 10k ohm variable resistor (or potentiometer)
- 330 ohm resistor
- 5V source
- Breadboard
- Jumper wires
GPS Clock using PIC16F887 and NEO-6M module circuit:
The following image shows the circuit schematic of the project.
(All grounded terminal are connected together)
The NEO-6M GPS module board has 4 pins connected as follows:
GND (Ground) pin connected to circuit GND
TX pin goes to PIC16F887 RX (RC7) pin (pin #26)
RX pin is not connected (there is no need to send data from the microcontroller to the GPS module)
VCC pin goes to circuit +5V pin.
The 20×4 LCD screen is used to display the real time clock time and date where:
RS —> RD0 pin
E —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, 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 PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.
GPS Clock using PIC16F887 and NEO-6M module C code:
The following C code is for mikroC PRO for PIC compiler.
In this project I used a small GPS library for mikroC compiler, this library decodes NMEA sentence that the microcontroller receive from the GPS module, therefore getting time and date is more easier.
GPS Library for CCS C compiler downloaded link is the one below, library full name is “GPS_mikroc.c“:
mikroC GPS Library download
after the download, add the library file (GPS_mikroc.c) to project folder.
As known, the GPS module always gives UTC time and date which may need to be converted to our time zone (for example UTC + 1). To do the conversion of time we can use mikroC PRO for PIC time library (built-in library). But we’ve to add the library header to the project folder, the library header named timelib.h, it is located in examples folder of mikroC installation folder (for example, C:\Program Files (x86)\MikroElektronika\mikroC PRO for PIC\Examples\Other\Time).
Or you can download it from the following link:
timelib.h
To be able to convert the UTC standard time into our time zone (local time), first we’ve to convert the UTC time into UNIX time (also known UNIX Epoch time or POSIX time ) which is the number of seconds that have elapsed since 00:00:00 UTC, 1 January 1970.
After we get the UNIX Epoch time we add our time offset to it (plus or minus), for example for UTC + 1 time zone we add 3600 seconds, for UTC – 5 we add -18000 seconds and so on.
After that we converter the ‘new’ UNIX time into our zone time and date.
Functions used in the code:
UART1_Data_Ready(): returns 1 if a character has been received by the MCU.
GPS library functions:
GPSRead(): returns 1 if the GPS data (NMEA sentences) have been received and parsed with successful.
GPSHour(): returns hours from the decoded GPS data (0 – 23)
GPSMinute(): returns minutes (0 – 59)
GPSSecond(): returns seconds (0 – 59)
GPSDay(): returns day of month (1 – 31)
GPSMonth(): returns month (1 – 12)
GPSYear(): returns year with 2 digits (0 – 99)
mikroC Time library functions:
First we need to save the UTC time and date (in order to convert it to UNIX time) to a variable of type TimeStruct (structure type), I named this variable: utc_time. This variable is a collection of 7 variables, 6 of them are required (the 7th variable is utc_time.wd which is day of the week, not needed here):
utc_time.hh, utc_time.mn, utc_time.ss, utc_time.md, utc_time.mo and utc_time.yy .
utc_time.mn has to be between 1 and 12. utc_time.yy is 4-digit year (2018, 2019, 2020 …).
Now we can convert the UTC time to UNIX time with the following function where the converted UNIX time is saved to unix_time variable of type long (4 byte signed number):
unix_time = Time_dateToEpoch(&utc_time) ;
Returning from the UNIX time to our zone time and date is also simple, here an other variable of type TimeStruct is used, I used a variable named: my_time. This variable is also a collection of 7 variables:
my_time.hh, my_time.mn, my_time.ss, my_time.md, my_time.mo, my_time.yy and my_time.wd.
The last variable is day of the week between 0 and 6 (0 for Monday).
my_time.md is day of month (1-31)
my_time.mo is the month (1-12)
Our zone time and date are stored to the variable my_time using the function:
Time_epochToDate(unix_time, &my_time) ;
Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | /************************************************************************************** GPS Clock with local time using PIC16F887 microcontroller and NEO-6M GPS module C Code for mikroC PRO for PIC compiler Internal oscillator used @ 8MHz Configuration words: CONFIG1 = 0x2CD4 CONFIG2 = 0x0700 This is a free software with NO WARRANTY. http://simple-circuit.com/ ***************************************************************************************/ // LCD module connections sbit LCD_RS at RD0_bit; sbit LCD_EN at RD1_bit; sbit LCD_D4 at RD2_bit; sbit LCD_D5 at RD3_bit; sbit LCD_D6 at RD4_bit; sbit LCD_D7 at RD5_bit; sbit LCD_RS_Direction at TRISD0_bit; sbit LCD_EN_Direction at TRISD1_bit; sbit LCD_D4_Direction at TRISD2_bit; sbit LCD_D5_Direction at TRISD3_bit; sbit LCD_D6_Direction at TRISD4_bit; sbit LCD_D7_Direction at TRISD5_bit; // End LCD module connections #define time_offset 3600 // define a clock offset of 3600 seconds (1 hour) ==> UTC + 1 // include GPS mikroC library #include <GPS_mikroc.c> // include mikroC time library #include <timelib.h> // mikroC time library variables long unix_time; TimeStruct utc_time, my_time; // other variables char txt[3]; char* codetxt_to_ramtxt(const char* ctxt) { static char text_[18]; char i; for(i = 0; text_[i] = ctxt[i]; i++); return text_; } void print_wday(short wday) { switch(wday) { case 0: lcd_out(2, 6, codetxt_to_ramtxt(" MONDAY ")); break; case 1: lcd_out(2, 6, codetxt_to_ramtxt(" TUESDAY ")); break; case 2: lcd_out(2, 6, codetxt_to_ramtxt("WEDNESDAY")); break; case 3: lcd_out(2, 6, codetxt_to_ramtxt("THURSDAY ")); break; case 4: lcd_out(2, 6, codetxt_to_ramtxt(" FRIDAY ")); break; case 5: lcd_out(2, 6, codetxt_to_ramtxt("SATURDAY ")); break; default: lcd_out(2, 6, codetxt_to_ramtxt(" SUNDAY ")); } } // convert byte to string void byte_to_str(unsigned short nub, char *str) { str[0] = nub / 10 + '0'; str[1] = nub % 10 + '0'; str[2] = '\0'; } // main function void main() { OSCCON = 0X70; // set internal oscillator to 8MHz Lcd_Init(); // initialize LCD module Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off Lcd_Cmd(_LCD_CLEAR); // clear LCD lcd_out(1, 2, codetxt_to_ramtxt("GPS CLOCK (UTC+1)")); lcd_out(3, 3, codetxt_to_ramtxt("TIME: : :")); lcd_out(4, 3, codetxt_to_ramtxt("DATE: - -20")); UART1_Init(9600); while(1) { if (UART1_Data_Ready() == 1) if(GPSRead()) { // read UTC time and date and save them to utc_time structure utc_time.hh = GPSHour(); // hour 0-23 utc_time.mn = GPSMinute(); // minutes 0-59 utc_time.ss = GPSSecond(); // seconds 0-59 utc_time.md = GPSDay(); // month day 1-31 utc_time.mo = GPSMonth(); // month 1-12 utc_time.yy = GPSYear() + 2000; // four digits year (2018, 2019 ...) // get unix time unix_time = Time_dateToEpoch(&utc_time) ; // add the offset (in seconds) to the unix time unix_time += time_offset; // get the local time Time_epochToDate(unix_time, &my_time) ; // print day of the week print_wday(my_time.wd); // print time byte_to_str(my_time.hh, txt); lcd_out(3, 9, txt); byte_to_str(my_time.mn, txt); lcd_out(3, 12, txt); byte_to_str(my_time.ss, txt); lcd_out(3, 15, txt); // print date byte_to_str(my_time.md, txt); lcd_out(4, 9, txt); byte_to_str(my_time.mo, txt); lcd_out(4, 12, txt); byte_to_str((my_time.yy - 2000), txt); lcd_out(4, 17, txt); } } } // End of code |
The result of this project should be as shown in the following video where PIC16F877A is used:
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
sorry but it throws me an error, the strcmp variable does not have its identifier declared, I already tried with some types but I cannot solve it, could you please advise me
How to see source code of function Time_dateToEpoch(&utc_time) ;
Good morning, I’m working on this and it seems interesting. But I keep getting undeclared identifier for GPS Read, utc_time, GPS Hour etc