[GPS_Standard] PIC program to check validity of GPS fix

Charles Stackhouse cstack14478 at yahoo.com
Wed May 29 18:18:38 EDT 2013


While collecting the parts to assemble VE2ZAZ’S GPS-DERIVED 10MHZ FREQUENCY STANDARD, I notice that the 1 pps output of my Axiom GPS receiver was active even when the GPS link was broken. To monitor the quality of the GPS fix, I wrote 2 PIC programs which seem to be working fine. John Drew VK5DJ has already done this so I used his article as a starting point (I couldn't find his Basic source code so I wrote my own.

I will add the 12F628 unit to my frequency standard. Maybe this work will be of interest to others on this list.


The first program uses an 8 pin PIC 12F675 to monitor the serial data from the GPS receiver and lights up a red,  yellow or green LED to indicate "No Lock",  "2D Lock", or "3D lock". It is fairly simple. The code written in PBP is listed below:

'****************************************************************
'*  Name    : GPS_fix3.BAS                                      *
'*  Author  : Charles L. Stackhouse, W2GN                       *
'*  Notice  : Copyright (c) 2013                          *
'*          : All Rights Reserved                               *
'*  Date    : 5/28/2013                                         *
'*  Version : 1.0                                               *
'*  Notes   :This program checks GPS signal for validity of     *
'*          :   position lock.                                  *
'*          :Written in PICBasicPro                             *

'*          :Red led=zero lock   (unacceptable)                 *
'*          :Yellow led= 2D lock (acceptable, not ideal)        *
'*          :Green led=3D lock   (best result available         *
'*          :Position lock is read from NMEA $GPSGA sentence    *
'*          :  where  a number follows the first comma.   *
'*          :   1 = zero lock                                   *
'*          :   2 = 2D lock   (acceptable, not ideal)           *
'*          :   3 = 3D lock   (best result available)           *
'*          :8Hz crystal oscillator                             *
'*          :Pin assignments                                    *
'*          :    GP0 = Red led    (pin 7)                       *
'*          :    GP1 = Yellow led (pin 6)                       *
'*          :    GP2 = Green led  (pin 5)                       *
'*          :    GP3 = GPS pin    (pin 4)Connects to GPS Module * 
'*          :               RS232 out pin(turn MCLR off in con- *
'*          :               figuration fuses)                   *

'*          :GPS serial output is at RS232 levels;either a level*
'*          :  converter circuit or a 22k serial resistor needed*
'*          :  to interface to PIC                              * 
'****************************************************************
'pic 12f675 WITH 8 MHz across pins 2,3 (also put 20pf from these pins to ground
'Configuration fuses ....
'  @Device PIC12F675,WDT_ON,PWRT_ON,PROTECT_OFF,MCLR_OFF,BOD_OFF

'-----[ Front-end setup ]--------------------------------------------------
DEFINE OSC 8        'use 8MHz crystal for oscillator
ansel=0             'set ADC inputs as digital
cmcon=7             'turn comparators off
TRISIO=%001000      'all ports output except GPIO.3

' -----[ I/O Definitions ]-------------------------------------------------
gps_pin     var     GPIO.3      ;Connects to GPS Module RS232 out pin
Red_led     var     GPIO.0      ;lock=1, i.e. No lock
Yellow_led  var     GPIO.1      ;lock=2, i.e. 2D lock (acceptable, not ideal)
Green_led   var     GPIO.2      ;lock=3, i.e. 3D lock (best result available)
 
' -----[ GPS Constants ]---------------------------------------------------
gps_baud    con     16572    ;GPS serial baud rate at 4800,N inverted

'------[ Variables  ]------------------------------------------------------
satellites var word
lock var byte    '1=zero lock, 2=2D 3=3D
x  var byte      'for use in for..next loop
  
'test LEDs routine
 for x = 1 to 5
   Red_LED = 1
   Yellow_Led = 1
   Green_led = 1
   pause 250
   Red_LED = 0
   Yellow_Led = 0
   Green_led = 0
   pause 250
   next x
   
Red_led = 0: Yellow_led= 0: Green_led = 0    'initialize LEDs to off
 
START:
'-----------Get NMEA data from GPS-----------------------------------
SERIN2 gps_pin,16572,[WAIT("GPGSA,A"),skip 1,DEC1 lock] 
  
'--------Lock indicator LEDs routine----------------------------------
if lock = 1 then 
        Red_led =1
else
        Red_led =0
Endif 
if lock = 2 then 
        Yellow_led =1
else
        Yellow_led =0
Endif 
if lock = 3 then 
       Green_led =1
else
        Green_led =0
Endif 
       
pause 2000       
GOTO start

END
'*************************************************
I can email the hex code if anyone is interested and wants to program a 12F675.


The second program uses a PIC 16F628 , 3 colored LEDs, and an LCD to do the same thing but also reads out the quality of the GPS lock on the LCD and also the number of satellites in use (not just seen).The code is below:


'****************************************************************
'*  Name    : GPS_fix2c.BAS                                     *
'*  Author  : Charles L. Stackhouse, W2GN                       *
'*  Notice  : Copyright (c) 2013                                *
'*          : All Rights Reserved                               *
'*  Date    : 5/28/2013                                         *
'*  Version : 1.0                                               *
'*  Notes   :This program checks GPS signal for validity of     *
'*          :   position lock.                                  *
'*          :Written in PICBasicPro                             *
'*          :Position lock is read from NMEA $GPSGA sentence    *
'*          :  where a number follows the first comma.          *
'*          :   1 = zero lock                                   *
'*          :   2 = 2D lock   (acceptable, not ideal)           *
'*          :   3 = 3D lock   (best result available)           *
'*          :8Hz crystal oscillator                             *
'*          :Red led=zero lock   (unacceptable)                 *
'*          :Yellow led= 2D lock (acceptable, not ideal)        *
'*          :Green led=3D lock   (best result available         *
'*          :Number of satellites in use is read from GPGCA     *
'*          :   sentence word 7                                 *
'****************************************************************
'PicBasicPro default LCD hookup
'     4 bit data bus with DB4-DB7 connected to PORTA.0 to PORTA.3
'     Register Select to PORTA.4
'     Enable to PORTB.3

'GPS serial output is at RS232  levels; either a level converter circuit
'     or a 22k serial resistor needed to interface to PIC

'16F628A  at 8 MHz  
'At programming,manually set config fuses to OSC = HS, MCLR = reset

' LCD should be connected as follows:
' LCD   PIC
' DB4   PortA.0
' DB5   PortA.1
' DB6   PortA.2
' DB7   PortA.3
' RS    PortA.4 (add 4.7K pullup resistor to 5 volts)
' E     PortB.3
' RW    Ground
' Vdd   5 volts
' Vss   Ground
' Vo    20K potentiometer (or ground)
' DB0-3 No connect

'PIC pins
'           pin 1       LCD pin 13
'           pin 2       LCD pin 14
'           pin 3       LCD pin 4 ; pullup to +5v with 10k resistor 
'           pin 4       pullup to +5v with 10k resistor
'           pin 5       ground
'           pin 6       Red LED anode (LED cathode to ground wia 470 ohm resistor)
'           pin 7       Yellow LED anode (") 
'           pin 8       Green LED anode (")
'           pin 9       LCD pin 6
'           pin 10      To GPS serial output (4800,N,8,1,None) via 22k resistor
'           pin 11      No connection
'           pin 12      No connection
'           pin 13      No connection
'           pin 14      +5 volts (bypass to ground with 0.1uF capacitor)
'           pin 15      8 MHz crystal to pin 16 (20pF capacitor to ground)
'           pin 16      "
'           pin 17      LCD pin 11
'           pin 18      LCD pin 12

'-------[  Setup  ]--------------------------------------------------
DEFINE OSC 8 
cmcon = 7               'PortA = digital I/O   
trisA = 0               'PORTA pins are outputs;Serin2 sets B.4 to input
trisB = $00000000       'PORTB pins are outputs

'------[ I/O Definitions ]-------------------------------------------------
gps_pin         var  PORTB.4      ;Connects to GPS reciver RS232 out pin
Red_led      var  PORTB.0      ;lock=1, i.e. zero lock
Yellow_led   var  PORTB.1      ;lock=2, i.e. 2D lock (acceptable, not ideal
Green_led    var  PORTB.2      ;lock=3, i.e. 3D lock (best result available)
 
'------[ GPS Constants ]---------------------------------------------------
gps_baud    con     16572    ;GPS serial baud rate at 4800,N inverted

'------[ Variables  ]------------------------------------------------------
satellites var byte
lock var byte    '1=zero lock, 2=2D 3=3D
x  var byte      'for use in for..next loop

'------[ test LEDs at startup ]-------------------------------------------
 for x = 1 to 5
   Red_LED = 1
   Yellow_Led = 1
   Green_led = 1
   pause 250
   Red_LED = 0
   Yellow_Led = 0
   Green_led = 0
   pause 250
   next x
Red_led = 0: Yellow_led= 0: Green_led = 0    'initialize LEDs to off

Pause 1000                 'Let LCD initialize
LCDOUT $FE, 01             'clear screen, cursor to position 1
LCDOUT "Is GPS locked?"
Pause 1000

START:
'-----------[ Get NMEA data from GPS ]---------------------------------
SERIN2 gps_pin,16572,[WAIT("GPGSA,A"),skip 1,DEC1 lock] 

SERIN2 gps_pin,16572,[WAIT("$GPGGA,"),skip 31, wait(","),skip 1, wait(","),_
    DEC2 SATELLITES]

'------------ [ OUTPUT ROUTINE ]----------------------------------------
LCDOUT  $FE,$01     'clear screen
PAUSE 10
LCDOUT  $FE,$01,DEC2 satellites," SATELLITES"

'-------- [Lock indicator LEDs routine ]----------------------------------
if lock = 1 then 
        Red_led =1
        LCDOUT $FE,$C0, "   ZERO-LOCK "
Endif        
if lock = 2 then 
        Yellow_led =1
        LCDOUT $FE,$C0, "   2D-LOCK"
else
        Yellow_led =0
Endif 
if lock = 3 then 
       Green_led =1
       LCDOUT $FE,$C0, "   3D-LOCK"
else
        Green_led =0
Endif 
       
pause 2000       
GOTO start

END


'********************************************************************************

I can email the hex code also if anyone is interested. 

73, W2GN
cstack14478 at yahoo.com


More information about the GPS_Standard mailing list