View Full Version : GPS clock timing !
  
bethr
- 1st July 2008, 18:34
Dear Forum:
I'd like to build a small Clock with GPS time-sync, look, I'm using the Darrel instant interrups to drive the Timer 1 clock, it works great ! (Thanks Darrel !), but when I tried to read the GPS data (over Pin RB1) there is a lot of missed/corrupted bytes in my buffer, something like this:
http://farm4.static.flickr.com/3038/2627934849_8e62561a62.jpg
the first lcd line is the RMC message data (corrupted in this case) and the second line display the  RTC registers, (plese note tha my LCD was broken so the lower line is wrong )
this is the code
'''''''''''''' INCLUDES ''''''''''''''''''''''''''''''''''''	
	DEFINE OSC 4 
	INCLUDE "DT_INTS-18.bas"
	INCLUDE "ReEnterPBP-18.bas"
	INCLUDE "Elapsed_INT-18.bas"  
	INCLUDE "LCDPICDEM2.BAS"		' it runs on PIDEM2 PLUS Board
''''''''''''' VARIABLES ''''''''''''''''''''''''''''''''''''	
		j    var byte
		rmc  var byte[100]	' this gets RMC data
		led1 var  portb.2
		b0   var byte
		adcon1 =$07
		trisa.2 = 0
		trisa.4 = 1
		trisb.1 = 1
		trisb.2 = 0
		trisb.0 = 1
		trisc.2 = 0
	    porta.2 = 0
		gps var portb.1
	 	spk    var portc.2
''''''''''''' CODE ''''''''''''''''''''''''''''''''''''	
	
init:	
	ASM
	INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
	        INT_Handler   TMR1_INT,  _ClockCount,   PBP,  yes
	        endm
	INT_CREATE                 
	ENDASM
	T0CON = %10011010
	@    INT_ENABLE  TMR1_INT      
	GOSUB ResetTime
	GOSUB StartTimer           
	hours = 11 : minutes = 30: seconds = 0
    
Start:
	LCDOUT $FE,1                    ' Initialize LCD
	PAUSE  200	
	lcdout $fe,2,"Starting ..." : pause 500
Main:
 
	serin2 gps,188,1000,no_gps,[wait("MC"),str rmc\15\10]	'15 bytes only for testing () wait for RMC message
 
	lcdout $fe,$c0,"Time : ",dec2 hours,":",dec2 minutes,":",dec2 seconds
 
	lcdout $fe,2,"$",str rmc\15 :pause 500						' these are the bytes as captured !
	goto main
no_gps:
	lcdout $fe,2,"Np Gps stream ": pause 500
	goto  main
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''' 
 
my first idea to fix it,was stop the timer just before get the gps data stream, like this:
	gosub stoptimer
	serin2 gps,188,1000,no_gps,[wait("MC"),str rmc\15\10]	'15 bytes only for testing () wait for RMC message
	gosub starttimer
using above lines my  LCD looks like this:
http://farm4.static.flickr.com/3274/2627933863_9669a34f7c.jpg
looks good !!!
but here, I have a problem with the time !, this make delay on the RTC, If I just was updated my clock register each ten minutes or so, that work out very well, but I want to keep my RTC all the time, (for logging timed-stamp GPS locations by example).
I knew this is a time issue (software generated timing "serin2"), another plan is to use the internal UART build in my pic (i'm using a18f452), but my GPS only outputs TTL levels this create interfacing problems, how can I get working this project?
Thanks !!!
skimask
- 1st July 2008, 18:46
I'd like to build a small Clock with GPS time-sync, look, I'm using the Darrel instant interrups to drive the Timer 1 clock, it works great ! (Thanks Darrel !), but when I tried to read the GPS data (over Pin RB1) there is a lot of missed/corrupted bytes in my buffer, something like this:
Probably the interrupts interrupting the SERIN statements.  Disable the interrupts before going into SERIN, reenable after coming back.
I knew this is a time issue (software generated timing "serin2"), another plan is to use the internal UART build in my pic (i'm using a18f452), but my GPS only outputs TTL levels this create interfacing problems, how can I get working this project?
Normal, Inverted RS232, the PICs capabilities, all discussed fairly well in the PBP manual.
Darrel Taylor
- 1st July 2008, 19:33
Since you're using a GPS, you already have a clock that's more accurate than anything you could possibly add to a PIC.
Just read the GPS time to use as a time stamp in the logs.
OR, a single transistor with 2 resistors can invert the serial data to the USART, if you still want the internal clock.
<br>
Darrel Taylor
- 1st July 2008, 20:45
OOPs.
I just looked up the 188 from your SERIN2 statement.
That's 4800 baud TRUE levels.
You can hook it up to the USART's RX pin. No transistor needed.
hth,
bethr
- 1st July 2008, 23:43
Thanks by answer Skimask, Darrel !
you both Are right, there is no interfacing conflicts, but unfortunately I always get the ' no gps ' string on the LCD (with or without running the RTC), I mean, I have made changes for using Hserin instead serin2,
'''''''''''''' INCLUDES ''''''''''''''''''''''''''''''''''''	
	DEFINE OSC 4 
	INCLUDE "DT_INTS-18.bas"
	INCLUDE "ReEnterPBP-18.bas"
	INCLUDE "Elapsed_INT-18.bas"  
	INCLUDE "LCDPICDEM2.BAS"		' it runs on PIDEM2 PLUS Board
	
	DEFINE HSER_TXSTA 20h
	DEFINE HSER_RCSTA 90h
	DEFINE HSER_BAUD 4800
''''''''''''' VARIABLES ''''''''''''''''''''''''''''''''''''	
		j    var byte
		rmc  var byte[100]	' this gets RMC data
		led1 var  portb.2
		b0   var byte
		adcon1 =$07
		trisa.2 = 0
		trisa.4 = 1
		trisb.1 = 1
		trisb.2 = 0
		trisb.0 = 1
		trisc.2 = 0
		trisc.7 = 1
	    porta.2 = 0
		gps var portb.1
	 	spk    var portc.2
''''''''''''' CODE ''''''''''''''''''''''''''''''''''''	
	
init:	
	ASM
	INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
	        INT_Handler   TMR1_INT,  _ClockCount,   PBP,  yes
	        endm
	INT_CREATE                 
	ENDASM 
	@    INT_ENABLE  TMR1_INT      
	GOSUB ResetTime
	GOSUB StartTimer           
	hours = 11 : minutes = 30: seconds = 0
    
Start:
	LCDOUT $FE,1                    ' Initialize LCD
	PAUSE  200	
	lcdout $fe,2,"Starting ..." : pause 500
Main:  
	Hserin 1500,no_gps,[wait("MC"),str rmc\15\10]	'15 bytes only for testing () wait for RMC message
  
	lcdout $fe,$c0,"Time : ",dec2 hours,":",dec2 minutes,":",dec2 seconds
 
	lcdout $fe,2,"$",str rmc\15 :pause 500						' these are the bytes as captured !
	goto main
no_gps:
 
	lcdout $fe,1,"No Gps stream "
	lcdout $fe,$c0,"Time : ",dec2 hours,":",dec2 minutes,":",dec2 seconds
	goto  main
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''' 
 
http://img68.imageshack.us/img68/2804/dsc01281zz5.jpg
currently for debug I'm using  an GPS software simulator:
http://img117.imageshack.us/img117/2066/dsc01284ss7.jpg
and I'm sure that it works, this is a debug rs232 window (hyperterm)
http://img300.imageshack.us/img300/3294/dsc01283bl1.jpg
that was taken on Sub-DB9 connector of the picdem2 board
there is something missing in the set-up hardware UART ???
Thanks again !
Darrel Taylor
- 2nd July 2008, 00:00
Try it like this...
	DEFINE HSER_TXSTA 24h ' BRGH=1
	DEFINE HSER_RCSTA 90h
	DEFINE HSER_BAUD 4800
        DEFINE HSER_CLROERR 1 'Hser clear overflow automatically
bethr
- 2nd July 2008, 00:42
It's alive !!
it works Darrel, I'm wondered why this bit must be set in hi speed if my bps is low ! (4800 bps)
bit 2 BRGH: High Baud Rate Select bit
Asynchronous mode:
1 = High speed
0 = Low speed
and now it looks run pretty ! (with RTC running ), look this:
http://img48.imageshack.us/img48/3690/dsc01285ce6.th.jpg
Now, I'll keep it running for a while, I hope the RTC doesn't have long-delays. any-way as you said, always there is way to update ths RTC register with GPS ones !!
BTW currently I'm not using UART interrupts,  but . . .
. . . what if . . .
I enable it ?, this give more reliable data incoming form the GPS line ? or it can be dangerous for the RTC health ?
http://bp1.blogger.com/_QMOZfcg3_aw/RgtPmaRP5-I/AAAAAAAAABE/F-HUHA5zFBw/s400/thanks-for-the-info.jpg
Darrel Taylor
- 2nd July 2008, 01:13
It's alive !!
WooHoo!
I'm wondered why this bit must be set in hi speed if my bps is low ! (4800 bps)
With a 4mhz OSC, I wouldn't consider 4800 "low".
In the datasheet there's a couple tables that show the baud rates vs. OSC freq with BRGH either 1 or 0. With BRGH=0 @ 4mhz, the highest baudrate in the table is 2400. With BRGH=1 it's 19200.
It may still work with BRGH=0, but it's close to the edge, so why not just make it BRGH=1.
I think the HSER_CLROERR 1 may have had the biggest effect.
The GPS will just spit out the data at regular intervals. It doesn't wait for the PIC to be ready. And at 4800 baud it can send a byte every 2ms. So when the program sits in a 500 ms pause, it may receive a couple hundred bytes before the program gets around to looking at the data. There's only a 2 byte buffer in the USART so it causes an Overflow that must be cleared before it will start receiving data again.
BTW currently I'm not using UART interrupts,  but . . .
. . . what if . . .
I enable it ?, this give more reliable data incoming form the GPS line ? or it can be dangerous for the RTC health ?
HSERIN is not compatible with USART interrupts.
You would need to parse the incomming data 1 byte at a time with your own handlers.
It's much easier to use HSERIN.
hth,
bethr
- 3rd July 2008, 17:44
Yes you're Right, I think that on this way the gps clock work well, there is no problem with it, I have it running & all looks nice !
 
The program doesn't  anything important (just led blinking), and when BTN rb0 is pushed, it gets info from GPS module and update my RTC registers  just as i wanted !
 
'BETHR
'''''''''''''' INCLUDES ''''''''''''''''''''''''''''''''''''	
	DEFINE OSC 4
	INCLUDE "DT_INTS-18.bas"
	INCLUDE "ReEnterPBP-18.bas"
	INCLUDE "Elapsed_INT-18.bas"  
	INCLUDE "LCDPICDEM2.BAS"		' it runs on PIDEM2 PLUS Board
	DEFINE HSER_TXSTA 24h 			' BRGH=1
	DEFINE HSER_RCSTA 90h
        DEFINE HSER_CLROERR 1 			' Hser clear overflow automatically
	DEFINE HSER_SPBRG 51
''''''''''''' VARS ''''''''''''''''''''''''''''''''''''	
		led   var Portb.2
		timeh var byte
		timem var byte
		times var byte
		day	  var byte
		mon   var byte
		yr    var byte
		stat  var byte
		gps   var Portb.1 
		TRISB.2 = 0
		TRISB.0 = 1 
    	         Adcon1 = $07
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''  
	
init:	
	ASM
	INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
	        INT_Handler   TMR1_INT,  _ClockCount,   PBP,  yes
	        endm
	INT_CREATE                 
	ENDASM 
	@    INT_ENABLE  TMR1_INT      
	Gosub ResetTime
	Gosub StartTimer           
	hours = 12 'after reset we always star @12:00:00
    
Start:
	lcdout $fe,1,"GPS Clock "
    Pause 500
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''' 
Main: 
	While 1
'do something [  something more useful than this :-)  ]
		While (portb.0=1)
			Toggle led
			Lcdout $fe,2,"Date : ",dec2 day,47,dec2 mon,47,dec2 yr
			Lcdout $fe,$c0,"Time : ",dec2 hours,":",dec2 minutes,":",dec2 seconds
		 	Pause 200
		Wend
' if i press rb0 btn update my RTC
		Gosub Update_rtc
	Wend
 
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''' 
 
''' SUBS ''''
Update_rtc:
'look 4 stream
	lcdout $fe,1,"Updating..."
	Hserin 500,no_gps,[WAIT("MC,"),dec2 TIMEH,dec2 TIMEM,dec2 TIMES,SKIP 5,_
	STAT,skip 30,dec2 day,dec2 mon,dec2 yr]	
'check 4 Valid GPS if so, then update my RTC
	if stat = "A" then
		hours=timeh - 5 : minutes = timem: seconds = times
'no gps so don't update 
	else
		lcdout $fe,1,"Bad Fix" : pause 500
  	endif
	return
'np gps or bad sintax
no_gps:   
	lcdout $fe,1,"No Gps stream" : pause 500
	return
''' END SUBS '''' 
I've had to replace my broken LCD also !!
http://farm4.static.flickr.com/3091/2633527287_769077137f.jpg
BIG TNX !
earltyso
- 3rd July 2008, 18:21
Hi there Bethr,
I am looking at purchasing a garmin LVS gps for a robot project and I was just wondering what kind of gps you are using? Cost? etc....
skimask
- 3rd July 2008, 18:26
Hi there Bethr,
I am looking at purchasing a garmin LVS gps for a robot project and I was just wondering what kind of gps you are using? Cost? etc....
If it has an NMEA serial output line, then you can get usable data out of it.
www.sparkfun.com has a few GPS modules already mounted, ready to use.
bethr
- 3rd July 2008, 20:11
Hi there Bethr,
I am looking at purchasing a garmin LVS gps for a robot project and I was just wondering what kind of gps you are using? Cost? etc....
currently I have two modules
this:
http://farm4.static.flickr.com/3016/2633846213_02261cf08b.jpg
http://www.trimble.com/embeddedsystems/lasseniq.aspx?dtID=overview
and this one , a low profile asian embedded device:
http://farm4.static.flickr.com/3089/2634667402_716d9ac05b.jpg
both were removed from used equipment, but I know that the trimble module have a cost near to 50 usd (versus qty)
and the other one near to 30 usd 
both devices has NMEA ascii output @t vdd levels, trimble vdd=3v3 and asian device vdd=5v
Cheers !!
 
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.