Parsing NMEA $GPGGA $GPRMC with a 28X1 to display TIME

Simmicht

Senior Member
Parsing GPS NMEA $GPGGA $GPRMC with a 28X1 to display TIME

For those wanting a start on how to read NMEA strings from a GPS, I found this works well enough. It does not get every string, but it will get every second or third string.

If you remove the comments the sertxd's you can see which NMEA string was parsed.

Code:
'Written on 21-Feb-2009
'For PicAxe28X1  and Serial GMouse GPS
'GPS Serial input on pin 18 ser RX
'LED on pin 28


SYMBOL Hours 		= B1
SYMBOL Minutes 		= B3
SYMBOL Seconds 		= B5
SYMBOL MilliSeconds	= W6


'Blink LED
loop1:high 7:pause 10 :low 7:pause 1000


main:
hsersetup B4800_4, %00 			‘baud 4800 at 4MHz
hserin [2000,nodata],0,80,("$") 	‘wait for $ then receive 80 bytes into sp
ptr = 0 					‘reset scratchpad pointer

gettime: 					'GPGGA GPRMC sentences have the time HHMMSS.sss
B0=@ptrinc : if B0<>"G" then mainx
B0=@ptrinc : if B0<>"P" then mainx
B0=@ptrinc : if B0<>"G" then lookforR
B0=@ptrinc : if B0<>"G" then mainx
B0=@ptrinc : if B0<>"A" then mainx
'sertxd ("$GPGGA",cr,lf)
goto TXControllerInfo

lookforR:
if B0<>"R" then mainx
B0=@ptrinc : if B0<>"M" then mainx
B0=@ptrinc : if B0<>"C" then mainx
'sertxd ("$GPRMC",cr,lf)


'============================================================
TXControllerInfo:
B0 = @ptrinc				'skip comma after NMEA Sentence ID

B0 = @ptrinc - 48				'get tens of hour value
Hours = B0 * 10 + @ptrinc - 48	'add units of the hour value

B2 = @ptrinc - 48				'get tens of minutes value 
Minutes = B2 * 10 + @ptrinc - 48	'add units of the minutes value

B4 = @ptrinc - 48				'get tens of the seconds value
Seconds = B4 * 10 + @ptrinc - 48	'get units of the seconds value

B0 = @ptrinc				'skip decimal

B6 = @ptrinc - 48				'get 1st digit of milliseconds
B7 = @ptrinc - 48				'get 2nd digit of milliseconds
B8 = @ptrinc - 48				'get 3rd digit of milliseconds
MilliSeconds = B6*10 + B7*10 + B8	'calc milliseconds , NOTE the tricky math here with B6*10 not *100 as you would think
sertxd (#Hours,":",#Minutes,":",#Seconds,".",#MilliSeconds," UTC",cr,lf)


goto loop1

nodata:
sertxd ("no data",cr,lf)
goto loop1
mainx:
;sertxd ("x")
goto loop1
 
Last edited:

OGMrStinky

New Member
If all you want is time from a GPS receiver, or even if you want position as well, all you need is the GGA string. Many of the GPS modules available have configurable NEMA outputs. Or, you could include "GP" in the wait of your hserin statement, read the next three bytes and only parse strings that have GGAs. To add speed and heading, just add RMC in a different parse routine.
Just some thoughts!
 

MPep

Senior Member
If ZDA is present, then use that.

OR use RMC (like so this): SERIN GpinI,Gbd,("$GPRMC,"),b1,b2,b3,b4,b5,b6
 

Simmicht

Senior Member
HSERIN vs SERIN

It seems that the format for HSERIN vs SERIN are different. Correct me if I am wrong, the HSERIN can only accept a single character in the qualifier. Probably due to the high speed processing that this command has to achieve.
 

Simmicht

Senior Member
Code correction

Looking at the code, it should have the following correction.
The setup needs only be done once.

Code:
hsersetup B4800_4, %00 			‘baud 4800 at 4MHz
main:
hserin [2000,nodata],0,80,("$") 	‘wait for $ then receive 80 bytes into sp
 

n0167956

New Member
hey

i tried the code above with and without the offered correction but it did not work.

i am attempting to connect the gps (ls40eb) chip to a picaxe and then make the whole thing wireless using the xbee chips.

any help would be greatly appreciated

thanks
 

hippy

Ex-Staff (retired)
@ n0167956 : Welcome to the PICAXE forum.

The original code given and subseqent versions using HSERIN etc will only work with the PICAXE-28X1 and 40X1, not with an 18X. I suspect you got a "Syntax error in this line" error or similar - it always helps to be specific rather then "it did not work" which leaves people guessing what did not work, whether the code would not compile, or it did but the results were not as expected.

To parse NMEA strings using an 18X you will need to use SERIN, in a manner as shown by MPep in post #3. A Forum Search should also return results for other NMEA interacing projects and discussions.
 

Simmicht

Senior Member
Temperature controlled Error

This code has been extracting the time from the NMEA string for a few months now and works flawlessly until it doesn't. Let me explain, during the day the PicAxe starts throwing parse errors for few hours, until normal operation resumes. I raw dumped the incoming RS232 data and i can see the MNEA string has lots of random characters in it. As this setup is in my shed and the Australian summer is heating up, I wondered if temperature was affecting the 28X1 as it does not have a crystal resonator, connected but is relying on the internal oscillator.
So today around lunch time it did the usual, it started to read random characters from the GPS. I sprayed the PicAxe lightly with freeze spray and viola, it functioned correctly almost instantly.

So the question to the smart people is, will the PicAxe 28X1 be more temperature stable with a crystal resonator, or should I adjust the baud as described in the hsersetup section of the manual?

Note that I am using the Setfreq 8m to speed the whole processing of the string anyway.
 

Simmicht

Senior Member
Calibfreq

I found interesting things under calibfreq and also OSCTUNE.
I think I should just use a resonator to be really sure.

Quoting the manual below ...

On these chips it is also possible to ‘calibrate’ this frequency. This is an advanced
feature not normally required by most users, as all chips are factory calibrated to
the most accurate setting. Generally the only use for calibfreq is to slightly adjust
the frequency for serial transactions with third party devices. A larger positive
value increases speed, a larger negative value decreases speed. Try the values -4 to
+ 4 first, before going to a higher or lower value.
Use this command with extreme care. It can alter the frequency of the PICAXE
chip beyond the serial download tolerance - in this case you will need to perform
a ‘hard-reset’ in order to carry out a new download.
The calibfreq is actually a pseudo command that performs a ‘poke’ command on
the microcontrollers OSCTUNE register (address $90).
 

MartinM57

Moderator
There really should be a general warning about that reminds people to always use an external resonator or crystal if you want reliable RS232 comms (unless you want to chase the right internal oscillator frequency with OSCTUNE/CALIBFREQ)

Resonator or crystal solutions will give:
- the right frequency first time
- far better temperature stability.

Unfortunately that precludes the choice of some PICAXES to some solutions, e.g. 14M, 20X2 etc that don't have connections for external frequency sources.
 

hippy

Ex-Staff (retired)
will the PicAxe 28X1 be more temperature stable with a crystal resonator, or should I adjust the baud as described in the hsersetup section of the manual?
The frequencies of all internal oscillators will shift with temperature and/or supply voltage and a crystal or resonator will be more stable. For the 28X1 this should be reasonably easy to add.

The 28X1 with internal oscillator should only start to get unreliable with serial comms at around 85'C or when operated below 2V5. Until then the accuracy should be 2%. Serial needs a baud rate accuracy of 6% to work so it may be that the GPS baud rate / operating speed is also not what it should be. Cooling the PICAXE brings that to a better accuracy, the combined baud rate error drops below 6% and everything works again.

Assuming you cannot control temperature there are four things you can do -

1) Use an external crystal or resonator
2) Operate the PICAXE near 3V5
3) Adjust OSCTUNE ( CALIBFREQ )
4) Adjust HSERSETUP baud rate

Fitting the crystal is the easy option. Operating at 3V5 may not gain a lot when operating further from 25'C.

If the GPS baud rate is inaccurate it may be possible to tweak HSERSETUP baud rate so it works at normal temperatures and is still within the 6% margin at elevated temperatures. You can determine the GPS baud rate by adjusting the HESROUT baud rate; there will be a low and high baud rate within which it works, the GPS baud rate will be about midway. With the PICAXE HSERSETUP baud rate set near the lowest/highest baud rate it should keep working longer even as temperature changes - you'll have to test which works best.

It may be a good idea to tweak the baud rate this way even when using an external crystal as it will better cope with any varying GPS baud rate as temperature changes.
 
Last edited:

inglewoodpete

Senior Member
Check the firmware revision.

If your 28x1 has really old firmware (A2 or earlier), it may suffer from a problem that was corrected in firmware A.3 and later.

The original 28x1s and 40x1s were not 'centred' particularly well, being slightly off frequency. If your chip is one of these, it may be particularly sensitive, easily drifting further off with relatively small temperature changes. (On the other hand, heating one of these chips may bring the on-chip resonator back into line - I don't know which way the error was:rolleyes:)

Can you advise us of the firmware version of your chip?
 

Simmicht

Senior Member
I will check the version of the 28X1 tonight and post back to here. It was purchased more the 12 months ago.
The GPS is in a sealed box in the full sun, it needs to be outside to 'see' the satellites.
I have a 8MHz resonator, so I will fit that tonight as well.

Can I still use setfreq commands once the 8MHz resonator is added? Not that I will need to.
 

BCJKiwi

Senior Member
Check out the setfreq command in Manual2 (p185 in ver 6.9).
There is an 'e' required in front of the m8 - i.e. em8 - for the external resonator.
 

Simmicht

Senior Member
Cool

@BCJKiwi thanks for the setfreq e8m tip.
@inglewoodpete I checked the firmware and it is A.3 so it looks like the resonator is needed to compensate for the temperature fluctuation. Some rainy weather so it will take a week before i know that it has fixed the problems.
 
Last edited:

hippy

Ex-Staff (retired)
Some rainy weather so it will take a week before i know that it has fixed the problems.

You really need to show the PICAXE failing without the resonator and that adding it fixes the problem. To get the chip a bit warmer you may be able to use a hair dryer.
 

Simmicht

Senior Member
Holidays too so, User Acceptance Testing may have to wait.

@Hippy Holidays too so, it may have to wait.

However I did solder in the resonator 8MHz. I made a mistake, soldered it in bakwards, so I had to unsolder and correct it.
Sertxd ("HELLO",cr,lf) shows up at the standard 4800 terminal.
If I add a Setfreq m8 then the SerTxd is at 9600 as it should be.

:(How can I tell the 28X1 is using the 8MHz resonator?
 

hippy

Ex-Staff (retired)
How can I tell the 28X1 is using the 8MHz resonator?

CALIBFREQ will have no effect if the PICAXE is using the external crystal. Run the following program ...

#Picaxe 28X1
#Terminal 9600
#No_Data
#No_Table

SetFreq M8
For b0 = 0 To 31
b1 = -b0
CalibFreq b1
Pause 2000
SerTxd(" Working -", #b0, CR, LF )
Next

The serial output will get progressively more corrupted. Replace with SETFREQ EM8 and run again, the PICAXE will maintain 9600 baud regardless.
 

Simmicht

Senior Member
Thanks Hippy, however with the setfreq M8 it gets to about -6 and stops receiving at 9600.
With setfreq EM8 , only works at 4800 and once again stops receiving when at -6 :confused:

I am assuming that the resonator is not working.

Thanks Hippy for the code snippet.
 

hippy

Ex-Staff (retired)
Seems like your resonator isn't wired up right - I wasn't sure how you meant, "I made a mistake, soldered it in backwards, so I had to unsolder and correct it"; there's no real 'backwards' to a resonator.

If it's a three-leg type ( recommended ), the middle leg goes to 0V, the outer legs go to leg 9 and leg 10 of the 28X1, either way round.

If it's two leg type ( or an actual crystal ), the legs go to leg 9 and leg 10 of the 28X1, either way round, but you have to add two capacitors ( usually around 33pF ) from both resonator legs to 0V.

Occasionally you may have to add a series R to the leg 10 connection and/or a parallel R between leg 9 and 10 - It's usually easier to find a resonator which works without them, most seem to and I've never needed either.
 

Simmicht

Senior Member
Ok then mine has ZTT on it and for some reason i assumed the T was 0v and the other 2 when to the Occ pins.:rolleyes:

So I need to try again then.
Thanks
 

Simmicht

Senior Member
Finally Working

The test now shows the resonator is working, all all it took was to solder it in with all the pins in the right place...:D

So now i wait and see, off to the beach for a few days but I will get the SMS from my monitoring system if the "time" data stops flowing during the heat of the day.



Thanks to Hippy
 
Top