Sunday 15 July 2018

Interfacing GPS Module to 8051 Microcontroller

Aim:

A GPS module is a device that uses Global Positioning System to determine the location of a vehicle or person. GPS receivers are used to provide reliable navigation, positioning and timing services to the users at anytime and anywhere on the earth.  This Global positioning system uses 24 to 32 satellites to provide the data to the receivers. GPS has become very important for worldwide navigation and it is useful for land surveying, way marking, map-making, tracking and surveillance commerce and scientific uses.

Description:

GPS receivers have many applications in aircraft, ships, sea vessels and the like for navigation purposes. Smartphones with maps (like Google Maps) find routes to a specific destination such as a restaurant, hospital or hotel. With the help of a GPS, a target camp can be located and a missile launched to destroy it.
Picture
The GPS receiver module includes an antenna and a built-in processor. The built-in processor calculates the required information from the signals’ output serially in form of strings using NMEA (expanded as National Marine Electronics Association) 0183 protocol. These strings are received serially by the host computer for display or by host processor/controller to take any decision or action.





Fig. 2: GPRMC string format
FIG. 2: GPRMC STRING FORMAT

NEMA protocol includes standard messages given by the GPS receiver. Some standard message outputs from NMEA are GGA, ZDA, VTC, RMC, GSA and GSV. The string starts with $GPRMC tag and gives time, latitude, longitude, etc.

Block Diagram

gps

Schematic

gps

Code

// ***************************************************
// Project: Interfacing GPS module with 8051
// Author: Code Bloges
// Module description: Operate GPS
// ***************************************************
#include<reg51.h>
#define Baud_rate 0xFD  // BAUD RATE 9600
//Function declarations
void delay(int);
void LCDInit();
void LCDCmd(char cmd);
void LCDData(char dat);
void LCDWriteInt(int val,unsigned int field_length);
void LCDWriteString(char *str);
void LCDGotoXY(unsigned int x,unsigned int y);
unsigned char rx_data();
void USART_Init(void);
void tx_data(unsigned char serialdata);
void Tx_String(char *str);
unsigned char LATITUDE[15];
unsigned char LONGITUDE[15];
// Defines Pins
sbit RS = P1^0;
sbit RW  = P1^1;
sbit EN  = P1^2;
#define LCDWriteStringXY(x,y,msg) {\
 LCDGotoXY(x,y);\
 LCDWriteString(msg);\
}
#define LCDWriteIntXY(x,y,val,fl) {\
 LCDGotoXY(x,y);\
 LCDWriteInt(val,fl);\
}
#define DATA P2
unsigned int k=6,d=0,y=0,i=0,l=6,z=1,b,a=0,c=0,Temp=0;
// ***********************************************************
// Main program
//
void main(void)
{
    LCDInit();
    USART_Init();
    delay(1000);
    while(1)
    {
       unsigned int k=6,d=0,y=0,i=0,l=6,z=1;
       for(b=0;b<6;b++)
       while(rx_data()=='$')
       {
        if(rx_data()=='G')
         {
          LCDCmd(0x01);
           if(rx_data()=='P')
           {
           if(rx_data()=='R')
            {
             if(rx_data()=='M')
             {
              if(rx_data()=='C')            
              {
                while(rx_data() != ',');
                LCDWriteString("LATI:");
                while(rx_data() != ',');
                LCDCmd(0xc0);
                LCDWriteString("LONG:");
                if(rx_data()=='V')
                {             
                  while( rx_data() != ',');
                  Temp = 0;
                  a=0;
                  i=0;
                  while(Temp != ',')
                  {
                    Temp = rx_data();
                    if(Temp != ',')
                    {
                      LATITUDE[i++] = Temp;
                    }
                    else
                    {
                       a=rx_data();
                       LATITUDE[i++]=a;
                       LATITUDE[i] = '\0';
                     }
                     LCDGotoXY(k++,y);
                     delay(5);
                     LCDData(Temp);
                     delay(5);
                     LCDData(a);
                     delay(5);
                     //LCDData(LATITUDE[i]);
                     //_delay_ms(0.2);
                   }
                 while( rx_data() != ',');
                 Temp = 0;
                 a=0;
                 i=0;
                 while(Temp != ',')
                 {
                    Temp = rx_data();
                    if(Temp != ',')
                    {
                       LONGITUDE[i++] = Temp;
                    }
                    else
                    {
                       a=rx_data();
                       LONGITUDE[i++]=a;
                       LONGITUDE[i] = '\0';
                    }
                    LCDGotoXY(l++,z);
                    delay(5);
                    LCDData(Temp);
                    delay(5);
                    LCDData(a);
                    delay(5);
                    //LCDData(LONGITUDE[i]);
                    //_delay_ms(0.2);
                  }
                   delay(5);                                                                                             
                  }
                  else
                  {
               LCDCmd(0x01);
               LCDWriteStringXY(0,1,"OUT OF CVG");
                delay(5);
                 }
               }             
            }
           }
          }
         }                                              
        }                                                             
     }             
}
void tx_data(unsigned char serialdata)
{
                SBUF = serialdata;                                                        // LOAD DATA TO SERIAL BUFFER REGISTER
                while(TI == 0);                                                   // WAIT UNTIL TRANSMISSION TO COMPLETE
                TI = 0;                                                                                    // CLEAR TRANSMISSION INTERRUPT FLAG
}
void Tx_String(char *str)
{
                while(*str)
                {
                                tx_data(*str);
                                str++;
                                delay(5);
                }
}
void LCDGotoXY(unsigned int x,unsigned int y)
{
 if(x<40)
 {
  if(y)
  x|=64;
  x|=128;
  LCDCmd(x);
  }
}
void delay(int a)
{
   int i;
   for(i=0;i<a;i++);   //null statement
}
void USART_Init(void)                              // INITIALIZE SERIAL PORT
{
                TMOD = 0x20;                           // Timer 1 IN MODE 2 -AUTO RELOAD TO GENERATE BAUD RATE
                SCON = 0x50;                                                    // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED
                TH1 = Baud_rate;                                            // LOAD BAUDRATE TO TIMER REGISTER
                TR1 = 1;                                                                   // START TIMER
}
unsigned char rx_data()
{
                 while(RI==0);
                 RI=0;   
                 return SBUF;    
}
void LCDInit()
{
    LCDCmd(0x02);
                LCDCmd(0X28);
                delay(100);
                LCDCmd(0X06);
                delay(100);
                LCDCmd(0X01);
                delay(100);
                LCDCmd(0X0e);
                delay(100);
                LCDCmd(0X80);
}
void LCDCmd(char cmd)
{
                RS=0;
                RW=0;
                EN=1;
                DATA=(cmd>>4);
                delay(1);
                EN=0;
                delay(1);
                EN=1;
                DATA=(cmd&0x0f);
                delay(1);
                EN=0;
}
void LCDData(char dat)
{
                RS=1;
                RW=0;
                EN=1;
                DATA=(dat>>4);
                delay(1);
                EN=0;
                delay(1);
                EN=1;
                DATA=(dat&0x0f);
                delay(1);
                EN=0;
}
void LCDWriteString(char *str)
{
                while(*str!='\0')
                {
                                LCDData(*str);
                                str++;
                                delay(1);
                }
}
void LCDWriteInt(int val,unsigned int field_length)
{
               char str[5]={0,0,0,0,0};
                int i=4,j=0;
                while(val)
                {
                str[i]=val%10;
                val=val/10;
                i--;
                }
                if(field_length==-1)
                                while(str[j]==0) j++;
                else
                                j=5-field_length;
                if(val<0) LCDData('-');
                for(i=j;i<5;i++)
                {
                LCDData(48+str[i]);
                }
}


Further Reading suggestions:

No comments:

Post a Comment