GPS Module Interfacing with ARM LPC2148

Introduction

NEO-6M GPS Receiver Module NEO-6M GPS Receiver Module

Global Positioning System (GPS) makes use of signals sent by satellites in space and ground stations on Earth to accurately determine their position on Earth.

Radio Frequency signals sent from satellites and ground stations are received by the GPS. GPS makes use of these signals to determine its exact position.

The GPS itself does not need to transmit any information.

The signals received from the satellites and ground stations contain time stamps of the time when the signals were transmitted.

By calculating the time difference between the time the signal was transmitted and the time the signal was received, and using the speed of the signal, the distance between the satellites and the GPS can be determined using a simple formula for distance using speed and time.

Using information from 3 or more satellites, the exact position of the GPS can be triangulated.

For more information about GPS and how to use it, refer the topic GPS Receiver Module in the sensors and modules section.

The GPS receiver module uses UART communication to communicate with controller or PC terminal.

For information about UART in LPC2148 and how to use it, refer the topic UART in LPC2148 in the ARM7-LPC2148 inside section.

 

Interfacing Diagram

Interfacing NEO-6M GPS Receiver Module with LPC2148

 Interfacing NEO-6M GPS Receiver Module with LPC2148

 

Example

We are going to display data (latitude, longitude, altitude and time) received by the GPS receiver module on a serial monitor using UART.

 

The “$GPGGA” string is parsed to extract the time, latitude, longitude and altitude information.

 

Program

/*
  GPS interfacing with LPC2148(ARM7)
  http://www.electronicwings.com/arm7/gps-module-interfacing-with-lpc2148
*/

#include <lpc214x.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>



char Latitude_Buffer[15],Longitude_Buffer[15],Time_Buffer[15],Altitude_Buffer[8];
char iir_val[10];
char GGA_String[150];
uint8_t GGA_Comma_Pointers[20];
char GGA[3];
volatile uint16_t GGA_Index, CommaCounter;
bool	IsItGGAString	= false;



__irq void UART0_Interrupt(void);




void delay_ms(uint16_t j)
{
    uint16_t x,i;
	for(i=0;i<j;i++)
	{
    for(x=0; x<6000; x++);    /* loop to generate 1 milisecond delay with Cclk = 60MHz */
	}
}




void UART0_init(void)
{
	PINSEL0 = PINSEL0 | 0x00000005;	/* Enable UART0 Rx0 and Tx0 pins of UART0 */
	U0LCR = 0x83;	/* DLAB = 1, 1 stop bit, 8-bit character length */
	U0DLM = 0x00;	/* For baud rate of 9600 with Pclk = 15MHz */
	U0DLL = 0x61;	/* We get these values of U0DLL and U0DLM from formula */
	U0LCR = 0x03; /* DLAB = 0 */
	U0IER = 0x00000001; /* Enable RDA interrupts */
}




void UART0_TxChar(char ch) /* A function to send a byte on UART0 */
{
	U0IER = 0x00000000; /* Disable RDA interrupts */
	U0THR = ch;
	while( (U0LSR & 0x40) == 0 );	/* Wait till THRE bit becomes 1 which tells that transmission is completed */
	U0IER = 0x00000001; /* Enable RDA interrupts */
}




void UART0_SendString(char* str) /* A function to send string on UART0 */
{
	U0IER = 0x00000000; /* Disable RDA interrupts */
	uint8_t i = 0;
	while( str[i] != '\0' )
	{
		UART0_TxChar(str[i]);
		i++;
	}
	U0IER = 0x00000001; /* Enable RDA interrupts */
}




__irq void UART0_Interrupt(void)
{
	int iir_value;
	char received_char;
	iir_value = U0IIR;
		received_char = U0RBR;
			if( received_char == '$' )
			{
				GGA_Index = 0;
				CommaCounter = 0;
				IsItGGAString = false;
			}
			else if( IsItGGAString == true )	/* If $GPGGA string */
			{
				if ( received_char == ',' )	
				{
					GGA_Comma_Pointers[CommaCounter++] = GGA_Index;	/* Store locations of commas in the string in a buffer */
				}
				GGA_String[GGA_Index++] = received_char;	/* Store the $GPGGA string in a buffer */
			}
			else if( ( GGA[0] == 'G' ) && ( GGA[1] == 'G' ) && ( GGA[2] == 'A' ) )	/* If GGA string received */
			{
				IsItGGAString = true;
				GGA[0] = GGA[1] = GGA[2] = 0;
			}
			else	/* Store received character */
			{
				GGA[0] = GGA[1];
				GGA[1] = GGA[2];
				GGA[2] = received_char;
			}
	VICVectAddr = 0x00;
}




void get_Time(void)
{
	U0IER = 0x00000000; /* Disable RDA interrupts */
	
	uint8_t time_index=0;
	uint8_t index;
	uint16_t hour, min, sec;
	uint32_t Time_value;

	/* parse Time in GGA string stored in buffer */
	for(index = 0; GGA_String[index]!=','; index++)
	{		
		Time_Buffer[time_index] = GGA_String[index];
		time_index++;
	}	
	Time_value = atol(Time_Buffer);               /* convert string to integer */
	hour = (Time_value / 10000);                  /* extract hour from integer */
	min = (Time_value % 10000) / 100;             /* extract minute from integer */
	sec = (Time_value % 10000) % 100;             /* extract second from integer*/

	sprintf(Time_Buffer, "%d:%d:%d", hour,min,sec);
	
	U0IER = 0x00000001; /* Enable RDA interrupts */
}




void get_Latitude(uint16_t Latitude_Pointer)
{
	U0IER = 0x00000000; /* Disable RDA interrupts */
	
	uint8_t lat_index = 0;
	uint8_t index = (Latitude_Pointer+1);
	
	/* parse Latitude in GGA string stored in buffer */
	for(;GGA_String[index]!=',';index++)
	{
		Latitude_Buffer[lat_index]= GGA_String[index];
		lat_index++;
	}
	
	float lat_decimal_value, lat_degrees_value;
	int32_t lat_degrees;
	lat_decimal_value = atof(Latitude_Buffer);	/* Latitude in ddmm.mmmm */       
	
	/* convert raw latitude into degree format */
	lat_decimal_value = (lat_decimal_value/100);	/* Latitude in dd.mmmmmm */
	lat_degrees = (int)(lat_decimal_value);	/* dd of latitude */
	lat_decimal_value = (lat_decimal_value - lat_degrees)/0.6;	/* .mmmm/0.6 (Converting minutes to eequivalent degrees) */ 
	lat_degrees_value = (float)(lat_degrees + lat_decimal_value);	/* Latitude in dd.dddd format */
	
	sprintf(Latitude_Buffer, "%f", lat_degrees_value);
	
	U0IER = 0x00000001; /* Enable RDA interrupts */
}




void get_Longitude(uint16_t Longitude_Pointer)
{
	U0IER = 0x00000000; /* Disable RDA interrupts */
	
	uint8_t long_index = 0;
	uint8_t index = (Longitude_Pointer+1);
	
	/* parse Longitude in GGA string stored in buffer */
	for(;GGA_String[index]!=',';index++)
	{
		Longitude_Buffer[long_index]= GGA_String[index];
		long_index++;
	}
	
	float long_decimal_value, long_degrees_value;
	int32_t long_degrees;
	long_decimal_value = atof(Longitude_Buffer);	/* Longitude in dddmm.mmmm */
	
	/* convert raw longitude into degree format */
	long_decimal_value = (long_decimal_value/100);	/* Longitude in ddd.mmmmmm */
	long_degrees = (int)(long_decimal_value);	/* ddd of Longitude */
	long_decimal_value = (long_decimal_value - long_degrees)/0.6;	/* .mmmmmm/0.6 (Converting minutes to eequivalent degrees) */
	long_degrees_value = (float)(long_degrees + long_decimal_value);	/* Longitude in dd.dddd format */
	
	sprintf(Longitude_Buffer, "%f", long_degrees_value);
	
	U0IER = 0x00000001; /* Enable RDA interrupts */
}




void get_Altitude(uint16_t Altitude_Pointer)
{
	U0IER = 0x00000000; /* Disable RDA interrupts */
	
	uint8_t alt_index = 0;
	uint8_t index = (Altitude_Pointer+1);
	
	/* parse Altitude in GGA string stored in buffer */
	for(;GGA_String[index]!=',';index++)
	{
		Altitude_Buffer[alt_index]= GGA_String[index];
		alt_index++;
	}
	
	U0IER = 0x00000001; /* Enable RDA interrupts */
}




int main(void)
{
	GGA_Index = 0;
	memset(GGA_String, 0 , 150);
	memset(Latitude_Buffer, 0 , 15);
	memset(Longitude_Buffer, 0 , 15);
	memset(Time_Buffer, 0 , 15);
	memset(Altitude_Buffer, 0 , 8);
	VICVectAddr0 = (unsigned) UART0_Interrupt;	/* UART0 ISR Address */
	VICVectCntl0 = 0x00000026;	/* Enable UART0 IRQ slot */
	VICIntEnable = 0x00000040;	/* Enable UART0 interrupt */
	VICIntSelect = 0x00000000;	/* UART0 configured as IRQ */
	UART0_init();
	while(1)
	{
		delay_ms(1000);
		UART0_SendString(GGA_String);
		UART0_SendString("\r\n");
		UART0_SendString("UTC Time : ");
		get_Time();
		UART0_SendString(Time_Buffer);
		UART0_SendString("\r\n");
		UART0_SendString("Latitude : ");
		get_Latitude(GGA_Comma_Pointers[0]);
		UART0_SendString(Latitude_Buffer);
		UART0_SendString("\r\n");
		UART0_SendString("Longitude : ");
		get_Longitude(GGA_Comma_Pointers[2]);
		UART0_SendString(Longitude_Buffer);
		UART0_SendString("\r\n");
		UART0_SendString("Altitude : ");
		get_Altitude(GGA_Comma_Pointers[7]);
		UART0_SendString(Altitude_Buffer);
		UART0_SendString("\r\n");		
		UART0_SendString("\r\n");
		memset(GGA_String, 0 , 150);
		memset(Latitude_Buffer, 0 , 15);
		memset(Longitude_Buffer, 0 , 15);
		memset(Time_Buffer, 0 , 15);
		memset(Altitude_Buffer, 0 , 8);
	}
}

 

Video

 


Components Used

ARM7 LPC2148
ARM7 LPC2148
1
Ublox NEO-6m GPS
Ublox Neo 6m GPS
1
CP2103 USB TO UART BRIDGE
CP2103 is single chip USB to UART Bridge. It supports USB 2.0 protocol.
1

Downloads

GPS_uVision_Project Download
Ad