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

Please check my Math

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



Joined: 08 Dec 2006
Posts: 125
Location: Texas

View user's profile Send private message

Please check my Math
PostPosted: Tue Oct 19, 2010 9:56 am     Reply with quote

I'm not much of a C programmer so I may be doing something incorrectly. I'm taking an char buffer (Altitude) and converting it to a Int16.

TmpBuffer is the Ascii Altitiude = "50.4" ending with a null.
fAlt is a float
GPS_Rec.Alt is int16

Code:

 fALT = atof(TmpBuffer);     //Read buffer
 if (Meters == 1)
      fALT *= 3.28f;

  GPS_Rec.ALT = (int16)fALT;

It appears to work but I have users reporting issues when I get over 1000 meters, the altitude only reports a few hundred feet.

Honestly, I haven't checked to see if it's a GPS issue, parsing issue, or math issue. I just wanted to check and make sure I was doing the calculations right to begin with.


Richard
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 19, 2010 11:39 am     Reply with quote

Make a test program and put in some number in the range that you want
to test. Run it in MPLAB simulator with printf going to the Output Window,
by selecting UART1 in the Debugger setup menu. The program below
gives this output with vs. 4.112:
Quote:

3297

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <stdlib.h>
//==========================================
void main()
{
float fALT;
char TmpBuffer[10] = {"1005.4"};
char Meters = 1;
struct {
int16 ALT;
}GPS_Rec;

fALT = atof(TmpBuffer);     
if(Meters == 1)
   fALT *= 3.28f;

GPS_Rec.ALT = (int16)fALT;

printf("%ld \r",  GPS_Rec.ALT);

while(1);
}
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