Você está na página 1de 9

Reference: http://www.boondog.com/tutorials/gps/gps.

html
Turbo C code
Note: download C source file gps1_5.c rather than cutting and pasting from below.
/*

*/

FILE:
AUTH:
DESC:
REFS:
NOTE:

gps1_5.c
P.OH
Garmin EMap connected to COM1
Uses ibmcom serial libraries
To compile: tcc -ml gps1_5.c ibmcom3.obj

/* Defines required for serial i/o */


#define COM_PORT
1
/* Serial device connected to COM 1 */
#define SPEED
4800
/* baud rate = 4800 */
#define CR
0x0d
#define LF
0x0a
#define ESC
0x1b
#define BEEP
0x07
/* Some
#define
#define
#define
message
#include
#include
#include
#include
#include
#include
#include
#include

helpful defines */
SPACE
0x20
COMMA
0x2C
MAXSIZE
100
/* GPS at most, sends 80 or so chars per
string. So set maximum to 100 */
< stdio.h >
< ctype.h >
< stdlib.h >
< string.h >
< conio.h >
< math.h >
< dos.h >
"ibmcom3.h"

/* Prototypes */
void comm_setting(void);
void close_com(void);

/* required for the isalnum function */

/* for serial */
/* Set com port */
/* Close com port */

int main(void) {
unsigned char charRead;
unsigned char stringRead[MAXSIZE];
from GPS */
unsigned char tempString[MAXSIZE];
unsigned char timeString[12];
unsigned char latitudeString[11];
unsigned char latitudeCardinalString[3];
unsigned char longitudeString[12];
unsigned char longitudeCardinalString[3];
unsigned char
unsigned char

*pChar;
dummyChar;

/* char read from COM port */


/* Buffer collects chars read

unsigned
and Eastern
unsigned
unsigned
unsigned
unsigned

long utcTime, estTime;


Standard Time */
long utcHour, estHour;
long utcMinutes, estMinutes;
long utcSeconds, estSeconds;
char lastCommaPosition;

float
int
float

latitude;
latDegrees;
latMinutes;

float
int
float

longitude;
longDegrees;
longMinutes;

FILE

*gpsFile;

*/

unsigned int
j, k;
unsigned int i;
message string */
unsigned int numLinesRead;

/* Coordinated Universal Time

/* Text file of GPS strings read


/* dummy variable */
/* Number of chars read per GPS
/* Number of GPS strings read */

dummyChar = 'A'; pChar = &dummyChar;


gpsFile = fopen("gpsData.txt", "w");
printf("Initializing port...");
comm_setting();
printf("done/n");
numLinesRead = 0;
printf("Entering while loop.../n");
do {
charRead = com_rx();
/* read char from serial port */
if(charRead == '$') {
/* GPS messages start with $ char */
i = 0;
numLinesRead++;
stringRead[i] = charRead;
do {
charRead = com_rx();
if( (charRead != '/0') && (isalnum(charRead) ||
isspace(charRead) || ispunct(charRead)) ) {
i++;
stringRead[i] = charRead;
}
} while(charRead != CR);
/* By this point, a complete GPS string has been read so save it to
file */

/* Append the null terminator to the string read */


stringRead[i+1] = '\0';
/* Analyze string that we collected */
j = 0;
pChar = stringRead;
while(*(pChar+j) != COMMA) {
tempString[j] = *(pChar+j);

j++;
}
tempString[j] = '\0';
/* Check if string we collected is the $GPGGA message */
if(tempString[3] == 'G' && tempString[4] == 'G' && tempString[5] ==
'A') {

/*

Found GPGGA string.


sentence structure is:

It has 14 commas total.

Its NMEA

$GPGAA,hhmmss.ss,ddmm.mmmm,n,dddmm.mmmm,e,q,ss,y.y,a.a,z,g.g,z,t.t,iii*CC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0
1
2
3
4
5
7

|
6

0123456789012345678901234567890123456789012345678901234567890123456789012
where:

as GMT
sign
sign

*/

GPGAA
hhmmss.ss

: GPS fixed data identifier


: Coordinated Universal Time (UTC), also known

ddmm.mmmm,n

: Latitude in degrees, minutes and cardinal

dddmm.mmmm,e

: Longitude in degrees, minutes and cardinal

q
ss
y.y
a.a,M
g.g,M
t.t
iiii
*CC

:
:
:
:
:
:
:
:

Quality of fix. 1 = there is a fix


Number of satellites being used
Horizontal dilution of precision
GPS antenna altitude in meters
geoidal separation in meters
Age of the defferential correction data
Deferential station's ID
checksum for the sentence

pChar = stringRead;
/* Get UTC time */
j = 7; /* start of time field */
k = 0;
while(*(pChar+j) != COMMA) {
timeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
timeString[k] = '\0';
sscanf(timeString, "%ld", &utcTime);
utcHour = (utcTime/10000);
/* extract Hours from long */
utcMinutes = (utcTime - (utcHour*10000))/100; /* extract
minutes from long */

utcSeconds = utcTime - (utcHour*10000) - (utcMinutes*100); /*


extract seconds from long */
if(utcHour >= 4 && utcHour <= 23) estHour = utcHour - 4;
else estHour = utcHour + 20;
estMinutes = utcMinutes;
estSeconds = utcSeconds;
/* NB: %02ld formats long to print 2 chars wide, padding with 0
if necessary */
printf("%02ld:%02ld:%02ld UTC = %02ld:%02ld:%02ld EST", utcHour,
utcMinutes, utcSeconds, estHour, estMinutes, estSeconds);
/* Get lattitude: ddmm.mmmm */
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
latitudeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
latitudeString[k] = '\0';
sscanf(latitudeString, "%f", &latitude);
latDegrees = (int)(latitude/100);
latMinutes = (float)(latitude - latDegrees*100);
printf("/t%02d DEG/t%2.4f MIN", latDegrees, latMinutes);
/* Get lattitude Cardinal direction */
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
latitudeCardinalString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
latitudeCardinalString[k] = '\0';
printf(" %s", latitudeCardinalString);
/* Get longitude: dddmm.mmmm */
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
longitudeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
longitudeString[k] = '\0';
sscanf(longitudeString, "%f", &longitude);
longDegrees = (int)(longitude/100);

longMinutes = (float)(longitude - longDegrees*100);


printf("/t%03d DEG/t%2.4f MIN", longDegrees, longMinutes);
printf("/n");
} /* else not a GPGGA sentence */
fprintf(gpsFile, "%d: (%d) %s/n", numLinesRead, i, stringRead);
} /* otherwise not a $ character... so loop back until one arrives */
} while(!kbhit());
printf("Exiting...");
close_com();
/* Finished with serial port so close it */
fclose(gpsFile);
printf("done/n");
return (0);
} /* end of main */
void comm_setting(void) {
int

dummy;

dummy = com_install(COM_PORT);
if(dummy != 0) {
switch (dummy) {
case 1 : printf("Invaid port number/n");
break;
case 2 : printf("No UART fot specified port/n");
break;
case 3 : printf("Drivers already installed/n");
break;
default : printf("Err #%d/n", dummy);
break;
}
exit(1);
} com_raise_dtr();
com_set_speed(SPEED);
com_set_parity(COM_NONE, STOP_BIT_1);
}
void close_com(void) {
com_lower_dtr();
com_deinstall();
}

To compile, at the DOS prompt type tcc -ml gps1_5.c ibmcom3.obj. This of course assumes
that ibmcom3.obj is in the same directory as gps1_5.c. the -ml option invoke Turbo C's large
memory model. Running the executable (gps1_5.exe) will display the UTC time, the Eastern
Standard time (EST) equivalent and both latitude and longitude coordinates. Additionally, all
message strings output by your GPS receiver are saved into an ASCII file named gpsData.txt.

Code Description
begins by opening a file gpsData.txt which will save all GPS message strings in
ASCII. Next, the serial port is opened using a function prototype comm_setting() which
invokes functions found in the IBMCOM library.
gps1_5.c

A while loop is entered, where the statement charRead = com_rx(); serially reads a character
and checks if it begins with a dollar sign. If so, this indicates a new GPS message string has been
received and more characters are read until a carriage return (CR) is found.
holds the GPS message string that was serially read. If it is a $GPGGA message,
additional reading is done, where we know that commas separate geospatial data. sscanf is used
to extract numerical data from the ASCII characters.
tempString

Reference: http://www.microchip.com/forums/m696382.aspx

PIC 18F4580 +RS232+GPS (MPLAB C18


I'm working on a project consisting in receiving a frame from a GPS (4800 baud). via
a RS232 port using a PIC 18F4580 microcontroller on MPLAB C18
Step2 : The project consists in extracting (latitude, longitude, time) off a GGA frame
and speed off the RMC one.
This is a code "below" that should be only receiving frames of GPS (not for
extravting NMEA frames) in which i used 'getsUSART, openUSART functions' as
described in the book of c18 compiler. ... My question is how to check if it is
receiving the frames or not ? (by the way, i dont have access to an oscillo...so i
cannot check it with this means :s)
My Code :
#include <p18f4580.h>
#include <usart.h>
#include <sw_uart.h>
#include <stdlib.h>
#include <stdio.h>
#include <delays.h>
void closeUSART(void);
//void getsUSART ( char * gps , unsigned char 80 );

void main(void)
{
char gps[80]; // array for receiving frames
OpenUSART (USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 130); //4800bauds
TRISCbits.TRISC6 = 0 ; //
TRISCbits.TRISC7 = 1;
while(1){
getsUSART( gps, 80); // receiving frames
}
}
Re:16F690 serial communication tutorial - Thursday, July 21, 2011 1:48 PM

0
Right, I am closer.
I now get gibberish in hyperterminal. I have gone through a lot of the datasheet, and while I have
learned a lot, I am afraid I still do not understand some (most) of it.
I have set up my board to use a 14.7456 mghz crystal. Based on how the datasheet said to
calculate error (sec 12.2), this frequency was spot on for 9600 baud. I also changed the
calculation usart.h was making to come up with SPBRG to reflect the datasheet.
I have also added a few lines to set certain pins correctly based on other info I found in the
datasheet. However, there must be something I am missing since it is not working.
Thanks for all of your previous reply's and any other help you can give!
code -- main.c -----------------------------------------------#include <stdio.h>
#include <htc.h>
#include "usart.h"
/* A simple demonstration of serial communications which
* incorporates the on-board hardware USART of the Microchip
* PIC16Fxxx series of devices. */
__CONFIG(FOSC_XT, WDTDIS);
void main(void){
unsigned char input; //for receiving data from pc
INTCON=0;

// purpose of disabling the interrupts.

init_comms();

// set up the USART - settings defined in usart.h

/* setup chip for usart */


SPEN = 1;
TRISB6 = 1;
TRISB7 = 1;
ABDEN = 1;
SPBRGH = 23;
SPBRG = 23;
// Output text
while(1){
printf("\rT\n");
}
code -- usart.h -----------------------------------------#ifndef _SERIAL_H_
#define _SERIAL_H_
#define BAUD 9600
#define FOSC 14745600
#define NINE 0 /* Use 9bit communication? FALSE=8bit */
//#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define DIVIDER (((FOSC/BAUD)/64) -1)
#define HIGH_SPEED 1
#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif
#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif
#if defined(_16F87) || defined(_16F88)
#define RX_PIN TRISB2
#define TX_PIN TRISB5
#else

#define RX_PIN TRISC7


#define TX_PIN TRISC6
#endif
/* Serial initialization */
#define init_comms()\
RX_PIN = 1; \
TX_PIN = 1;
\
SPBRG = DIVIDER;
\
RCSTA = (NINE_BITS|0x90); \
TXSTA = (SPEED|NINE_BITS|0x20)
void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);
#endif

Você também pode gostar