Hy,
I have a problem with my code. When I simulate it in MPLAB it works but when I put the program in uC it behaves strange. The code is:
#define A (3.9083e-3)
#define B (-5.775e-7)
float c, qc, c1, c2, c3, ttla;
signed int temperatura_tla;
unsigned short int tRef, tRsen; // values from a counter
char templ, temph;
//usually couter gives tRef=0x0A0A, tRsen= 0x0AEE;
//some calculations
c1 = 1 - ((float)tRsen)/((float)tRef);
c2 = 4*B*c1;
c3 = A*A;
c = c3-c2;
qc= sqrt(c);
ttla = (-A + qc)/(2*B);
temperatura_tla = (int)ttla; //converting float to int????
if (ttla > 0) temperatura_tla++;
// Simulation gives for temperatura_tla= 0x0016, what is ok
templ = (temperatura_tla & 0xFF); // LSB
temph = (temperatura_tla >> 8) & 0xFF;
ConsolePutROMString( (ROM char *)"Izmjerena temperatura tla je ");
PrintChar(temph);
PrintChar(templ);
ConsolePutROMString( (ROM char *)"\r\n");
//after this I get on console for temperatura_tla=0x7FFF or sometimes 0xFF0A
//that's wrong
Ok, now, it's maybe problem in shifting the signed int, or in converting float to int? I don't now how temperatura_tla becomes 0X7FFF... Can somebody give me advice what to do?
I have a problem with my code. When I simulate it in MPLAB it works but when I put the program in uC it behaves strange. The code is:
#define A (3.9083e-3)
#define B (-5.775e-7)
float c, qc, c1, c2, c3, ttla;
signed int temperatura_tla;
unsigned short int tRef, tRsen; // values from a counter
char templ, temph;
//usually couter gives tRef=0x0A0A, tRsen= 0x0AEE;
//some calculations
c1 = 1 - ((float)tRsen)/((float)tRef);
c2 = 4*B*c1;
c3 = A*A;
c = c3-c2;
qc= sqrt(c);
ttla = (-A + qc)/(2*B);
temperatura_tla = (int)ttla; //converting float to int????
if (ttla > 0) temperatura_tla++;
// Simulation gives for temperatura_tla= 0x0016, what is ok
templ = (temperatura_tla & 0xFF); // LSB
temph = (temperatura_tla >> 8) & 0xFF;
ConsolePutROMString( (ROM char *)"Izmjerena temperatura tla je ");
PrintChar(temph);
PrintChar(templ);
ConsolePutROMString( (ROM char *)"\r\n");
//after this I get on console for temperatura_tla=0x7FFF or sometimes 0xFF0A
//that's wrong
Ok, now, it's maybe problem in shifting the signed int, or in converting float to int? I don't now how temperatura_tla becomes 0X7FFF... Can somebody give me advice what to do?