CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Need help reading Serial data from the GPS receiver into PIC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
analog_world



Joined: 16 Sep 2008
Posts: 26

View user's profile Send private message

Need help reading Serial data from the GPS receiver into PIC
PostPosted: Tue Sep 16, 2008 4:07 pm     Reply with quote

Hi, I am working on my final project which is a GPS guided RC car. However, I have not had any prior experience working with microcontrollers as such. Can anyone help me on how to write the code using the CCS compiler functions on how to read in serial data from the USART port (the functions to use and other details...etc). I would really appreciate if any of the experts here can post the sample code on reading the data and other details. I really need help with this! ( I am using a PIC 16F877A).
ffotsch



Joined: 14 Sep 2008
Posts: 8

View user's profile Send private message

PostPosted: Wed Sep 17, 2008 4:58 am     Reply with quote

I'm no expert, but I did a similar project with a hot air balloon. Set the GPS to NMEA output and the gps starts sending NMEA strings updated each second. Check the web site in the code for the format of the strings. My code just listens for the correct string and then captures the data into a string and saves it to and external EEPROM. This can be formatted and saved as a float for calculations. I have a factor for changing lat and log to meters, this is a location dependent factor. Also, you don't need a level converter as the gps puts out TTL.

Code:

//http://www.cis.udel.edu/~mills/ntp/html/drivers/driver20.html
// distances for location of 37° 10'N
// distance of one minute of latitude 1849.679m
// distance of one minute of longitude 1480.284m
#include <16f877.h>
#include <stdlib.h>
#include <math.h>
#use RS232(Baud=4800,Rcv=pin_a0, xmit=pin_a1)//gps only needs the rx and gnd pin
#define round(x,n) (floor(x*(10.0^n)+0.49)/(10.0^n)))

void get_fix();
void store_waypoint(long position);
void load_waypoint (long position);
void write_float (long location, float store_float);
float read_float (long location);
float round_float (float temp_float, int8 n);

int1 good_fix;
char gps_str[53],temp_str[8];
int8 count=0;
long gps_count=0;
int8 rcv_chr;
float alt_float;
float lat_float;
float log_float;
float init_alt_float;
float init_lat_float;
float init_log_float;
float d_alt;
float d_lat;
float d_log;
float distance;
long temp_long=0;
float temp_float=0;


main(){
init_ext_eeprom();
lcd_init();
Printf (lcd_putc,"\f Wating");
output_high (pin_a3);
while (true){
   good_fix=false;
   gps_count++;
   get_fix();
   if (!input(pb_1)){
      Printf (lcd_putc,"\fwaypoint");
      Printf (lcd_putc,"\nstored");
      store_waypoint(1);
      load_waypoint (1);
      }
   d_alt = alt_float - init_alt_float;
   d_lat = (lat_float - init_lat_float)*1849.679;
   d_log = (log_float - init_log_float)*1480.284;

   distance= sqrt ( (d_lat*d_lat) +(d_log*d_log));
   //printf(lcd_putc,"\fD=%4.2f",distance);
   //printf(lcd_putc,"\nA=%4.2f",d_alt);
   temp_long=0;
   temp_long = (lat_float*1000);
   lat_float = temp_long;
   lat_float /=1000;
   printf(lcd_putc,"\f%f",lat_float);
   printf(lcd_putc,"\n%lu",temp_long);
   
   if (distance>5){
      output_high (pin_a3);
      }
      else{
      output_low (pin_a3);
      }

   }

}

void get_fix(){
while (!good_fix){
   rcv_chr=getc();
   if (rcv_chr=='$'){
      rcv_chr=getc();
      if (rcv_chr=='G'){
         rcv_chr=getc();
         if (rcv_chr=='P'){
            rcv_chr=getc();
            if (rcv_chr=='G'){
               rcv_chr=getc();
               if (rcv_chr=='G'){
                  rcv_chr=getc();
                  if (rcv_chr=='A'){
                     rcv_chr=getc();
                        if (rcv_chr==','){
                           for (count=0;count<53;count++){
                              gps_str[count]=getc();
                              }
                           for (count=0;count<7;count++){
                              temp_str[count]=gps_str[13+count];
                              }
                              lat_float=atof(temp_str);

                           for (count=0;count<7;count++){
                              temp_str[count]=gps_str[26+count];
                              }
                              log_float=atof(temp_str);

                           for (count=0;count<7;count++){
                              temp_str[count]=gps_str[45+count];
                              }
                              alt_float=atof(temp_str);
                           good_fix=true;
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }



float read_float (long location){
   float store_float;
   int8 *pointer;
   int8 position;
   pointer=&store_float;
   for (position =0; position<4; position++){
      *(pointer+position)=read_ext_eeprom((4*location)+position);
   }
   return store_float;
}

void write_float (long location, float store_float){
int8 position;
int8 *pointer;
pointer=&store_float;
for (position =0; position<4; position++){
   write_ext_eeprom ((4*location)+position, *(pointer+position));
   }
}

void store_waypoint(long int position){
   write_float (4*position, alt_float);
   write_float ((4*position)+4, lat_float);
   write_float ((4*position)+8, log_float);
}


void load_waypoint (long int position){
   init_alt_float = read_float (4*position);
   init_lat_float = read_float ((4*position)+4);
   init_log_float = read_float ((4*position)+8);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group