EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / problem interfacing gps with 8051

problem interfacing gps with 8051

|

Microcontroller › 8051 › problem interfacing gps with 8051

  • This topic has 3 replies, 3 voices, and was last updated 12 years, 7 months ago by Jason Jordon.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • April 2, 2013 at 11:24 am #2223
    sara
    Participant

    Hi,

     

    I have been interfacing gps modem with 8051 with correct connection as per your circuit diagram and followed the code given under EG labs.

     The program is given with comments which is very helpful but I am having difficulty in decoding the line in the given program , i.e

    if(check==69)… the program is not satisfying this condition and hence not going into the compare loop and not reading the latiitude and longitude values from the GPS receiver.

     

    Please clarify and help me out. Or provide me with an alternate program at the earliest possible..

     

    Thanks in advance.

     

     

    April 2, 2013 at 4:28 pm #9415
    AJISH ALFRED
    Participant

    Hi Sara,

    It would have been more easier for us to help you if you share the location of the code in this site.

     

    April 3, 2013 at 12:42 pm #9425
    sara
    Participant

    In this site u can go to EG labs >> MICROCONTROLLER >> 8051 >> How to interface gps with microcontroller 8051 >> code and you can find the code there..

     for convenience the program is as follows..

     

    /* Basic program to show latitude and longitude on LCD extracted from GPGGA statement */

    #include<reg51.h>

    #define port2 P2

    sbit rs = P1^0;

    sbit rw = P1^1;

    sbit e = P1^2;

    char info[70];

    char test[6]={“$GPGGA”};

    char comma_position[15];

    unsigned int check=0,i;

    unsigned char a;

    void receive_data();

    void lcd_latitude();

    void lcd_longitude();

     

    //DELAY FUNCTION

    void delay(unsigned int msec)

    {

                    int i,j ;

                    for(i=0;i<msec;i++)

                    for(j=0;j<1275;j++);

    }

     

    // LCD COMMAND SENDING FUNCTION

    void lcd_cmd(unsigned char item)

    {

                    port2 = item;

                    rs= 0;

                    rw=0;

                    e=1;

                    delay(1);

                    e=0;

                    return;

    }

     

    // LCD DATA SENDING FUNCTION

    void lcd_data(unsigned char item)

    {

                    port2 = item;

                    rs= 1;

                    rw=0;

                    e=1;

                    delay(1);

                    e=0;                                          

                    return;

    }

     

     // LCD STRING SENDING FUNCTION

    void lcd_string(unsigned char *str)

    {

                    int i=0;

                    while(str!=’’)

                    {

            lcd_data(str);

            i++;

            delay(10);

            }

            return;

    }

     

    // SERIAL PORT SETTING

    void serial()

    {

                    TMOD=0x20;      //MODE=2

                    TH1=0xfa;                 // 4800 BAUD

                    SCON=0x50  ;    // SERIAL MODE 1 ,8- BIT DATA ,1 STOP BIT ,1 START BIT , RECEIVING ON

                    TR1=1;           //TIMER START

    }

     

    void find_comma()

    {

                    unsigned int i,count=0;

                    for(i=0;i<70;i++)

                    {             

                                    if(info==’,’)

                                    {

                                                    comma_position[count++]=i;

                                    }

             }

    }

    void compare()

    { 

                    IE=0x00;                      //Interrupt disable

                    find_comma();     //Function to detect position of comma in the string

                    lcd_latitude();    //Function to show Latitude

                    lcd_longitude();   //Function to show Longitude

                    check=0;

                    IE=0x90;                                  //Interrupt enable

    }

    void receive_data()                         interrupt 4                                                                          

    {

                    info[check++]=SBUF;       //Read SBUF

                    if(check<7)                  //Condition to check the required data

            {

                                    if(info[check-1]!=test[check-1])

                                    check=0;

            }

                    RI=0;

    }

    void lcd_shape()                    //Function to create shape of degree

    {

                    lcd_cmd(64);

                    lcd_data(10);

                    lcd_data(17);

                    lcd_data(17);

                    lcd_data(10);

                    lcd_data(0);

                    lcd_data(0);

                    lcd_data(0);

                    lcd_data(0);

    }

     

    void lcd_latitude()                           //Function to display Latitude

    {

                    unsigned int c2=comma_position[1]; //Position of second comma

                    lcd_shape();

                    lcd_cmd(0x01);         // Clear LCD display

                    lcd_cmd(0x84);         //Move cursor to position 6 of line 1

                    lcd_string(“LATITUDE”);                //Showing Latitude

                    lcd_cmd(0xC0);                                 //Beginning of second line 

                    lcd_data(info[c2+1]);    

                    lcd_data(info[c2+2]);

                    lcd_data(0);                                        //Degree symbol

                    lcd_data(info[c2+3]);    

                    lcd_data(info[c2+4]);

                    lcd_data(info[c2+5]);

                    lcd_data(info[c2+6]);

                    lcd_data(info[c2+7]);

                    lcd_data(info[c2+8]);

                    lcd_data(info[c2+9]);

                    lcd_data(0x27);          //ASCII of minute sign(‘)

                    lcd_data(info[c2+10]);

                    lcd_data(info[c2+11]);

                    delay(250);

    }

     

    void lcd_longitude()

    {

                    unsigned int c4=comma_position[3];

                    lcd_cmd(0x01);            //Clear LCD display

                    lcd_cmd(0x84);            //Move cursor to position 4 of line 1

                    lcd_string(“LONGITUDE”);                           //Showing Longitude

                    lcd_cmd(0xC0);                                     //Begining of second line 

                    lcd_data(info[c4+1]);

                    lcd_data(info[c4+2]);

                    lcd_data(info[c4+3]);

                    lcd_data(0);

                    lcd_data(info[c4+4]);

                    lcd_data(info[c4+5]);

                    lcd_data(info[c4+6]);

                    lcd_data(info[c4+7]);

                    lcd_data(info[c4+8]);

                    lcd_data(info[c4+9]);

                    lcd_data(info[c4+10]);

                    lcd_data(0x27);               //ASCII of minute sign(‘)

                    lcd_data(info[c4+11]);

                    lcd_data(info[c4+12]);

                    delay(250);

    }

    void main()

    {

                    serial();

                    lcd_cmd(0x38);        //2 LINE, 5X7 MATRIX

                    lcd_cmd(0x0e);         //DISPLAY ON, CURSOR BLINKING

                    IE=0x90;

                    while(1)

                    {

                                    if(check==69)

                                    compare();

                    }

    }

     

     

    May 2, 2013 at 7:01 am #9606
    Jason Jordon
    Participant

    tell me if this one works (made on turbo c)

     

    /* 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 helpful defines */
    #define SPACE 0x20
    #define COMMA 0x2C
    #define MAXSIZE 100 /* GPS at most, sends 80 or so chars per message string. So set maximum to 100 */

    #include < stdio.h >
    #include < ctype.h > /* required for the isalnum function */
    #include < stdlib.h >
    #include < string.h >
    #include < conio.h >
    #include < math.h >
    #include < dos.h >
    #include "ibmcom3.h" /* for serial */

    /* Prototypes */
    void comm_setting(void); /* Set com port */
    void close_com(void); /* Close com port */

    int main(void) {

    unsigned char charRead; /* char read from COM port */
    unsigned char stringRead[MAXSIZE]; /* Buffer collects chars read 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 *pChar;
    unsigned char dummyChar;

    unsigned long utcTime, estTime; /* Coordinated Universal Time and Eastern Standard Time */
    unsigned long utcHour, estHour;
    unsigned long utcMinutes, estMinutes;
    unsigned long utcSeconds, estSeconds;
    unsigned char lastCommaPosition;

    float latitude;
    int latDegrees;
    float latMinutes;

    float longitude;
    int longDegrees;
    float longMinutes;

    FILE *gpsFile; /* Text file of GPS strings read */
    unsigned int j, k; /* dummy variable */
    unsigned int i; /* Number of chars read per GPS message string */
    unsigned int numLinesRead; /* 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 = charRead;
    do {
    charRead = com_rx();
    if( (charRead != '/0') && (isalnum(charRead) || isspace(charRead) || ispunct(charRead)) ) {
    i++;
    stringRead
    = 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] = '';

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

    /* Check if string we collected is the $GPGGA message */
    if(tempString[3] == 'G' && tempString[4] == 'G' && tempString[5] == 'A') {
    /*
    Found GPGGA string. It has 14 commas total. Its NMEA sentence structure is:

    $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 6 7
    0123456789012345678901234567890123456789012345678901234567890123456789012

    where:

    GPGAA : GPS fixed data identifier
    hhmmss.ss : Coordinated Universal Time (UTC), also known as GMT
    ddmm.mmmm,n : Latitude in degrees, minutes and cardinal sign
    dddmm.mmmm,e : Longitude in degrees, minutes and cardinal sign
    q : Quality of fix. 1 = there is a fix
    ss : Number of satellites being used
    y.y : Horizontal dilution of precision
    a.a,M : GPS antenna altitude in meters
    g.g,M : geoidal separation in meters
    t.t : Age of the defferential correction data
    iiii : Deferential station's ID
    *CC : 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] = '';
    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] = '';

    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] = '';
    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] = '';

    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();
    }

     

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Droplet1 December 12, 2025
  • What is involved to convert a small town to fiber optic? December 12, 2025
  • Help to Identify capacitor fault December 12, 2025
  • Dog fence help December 12, 2025
  • EEPROM not being written or read on dsPIC30F2010 December 12, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise