Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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);
/* 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;
unsigned
and Eastern
unsigned
unsigned
unsigned
unsigned
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;
j++;
}
tempString[j] = '\0';
/* Check if string we collected is the $GPGGA message */
if(tempString[3] == 'G' && tempString[4] == 'G' && tempString[5] ==
'A') {
/*
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
ddmm.mmmm,n
dddmm.mmmm,e
q
ss
y.y
a.a,M
g.g,M
t.t
iiii
*CC
:
:
:
:
:
:
:
:
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 */
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
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;
init_comms();