I Have this “simple” code.
union
{
unsigned int res;
char bytes[2];
} ADC;
char ADC_num[5];
float temprature;
void vis_temp (void) // Show temp..
{
signed int l, length;
unsigned int rem;
GO_nDONE=1; // initiate conversion on the channel 0
while (GO_nDONE) continue;
ADC.bytes[0]=ADRESL;
ADC.bytes[1]=ADRESH;
utoa(ADC_num, ADC.res, 10);
temprature = (float) ADC.res * 478.1 / 1024;
temprature = temprature - 50.0;
l = (signed int) temprature;
temprature -= (float) l;
rem = (unsigned int)(temprature* 1e1);
sprintf(&ADC_num, "%i.%u", l, rem);
When reading ADC_res (voltage on pin, temperature sensor) that temperature is 0 degree or under then program writes “0.65500” instead of “-3.5” or similar.
I should have declared the right as signed and unsigned int.
Any hints to fix it, or have an other way of converting it.
Suppose now
tempraturehas a negative value-x.yz.Now
l = -x, andtemprature = -x.yz - (-x) = -0.yz.Multiply with 10, and convert to
unsigned int. Usually, that results in undefined behaviour (6.3.1.4 (1)):But converting the negative value to
unsigned intwould produce the wrong result anyway, even if the remaindering operation is done, what you want is the absolute value, so you should convertthere.