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 receive on pin B2, how to timeout (PIC16F876)

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



Joined: 15 Sep 2003
Posts: 36

View user's profile Send private message

GPS receive on pin B2, how to timeout (PIC16F876)
PostPosted: Sat Aug 28, 2004 3:58 pm     Reply with quote

Hello all,

PIC16f876

I am receiving GPS readings on pin B2, I did not used any software or hardware interrupts to read the GPS because I wanted to poll the GPS instead of jumping to an interrupt routine every time a GPS char is present.

So, everytime I need a GPS position, I call a routine to get the characters using fgetc(GPS), this works ok if the GPS is connected but hangs up when GPS is disconnected, how can I timeout when GPS is disconnected?

Thank you in advance
Peter
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

PostPosted: Sat Aug 28, 2004 5:56 pm     Reply with quote

Have you tried polling using kbhit() before calling fgetc()?
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun Aug 29, 2004 7:11 am     Reply with quote

Quote:


So, everytime I need a GPS position, I call a routine to get the characters using fgetc(GPS), this works ok if the GPS is connected but hangs up when GPS is disconnected, how can I timeout when GPS is disconnected?



Does the level of B2 change whenever you disconnect the GPS ?

If so, put a pull up resistor (10K) in B2 and use kbhit(GPS)) function before
fgetc(GPS)) as stated dyeatman.

If this not solve your problem, post your code, headers and compiler version.

Best wishes,

Humberto
runtime



Joined: 15 Sep 2003
Posts: 36

View user's profile Send private message

Here is the code I'm using
PostPosted: Sun Aug 29, 2004 10:00 am     Reply with quote

// Nmea is pin B2
// GetPosition function reads $GPRMS sentence and buffers only relevant info, I need a way to get out of GetPosition if GPS not present...

#use rs232(baud=4800,rcv=Nmea,invert,stream=GPS)
#use rs232(baud=4800,xmit=MOut,rcv=MIn,stream=MODEM)
#use rs232(baud=4800,xmit=PCOut,invert,stream=PC) // RS232 to PC

// --------------------
// Function GetPosition // Parse $GPRMC
// --------------------
void GetPosition(void)
{

start: while (v != 'G') v=fgetc(GPS); // Finds the GPRMC sentence
v = fgetc(GPS);
if (v != 'P') goto start;
v = fgetc(GPS);
if (v != 'R') goto start;
v = fgetc(GPS);
if (v != 'M') goto start;
v = fgetc(GPS);
if (v != 'C') goto start;

fpoint = False; // first point found?
comma = 0; // Count comma chars
status = 0; // Build status bit

i = 0; // Buffer the GPRMC sentence
do {
v = fgetc(GPS);

if (v == ',') comma++;
if (comma == 6) v = '*'; // final marker

if ((v == '.') && (!fpoint)) { // discard .999 in time field
fpoint = True;
v = fgetc(GPS);
v = fgetc(GPS);
v = fgetc(GPS);
v = fgetc(GPS);
}
if (v == 'A'){ // Check for svs
sat = True;
if (firstpos) {
firstpos = False;
seconds = StoreInt - 1;
}
output_high(Valid);
bit_set(status,2);
v = fgetc(GPS); // Discard A,
v = fgetc(GPS);
}
else if (v == 'V') {
sat = False;
if (!input(Valid))(output_high(Valid)); // Flash Valid GPS led if not valid
else (output_low(Valid));
v = fgetc(GPS); // Discard V,
v = fgetc(GPS);
}
if (v == 'S') bit_set(status,3); // Build status bit
if (v == 'E') bit_set(status,4);

if ((v == 'N') || (v == 'S') || (v == 'W') || (v == 'E')) { // Discard N,S,W,E,
v = fgetc(GPS);
v = fgetc(GPS);
}

buf[i] = v; // Fill buffer
i++;
if (i == (MAXBUF - 2)) break;
} while (v != '*');

if (!input(Panic)) { // Check panic btn condition
remote = True;
if (!GPRSStarted && !LineOpen) BaseCall = True;
}

if (!input(Door)) { // Check Doors
DoorS = True;
if (!GPRSStarted && !LineOpen && !AckDoor) BaseCall = True;
}
else AckDoor = False;

if (remote) bit_set(status,0); // Panic bit
if (DoorS) bit_set(status,5); // Door sensor

if (!read_eeprom(12)) bit_set(status,1); // Engine Off bit

if (bit_test(status,0) && (!logP)) { // Log Panic On
seconds = StoreInt - 1;
logP = True;
ZeroStore = False;
}
if (!bit_test(status,0) && (logP)) { // Log Panic Off
seconds = StoreInt - 1;
logP = False;
ZeroStore = False;
}

if (bit_test(status,5) && (!logD)) { // Log Door Open
seconds = StoreInt - 1;
logD = True;
ZeroStore = False;
}
if (!bit_test(status,5) && (logP)) { // Log Door Close
seconds = StoreInt - 1;
logD = False;
ZeroStore = False;
}

status = status + 33;

zip(); // Compress buffer

}
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

Why not try it this way?
PostPosted: Sun Aug 29, 2004 5:56 pm     Reply with quote

A much better (cleaner) approach would be something like this:

Code:


#define bkbhit (next_in!=next_out)


// This routine automatically fetches and stores incoming
// chars in buffer. I would call it multiple times in main to
// make sure you dont miss any chars (depending on
// how long your main code is of course).
void Checkfordata()
  {
{// get char and put into a circular buffer
         RXbuff[next_in] = fgetc();
          t = next_in;  //save current buffer pointer
// get next open position in recv buffer
        next_in = (next_in + 1) % BUFFER_LEN; 
        if (next_in == next_out)  // is it full?
           next_in = t;  // yes!! restore old position
 } // end of CheckforData routine


// **** Start of main background loop *****
While kbhit()
     CheckforData();

// call this routine in Main() to check for something in the buffer
if (bkbhit)  // if begin and end are not the same, there must be               
                //something there to get
      {
        c = Rxbuff[next_out];  // so get it
        next_out = (next_out + 1) % BUFFER_LEN;  // update pointer
      }

While kbhit()
     CheckforData();


Here you process your incoming data in the main loop. Use flags to keep track of where you are in the process.

While kbhit()
     CheckforData();

etc...
 


Just a suggestion that I thought might be useful.... :-)
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