Hello
I'm having a problem with this code I'm stumped as to why the function Get_Distance() is returning the wrong value back to the caller. When I run a test in my IDE I watch the values of each variable at breakpoints. When executed distance is 1391.261 which is correct. When the value is returned back to the caller it turns into -6057.000 and assigns -6057.000 to distance2pet and that is what is displayed on my LCD. If I make distance2Pet a global and just make everything 1 big source file then it works fine, but I have a lot more code to do and don't want 5,000 lines of code in 1 file. Thank you for any input!
File 1 mainDemo.C
Code:
#include <p24FJ128GA010.h>
#include <stdio.h>
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_OFF & ICS_PGx2 & FWDTEN_OFF)
_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON & FNOSC_FRC & POSCMOD_NONE)
char distance2PetString[15];
main ()
{
float distance2Pet; //distance from pet to base
LCDInit(); //initialize LCD
LCDClear(); //clear the LCD
distance2Pet = Get_Distance(41.0344, 85.1662, 41.0444, 85.1762);
sprintf(distance2PetString, "%.3f", distance2Pet);
while(1) {
displayPetDistance();
} //end while
} //end main
displayPetDistance()
{
int d;
LCDL1Home();
for(d = 0; d<15; d++){
LCDPut(distance2PetString[d]);
}
}
File 2
gps_read.c
Code:
#include <p24fj128ga010.h>
#include <math.h>
#include "gps_read.h"
#define EarthRadius (6378100) // meters
#define Degrees_2_Radians (0.017453293) // pi/180
#define Radians_2_Degrees (57.29577951) // 180/pi
float Get_Distance( float lat1, float long1, float lat2, float long2)
{
float dLat, dLong, sideA, sideC;
lat1 *= Degrees_2_Radians; // 0.716185 value of variable at this point
long1 *= Degrees_2_Radians; // 1.48643
lat2 *= Degrees_2_Radians; // 0.71636
long2 *= Degrees_2_Radians; // 1.48661
dLat = lat2 - lat1; // 0.000175
dLong = long2 - long1; // 0.00018
sideA = (sinf(dLat/2)) * (sinf(dLat/2)); // 7.65625e-9
sideA = sideA + ((cosf(lat1)) * (cosf(lat2)) * (sinf(dLong/2)) * (sinf(dLong/2)));
sideC = 2 * atan2f( sqrt(sideA), sqrt(1-sideA) );
distance = (EarthRadius*sideC); // meters 1391.261
return distance;
}
header file gps_read.h