Wednesday 19 December 2018

Interfacing GPS with LPC2148 for finding current location latitude and longitude values.

 

Features of GPS:

  • Ultra high sensitivity: -165dBm
  • Built-in 12 multi-tone active interference canceller
  • Low power consumption: Typical 22mA@3.3V
  • ±10ns high accuracy time pulse (1PPS)
  • NMEA Output:GGA
  • Advanced Features: AlwaysLocate; AIC
  • UART interface: 4800/9600/38400/115200 bps




GPS Module Image:
 GPS Module Pins:
1) Vcc: 3.3v to 5v
2) GND
3) Tx
4) Rx
GPS Module







Interfacing Diagram of GPS with LPC2148:


Interfacing Diagram






Interfacing Diagram

Different GPS Received Data format:

1) GPGGA:
2) GPGLL:
3) GPGSA:
4) GPGSV:
5) GPRMC:
6) GPZDA:
7) GPVTG:


GPS Received Data Format(GPGGA):


GPS Received Data Format



Algorithm for GPS module interfacing with LPC2148:




  1.  START
  2.  Initialise UART0/ UART1 and LCD(16*2)
  3.  Receive GPS Message of location and longitude through UART module using function                 (uart1Getch())
  4.  Store received  string in respective array (cmd_data[], time_data[], latitude[], langitude[]).
  5.  Pass this array to LCD_write_string() function
  6. .STOP




Code:
GPS interfacing with LPC2148

/**************************************************************************************************
Expt. 5.: UART interfacing with to LPC2148
Platform:  LPC2148 Development Board.
College: PICT

Hardware Setup:-
 UART pins: P0.8 & P0.8         
********************************************************************************/


#include <lpc214x.h> 

#define RS (1<<16)

#define RW (1<<17)

#define E (1<<18)

void LCD_command(unsigned char command);

void delay_ms(unsigned char time);

void LCD_data(unsigned char data);

void LCD_write_string(unsigned char *string);

void LCD_init() ;

void uart1Init();

unsigned char uart1Getch();



int main(void)

{ unsigned char string1[] = "GPGGA";

  unsigned char i, flag=0;
unsigned char cmd_data[5], time_data[15], latitude[15], longitude[15];
    IODIR1= 0x07<<16; //Configure P1.18, P1.17, P1.16 as output
    IODIR0= 0xFF<<16;  //Configure P0.23 - P0.16 as output
    LCD_init();    //Initialize LCD 16x2
    LCD_command(0x01); 
  uart1Init(); 
  LCD_write_string("Welcome to PICT");
  LCD_command(0xc0);  //second line 1st position
  LCD_write_string("PUNE");
  delay_ms(1000) ;

while(1)
{
if(uart1Getch()=='$')
{
for (i=0; i<5; i++)
{
flag=0;
cmd_data[i]=uart1Getch();
if (cmd_data[i]!= string1[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
for (i=0; i<12; i++)
{
time_data[i]=uart1Getch();
time_data[12]='\0';
for (i=0; i<12; i++)
{
latitude[i]=uart1Getch();
}
latitude[11]='\0';
for (i=0; i<12; i++)
{
longitude[i]=uart1Getch();
}
longitude[12]='\0';
LCD_command(0x01);
LCD_write_string("GPS sent:");
LCD_write_string(cmd_data);
LCD_command(0xC0);
LCD_write_string("UTC:");
LCD_write_string(time_data);
delay_ms(2000);
LCD_command(0x01);
LCD_write_string("Lt:");
LCD_write_string(latitude);
LCD_command(0xC0);
LCD_write_string("Ln:");
LCD_write_string(longitude);
delay_ms(1000);
}
}
}
}
//Function to generate software delay
//Calibrated to 1ms
void  delay_ms(unsigned char time)    
{  
 unsigned int  i, j;
 for (j=0; j<time; j++)
 {
  for(i=0; i<8002; i++)
  {
  }
}
}

void LCD_command(unsigned char command)
{
 IOCLR0 = 0xFF<<16; // Clear LCD Data lines
 IOCLR1=RS;     // RS=0 for command
 IOCLR1=RW;     // RW=0 for write
 IOSET0=command<<16; // put command on data line
 IOSET1=E;   // en=1 
 delay_ms(10) ;   // delay
 IOCLR1=E;    // en=0
}

void LCD_data(unsigned char data)
{
 IOCLR0 = 0xFF<<16; // Clear LCD Data lines
 IOSET1=RS;     // RS=1 for data
 IOCLR1=RW;     // RW=0 for write
 IOSET0= data<<16;  // put command on data line
 IOSET1=E;   //en=1 
 delay_ms(10) ;    //delay
 IOCLR1=E;   //en=0
 }

void LCD_init()
{
 LCD_command(0x38); //8bit mode and 5x8 dotes (function set)
 delay_ms(10) ;   // delay
 LCD_command(0x0c); //display on, cursor off, cursor char blinking off(display on/off)
 delay_ms(10) ;   // delay
 LCD_command(0x0e);  //cursor increment and display shift(entry mode set)
 delay_ms(10) ;   // delay
 LCD_command(0x01);  //clear lcd (clear command)
 delay_ms(10) ;   // delay
 LCD_command(0x80); 
 delay_ms(10) ;//set cursor to 0th location 1st lne
}
void LCD_write_string(unsigned char *string)
{
int i=0;
  while(string[i]!='\0')//Check for End of String
  {   
  LCD_data(string[i]);
  i=i+1;  
}
}
void uart1Init(void)      
{
    PINSEL0=0x00050005;// port 0 tx P0.1 and rx P0.0 selected
    U1LCR=0x83; //8bit data, no parity, 1 stop bit
    U1DLL=97;// 9600 baud rate @15Mhz Pclk
    U1LCR=0x03;// DLAB=0
}

unsigned char uart1Getch(void)
{
    while(!(U1LSR & 0x01)); //RDR=0 then FIFO is empty then only this will rx character
    return(U1RBR);  // return this data
}









Code:
GPS interfacing with LPC2148



#include <lpc214x.h> 
#define RS (1<<16)
#define RW (1<<17)
#define E (1<<18)

unsigned int j,finish =0,pos_cnt=0,lat_cnt=0,log_cnt=0,flg =0,com_cnt=0,i=0,fg=0;
unsigned char Gpsdata;             // for incoming serial data
unsigned char lat[20];             // latitude array
unsigned char lg[20];              // longitude array


void LCD_command(unsigned char command);
void delay_ms(unsigned char time);
void LCD_data(unsigned char data);
void LCD_write_string(unsigned char *string);
void LCD_init() ;
void uart1Init();
unsigned char uart1Getch();
void gps();



int main(void)


    IODIR1= 0x07<<16; //Configure P1.18, P1.17, P1.16 o/p
    IODIR0= 0xFF<<16;  //Configure P0.23 - P0.16 as output
    LCD_init();    //Initialize LCD 16x2
    LCD_command(0x01); 
    uart1Init(); 
    LCD_write_string("Welcome to PICT");
delay_ms(1000) ;
LCD_command(0xc0);  //second line 1st position
    LCD_write_string("PUNE");
    delay_ms(1000) ;
delay_ms(1000) ;
  delay_ms(1000) ;
LCD_command(0x01);

while(1) 
 {
   gps();
   LCD_command(0x80);
   LCD_write_string("LT:");
   LCD_write_string(lat);
   LCD_write_string("N");
   LCD_command(0xC0);
   LCD_write_string("LG:");
   LCD_write_string(lg);
   LCD_write_string("E");
 }
}

void LCD_command(unsigned char command)
{
 IOCLR0 = 0xFF<<16; // Clear LCD Data lines
 IOCLR1=RS;     // RS=0 for command
 IOCLR1=RW;     // RW=0 for write
 IOSET0=command<<16; // put command on data line
 IOSET1=E;   // en=1 
 delay_ms(10) ;   // delay
 IOCLR1=E;    // en=0
}

void LCD_data(unsigned char data)
{
 IOCLR0 = 0xFF<<16; // Clear LCD Data lines
 IOSET1=RS;     // RS=1 for data
 IOCLR1=RW;     // RW=0 for write
 IOSET0= data<<16;  // put command on data line
 IOSET1=E;   //en=1 
 delay_ms(10) ;    //delay
 IOCLR1=E;   //en=0
 }

void LCD_init()
{
 LCD_command(0x38); //8bit mode and 5x8 dotes 
 delay_ms(10) ;   // delay
 LCD_command(0x0c); //display on, cursor off, 
 delay_ms(10) ;   // delay
 LCD_command(0x0e);  //cursor increment and display 
 delay_ms(10) ;   // delay
 LCD_command(0x01);  //clear lcd (clear command)
 delay_ms(10) ;   // delay
 LCD_command(0x80); 
 delay_ms(10) ;//set cursor to 0th location 1st lne
}
void LCD_write_string(unsigned char *string)
{
int i=0;
  while(string[i]!='\0')//Check for End of String
  {   
  LCD_data(string[i]);
  i=i+1;  
}
}
void uart1Init(void)      
{
    PINSEL0=0x00050005;// port 0 tx P0.1 and rx P0.0 
    U1LCR=0x83; //8bit data, no parity, 1 stop bit
    U1DLL=97;// 9600 baud rate @15Mhz Pclk
    U1LCR=0x03;// DLAB=0
}

unsigned char uart1Getch()
{
    while(!(U1LSR & 0x01)); //RDR=0 then FIFO is empty 
    return(U1RBR);  // return this data
}

void  delay_ms(unsigned char time)    
{  
 unsigned int  i, j;
 for (j=0; j<time; j++)
 {
  for(i=0; i<8002; i++)
  {
  }
}
}

void gps()
{
    while(finish==0)
{
      Gpsdata = uart1Getch();
     
        flg = 1;
       if( Gpsdata=='$' && pos_cnt == 0)   // finding GPGGA 
         pos_cnt=1;
       if( Gpsdata=='G' && pos_cnt == 1)
         pos_cnt=2;
       if( Gpsdata=='P' && pos_cnt == 2)
         pos_cnt=3;
       if( Gpsdata=='G' && pos_cnt == 3)
         pos_cnt=4;
       if( Gpsdata=='G' && pos_cnt == 4)
         pos_cnt=5;
       if( Gpsdata=='A' && pos_cnt==5 )
         pos_cnt=6;
       if(pos_cnt==6 &&  Gpsdata ==','){   // count commas in 
         com_cnt++;
         flg=0;
       }

       if(com_cnt==2 && flg==1){
        lat[lat_cnt++] =  Gpsdata;         // latitude
        flg=0;
       }

       if(com_cnt==4 && flg==1){
         lg[log_cnt++] =  Gpsdata;         // Longitude
         flg=0;
       }

       if( Gpsdata == '*' && com_cnt >= 5 && flg == 1)
      {
         lat[lat_cnt] ='\0';             // end of GPGGA message
         lg[log_cnt]  = '\0';
         com_cnt = 0;                     
         lat_cnt = 0;
         log_cnt = 0;
         flg     = 0;
         finish  = 1;

      }
    }

   finish = 0;
   pos_cnt = 0;
}









No comments:

Post a Comment