#picaxe 20m2
symbol GpsTX = B.1
symbol GMTHours = $20
symbol GMTMinutes = GMTHours + 1
symbol GMTSeconds = GMTMinutes + 1
symbol GMTDay = GMTSeconds + 1
symbol GMTMonth = GMTDay + 1
symbol GMTYear = GMTMonth + 1
symbol GpsRecord = GMTYear + 1
symbol GpsRecordEnd = GpsRecord + 60
setfreq m16
for b0 = 1 to 30
pause 1000 ' wait for GPS to initialise
sertxd(" ",#b0)
next b0
' Wait for a GPRMC record - include comma for convenience
ReadGPS:
bptr = GpsRecord
serin GPSTX,T9600_16,("$GPRMC,"),_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc, @bptrinc,_
@bptr
#rem
sertxd(13,10,">",13,10)
for bptr = GpsRecord to GpsRecordEnd
if @bptr = 0 then
sertxd("<NULL>")
else
sertxd(@bptr)
endif
next bptr
#endrem
' process record
' field 1 is hhmmss.sss if valid
bptr = GpsRecord
for b2 = GMTHours to GMTSeconds
gosub ConvertTwoBytes
if b1 <> 0 then ReadGPS ' error
poke b2,b0
next b2
gosub GetNextField
if @bptrinc <> "A" then ReadGPS ' not valid data
for b0 = 0 to 6 ' skip 6 fields
gosub GetNextField
next b0
for b2 = GMTDay to GMTYear
gosub ConvertTwoBytes
if b1 <> 0 then ReadGPS ' error
poke b2,b0
next b2
bptr = GMTHours
sertxd(13,10,#@bptrinc,":",#@bptrinc,":",#@bptrinc," ",#@bptrinc,"/",#@bptrinc,"/",#@bptrinc)
goto ReadGPS
' convert two decimal character bytes into a number
ConvertTwoBytes:
b0 = 0
b1 = 1
if @bptr < "0" or @bptr > "9" then goReturn ' error
b0 = @bptrinc - "0"
if @bptr < "0" or @bptr > "9" then goReturn ' error
b0 = b0 * 10 + @bptrinc - "0"
b1 = 0
goReturn:
return
' find beginning of next field (after comma)
GetNextField:
do
if @bptrinc = "," then exit
loop until bptr > GpsRecordEnd
return