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 support@ccsinfo.com

GPS help

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







GPS help
PostPosted: Tue May 27, 2008 10:15 am     Reply with quote

Using a GPS module using NEMA.

I am basically trying to get the PIC to receive the data string from the GPS, and then spit it right back out to a PC.

Right now, I have it so that it checks for the beginning character "$", which indicates it is the beginning of a new stream of data. I then have it record to an array the string until it reaches 0x0D, which is one of the end of transmission bits.

But what I end up getting out is about half the data from the data stream.
I don't know if this has to do with the timing, or just incorrect programming.

I have tried to use the interrupt for the USART, but the way I have it setup(not using the standard TX and RX pins), it won't work.
I really cannot figure out why my code would not work...so hopefully someone can point something out to me.

I am pretty new to using this compiler and using USART, so bare with me. =)

Code:
#include "GPS.h"

#define LCD_ENABLE_PIN PIN_E1
#define LCD_RS_PIN PIN_E0
#define LCD_RW_PIN PIN_A0
#define LCD_DATA_PORT 0xf83
#define LCD_TYPE 2
#define LCD_TRIS_LOCATION 0xf95
#include <lcd.c>


void main()
{
   lcd_init();

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   skip = 0x0A;      //skips line
   delay_ms(1000);   //setup delay
   
   while(true)
   {
       i = 0;                    //initialize counter
      j = 0;                    //initialize counter         
      output_low(PIN_E2);     //turns alive LED ON   
      
      //sets USART to GPS pins in order to receive data from GPS                                                     
      #use rs232(baud=4800,parity=N,xmit=PIN_B4,rcv=PIN_B5,bits=8,stream=GPS)
      if((fgetc(GPS) == 0x24))
      {
         GPSstream[i] = getc(GPS);
         i++;
         while(fgetc(GPS) != 0x0D)
         {
            GPSstream[i] = fgetc(GPS); 
            i++;
         }
      }
      output_high(PIN_E2);
            //if(i > 1)
            //   fputc(skip,PC);
        
      //sets USART to PC pins in order to send data to PC                                                 
      #use rs232(baud=9600,parity=N,xmit=PIN_A1,rcv=PIN_A2,bits=8,stream=PC)
      while(j < i)
      {   
         fputc(GPSstream[j],PC);         //outputs character from GPS string                         
         j++;                           //increments for next character       
      }
      if(j > 1) 
      fputc(skip,PC);
   }
}
Ryan
Guest







PostPosted: Tue May 27, 2008 10:18 am     Reply with quote

Before I forget....for those who aren't familiar with these GPS modules, they constantly send out data...and it's fairly quickly. So basically the PIC is always getting data from the PIC.

I also am not using the KBHIT because of this reason. I figure since I am always receiving data, I can just look for the beginning "$" character.
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Tue May 27, 2008 1:17 pm     Reply with quote

This has been asked and answered. You may wish to do it your way but if you are willing to search for a solution then use this board. Look into a circular buffer that is fed via the RX interrupt. When the buffer has a full sentence set a flag and begin parsing in it your main routine knowing that while you are parsing the sentences chars for the next will still be received and entered into the circular buffer. If you are selecting a specific sentence then only gate that sentence into the buffer. If this is your first PIC project then forget parsing until you master the receipt of chars into a circular buffer. Use the CCS example for this. Don't add you own ideas until you get it working and understand it.
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: GPS help
PostPosted: Tue May 27, 2008 2:25 pm     Reply with quote

Ryan,

First of all, I notice that you use both fgetc() and getc() in your receive loop. This is confusing. You should use just one form or the other. And getc() does not take any parameters. It is meaningless to write getc(GPS). The "GPS" is ignored. You are just lucky that getc() uses the most recent #use RS232 statement, which just happens to be for GPS. It would be safer to write fgetc(GPS), especially since you have two serial ports in your program and it is easy to get them confused. Use getc() in a program that only references one serial port.

The big problem is in:
Code:

while(fgetc(GPS) != 0x0D)
{
   GPSstream[i] = fgetc(GPS); 
   i++;
}

Every time you call fgetc() it gets a new character. So this loop fetches a character, compares that character to 0x0D, then if it is not 0x0D, it fetches another character and puts it in GPSstream[]. So you will be storing every other character in GPSstream[]. And there is a 50% chance you could miss the 0x0D entirely. Do something like this:
Code:

while((ch=fgetc(GPS)) != 0x0D)
{
   GPSstream[i] = ch;
   i++;
   if(i == sizeof(GPSstream)-1)
     break;
}

Note that I have added an overflow check for GPSstream[]. You always want to protect yourself from bad data. If the 0x0D never comes, but other stuff does, then your program would crash.

Robert Scott
Real-Time Specialties
Ryan
Guest







PostPosted: Tue May 27, 2008 3:10 pm     Reply with quote

RLScott,
Thanks...I'll give that a try tomorrow when I work on it.

In the mean time, maybe one of you can answer this question:
My chips built in USART (pins C6 and C7) do not seem to work. When I use the software USART, it reads fine. Is there any special setup I need to do in order to get my chips built in USART to work?
I know for a fact that the data is being sent to the correct pins, I used a digital probe to verify it.
I was thinking that part of my problem may be due to the fact that the software USART is a little slower, and sending the data to the PIC at 4800, with a 16MHz clock is causing issues.

If the software USART should work, I will just stick to that since I know it was working.
Ryan
Guest







PostPosted: Tue May 27, 2008 4:26 pm     Reply with quote

Ok, I actually went to work on it tonight.
That fixed my problem. It works like a charm now.
I went back to using the software USART for both the receiving and transmission.

Thanks for the help! I appreciate it!
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jan 28, 2009 4:26 pm     Reply with quote

Ryan wrote:
Ok, I actually went to work on it tonight.
That fixed my problem. It works like a charm now.
I went back to using the software USART for both the receiving and transmission.

Thanks for the help! I appreciate it!


I recently wrote a GPS parser using IRQ driven routines. It works great while I have all sorts of other whacky things going on.

I think for something like that where you'll have other things going on in the system, seriously consider doing your GPS work with the hardware UART of the PIC family. Hardware is spinning about worrying about all the details of RS232 for you.

Your software only has to cope when the data finally hits the RX buffer.

It's worth the ISR routine.

GPS's are chatty by design.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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