PIC16F877A GPS Interface

By Kunal Kapoor
12-05-2018
1573

GPS is an important and vital sensor. It is primarily used to determine location of the user.

In this project we will be demonstrating how to interface GPS unit with PIC16F877A microcontroller and read its data.

Information about GPS can be read HERE

 


Project

COMPONENTS


S. no

Item Name

Qty

1

PIC16F877A MCU development board

1

2

16 X 2 LCD

1

3

Standard issue GPS module

1 module

4

Miscellaneous jumper wires, power source,etc1

-

Circuit Diagram:



IDE used : MPLAB by Microchip


Code Snippet :



main.h:

#include
#include"delay.h"
//#include"serial.h"
//#include"lcd.h"
//#include

#define XTAL_FREQ 16000000

__CONFIG(0x2f0a);		

void main(void);

delay.h:

#ifndef DELAY_H
#define DELAY_H

#include
//#include"delay.h"
#include"lcd.h"
#include"serial.h"


void delay(unsigned int time);

typedef char ubyte;

#endif


delay.c:

#include"delay.h"

void delay(unsigned int time)
{
	unsigned char count;
	while(time>0)
	{	
		count=255;
		while(count>0)
		{count--;}
		time--;
	}
}
					
		


gpsdatarecieve.c:

/////////		/////////		//////////		////////

//			main file for gps data reception only	//

/////////////	/////////	////////	/////////	////////

#include"main.h"
#include
//void lcd_initialize(void);

void lcdat(unsigned char r);
void lcdcmd(unsigned char y);
void gps(void);

#define rs RB7
#define rw RB6
#define en RB5
#define ldata PORTD

unsigned char gpsdata[]={"GPS DATA"};

unsigned char lat[]={" LA:"};
unsigned char lon[]={"LO:"};
unsigned char time[]={" T:"};
unsigned char alt[]={"atlitude:"};

unsigned char temp,rec;


unsigned char a[]={"0000000000"};
unsigned char latread[]={"0000000000"};
unsigned char lonread[]={"00000000000"};
unsigned char timeread[]={"0000000000"};
unsigned char altread[]={"0000"};
unsigned char d[50],e[50];

unsigned int i=0,j=0,k=0,l=0;

unsigned int latlondelay=500;		///		time to hold lat and lon readings on lcd
unsigned int timedelay=500; 		/// 	time to hold time readings on lcd

void main(void)
{


	TRISB=0x04;			///		for lcd control bits
	TRISD=0;			///		for lcd data bits

	
///////////		lcd initialize		//////
	PORTB=0x00;
	delay(10);
	lcdcmd(0x38);
	delay(10);
	lcdcmd(0x0c);
//	delay(10);
	lcdcmd(0x01);
	delay(10);
/////////	lcd initialize over		//////

/////	print 'gps data' on lcd for 1 sec		/////

	lcdcmd(0x01);			///	clear screen
	lcdcmd(0x81);			///	lin1 pos 0

	i=0;
	while(gpsdata[i]!=NULL)
	{
		lcdat(gpsdata[i]);
		i++;
		delay(10);
	}
	delay(100);
	lcdcmd(0xc0);			///	line 2 pos 0
	i=0;
	for(i=0;i<20;i++)
	{
		lcdat(0x2e);		///	display '.'
		delay(10);
	}
	delay(20);
	lcdcmd(0x01);			///	clear screen

/////	print gps data on lcd for 1 sec	over\	/////



TXSTA=0x20;
RCSTA=0x90;
	SPBRG=0x19;		//baud rate=9600

	///////////////////////////////////////////////////////////////////////////////////////////


	while(1)
	{
		
		RB1=1;
		gps();			/// run gps function
		RB1=0; 			///	check bit
		

			/////	print LAT and LON		/////

	lcdcmd(0x01);			///	clear screen
	i=0;
	lcdcmd(0x80);			///	lin1 pos 0
	
	while(lat[i]!=NULL)
	{
		lcdat(lat[i++]);
	}
	
				lcdcmd(0xc0);			///	line 2 pos 0
				i=0;
				
				while(lon[i]!=NULL)
				{
					lcdat(lon[i++]);
				}
			
				i=0;	
				lcdcmd(0x83);
				while(latread[i]!=NULL)
				{
				lcdat(latread[i]);
				i++;
				}	
			i=0;
					lcdcmd(0xc3);
				while(lonread[i]!=NULL)
				{
				lcdat(lonread[i]);
				i++;
				}

			/////print LAT and LON		over\	/////

delay(latlondelay);

				/////	print 'time' and altitude		/////

	lcdcmd(0x01);			///	clear screen
	i=0;
	lcdcmd(0x80);			///	lin1 pos 0
	
	while(time[i]!=NULL)
	{
		lcdat(time[i++]);
	}

		lcdcmd(0xc0);			///	line 2 pos 0
				i=0;
				
				while(alt[i]!=NULL)
				{
					lcdat(alt[i++]);
				}

	i=0;	
	lcdcmd(0x83);
	while(timeread[i]!=NULL)
	{
	lcdat(timeread[i]);
	i++;
	}	
/////print time		over\	/////

delay(timedelay);


	}
}
void gps(void)
{
	while(rec!='$')
	{
	RCIF=0;
	while(RCIF==0);		//until not recieved data
	rec=RCREG;		//rcieved value in portb
	CREN=0;
	CREN=1;
	}

	for(i=0;i<5;i++)
	{
			RCIF=0;
	while(RCIF==0);		//until not recieved data
	rec=RCREG;		//rcieved value in portb
	CREN=0;
	CREN=1;
	a[i]=rec;
//	lcdat(rec);
	}


if(a[0]=='G' && a[1]=='P' && a[2]=='G' && a[3]=='G' && a[4]=='A')
{
	i=0;
	while(rec!='E')
	{
			RCIF=0;
	while(RCIF==0);		//until not recieved data
	rec=RCREG;		//rcieved value in portb
	CREN=0;
	CREN=1;
	d[i]=rec;	
	//lcdat(rec);
	i++;
	}	

	while(rec!='M')
	{
			RCIF=0;
	while(RCIF==0);		//until not recieved data
	rec=RCREG;		//rcieved value in portb
	CREN=0;
	CREN=1;
	e[i]=rec;	
	//lcdat(rec);
	i++;
	}		


	i=1;
	l=0;
	while(d[i]!=',')			////	time
	{	
		timeread[l]=d[i];
		i++;
		l++;
  	}
i++;
	j=0;
		while(d[i]!='N')			////	lat
	{	
		latread[j]=d[i];
		i++;
		j++;
  	}
		j--;
		latread[j]=NULL;
i=i+2;
k=0;
	
		while(d[i]!='E')			////	lon
	{	
		lonread[k]=d[i];
		i++;
		k++;
  	}
		k--;
		lonread[k]=NULL;
}

}

void lcdat(unsigned char r)
{
	rs=1;
	rw=0;
	ldata=r;
	en=1;
	delay(1);
	en=0;
//	RB0=1;
	
}

void lcdcmd(unsigned char y)
{
	rs=0;
	rw=0;
	ldata=y;
	en=1;
	delay(1);
	en=0;
} 

Output:

Download the code from here



Related Tutorials