Reply To: PIC16F877A with GPS and GSM for tracking purpose

Home Forums Microcontrollers PIC Microcontroller PIC16F877A with GPS and GSM for tracking purpose Reply To: PIC16F877A with GPS and GSM for tracking purpose

#11011
vineethkumarc
Participant

hi,
this is the code written to dispaly and snd msg to gsm with the gps data..
I use software uart for recieving gps data..is there any problem?

sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB2_bit;\
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;
// END LCD

// GPS
char Gpsdata,gps,error;               // for incoming serial data
unsigned int finish =0;    // indicate end of message
unsigned int pos_cnt=0;    // position counter
unsigned int lat_cnt=0;    // latitude data counter
unsigned int log_cnt=0;    // longitude data counter
unsigned int time_cnt=0;
unsigned int flg=0;       // GPS flag
unsigned int com_cnt=0;   // comma counter

char time[20];
char lat[20],lat1[20],lat2[20];             // latitude array
char lg[20],lg1[20],lg2[20];              // longitude array

//GSM
void Receive_GPS_Data();
void GSM_Initialisation();
void GSM_Write();
void GSM_Read();
void main()
{
  Soft_Uart_init(&PORTD,1,2,9600,0);

  do
  {
    int t;
    finish = 0;pos_cnt = 0;
    Receive_GPS_Data();
    Lcd_Init();         // Initialize LCD connected to PORTB
    Lcd_Cmd(_LCD_CLEAR);       // Clear display 
    Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off
    Lcd_Out(1, 1,"log");      // Print text to LCD, 1st row, 1st column
    Lcd_Chr_Cp(lg);
    Lcd_Out(2, 1,"lat");     //  Print text to LCD, 2nd row, 1st column
    Lcd_Chr_Cp(lat);

    for(t=10;t>=0;t--)
    {
      Delay_ms(200);
      if((lat1==lat)&&(lg1==lg))
      {
        lat2[20]=lat1[20];
        lg2[20]=lg1[20];
      }
      if((lat2!=lat1)&&(lg2!=lg1))
      {
        GSM_Initialisation();
        GSM_Write();
      }
    }

    while(UART1_Data_Ready()==1)
    {
      GSM_Initialisation();
      GSM_Read();
      GSM_Write();
    }
  }
  while(1);
}

void Receive_GPS_Data()
{
  do
    Gpsdata = Soft_UART_Read(&error);
  while (error);

  while(finish==0)
  {
    Gpsdata = Soft_Uart_Read(&error);
    flg = 1;
    if( Gpsdata=='$' && pos_cnt == 0)   // finding GPRMC header
    pos_cnt=1;
    if( Gpsdata=='G' && pos_cnt == 1)
    pos_cnt=2;
    if( Gpsdata=='P' && pos_cnt == 2)
    pos_cnt=3;
    if( Gpsdata=='R' && pos_cnt == 3)
    pos_cnt=4;
    if( Gpsdata=='M' && pos_cnt == 4)
    pos_cnt=5;
    if( Gpsdata=='C' && pos_cnt==5 )
    pos_cnt=6;
    if(pos_cnt==6 &&  Gpsdata ==',')
    {
      // count commas in message
      com_cnt++;
      flg=0;
    }
    if(com_cnt==3 && flg==1)
    {
      lat[lat_cnt++] =  Gpsdata;         // latitude
      flg=0;
    }
    if(com_cnt==5 && flg==1)
    {
      lg[log_cnt++] =  Gpsdata;         // Longitude
      flg=0;
    }
    lat1[20]=lat[20];
    lg1[20] = lg[20];
    if( Gpsdata == '*' && com_cnt >= 5)
    {
      com_cnt = 0;                      // end of GPRMC message
      lat_cnt = 0;
      log_cnt = 0;
      flg     = 0;
      finish  = 1;
    }
  }
}

void GSM_Initialisation()
{
  UART1_Init(9600);
  Delay_ms(1000);
  UART1_Write_Text("AT");
  UART1_Write(10);
  UART1_Write(13);
  delay_ms(500);
  UART1_Write_Text("ATE0");
  UART1_Write(10);
  UART1_Write(13);
  delay_ms(500);
  UART1_Write_Text("AT+CMGF=1");
  UART1_Write(10);
  UART1_Write(13);
  delay_ms(500);
}

void GSM_Write()
{
  UART1_Write_Text("AT+CMGS=\"+919946977684\"");
  UART1_Write(10);
  UART1_Write(13);
  delay_ms(500);
  UART1_Write_Text("THEFT ALERT");
  UART1_Write(lat);
  UART1_Write(lg);
  UART1_Write(0x1A);
  Delay_ms(1000);
  UART1_Write_Text("ATD=9946977684");
  UART1_Write(10);
  UART1_Write(13);
  delay_ms(500);
}

void GSM_Read()
{
  int i,w=0;
  char msg[55];
  UART1_Write_Text("AT+CMGL=\"REC UNREAD\"\r");
  UART1_Write(10);
  UART1_Write(13);
  Delay_ms(5000); 
  UART1_Write_Text("AT+CMGR=1\r");
  for(i=0;i<75;i++)
  {
    if(UART1_Data_Ready())
    {
       msg[w] = UART1_Read();
       ++w;
    }
  }
 msg[55] = '';
}
>