I am using a DS18B20 to read a temperature. It is easy to get a float value and then use the normal sprintf to format that into a string. However sprintf uses humungous amounts of flash. I found a much cut down version which doesn't do floats so I want to extract the sign, units, and decimal parts which I can then use with %d in the replacement sprintf. The code below does seem to work but it is horrible. There must be a better way - please!
[Edit] This is C code for the microchip XC8 compiler.
The DS18B20 output format is as below:
[Edit] This is C code for the microchip XC8 compiler.
Code:
uint16_t Tem = 0b1111010101011100;
bool Sign = Tem & 0xF000;
char Decimal = ((Tem & 0x000F) * 10) >> 4;
Tem &= 0x0FFF;
Tem = Tem >> 4;
if(Sign)
{
Temp = -Tem;
}
sprintf(LCDbuf, "%3d.%d%cC\0", Temp, Decimal, 0xDF);
Last edited: