I am developing a C code for a microcontroller, this code takes input from sensors and outputs the data from the sensors along with other strings on an alphanumeric character LCD. I generally use sprintf for this, but I came to notice that when using sprintf to format floats into strings, it takes up too much program memory space, which is quite low on a microcontroller. (By too much I mean jumping straight from 34% of program memory to 99.2%)
So my question is, is there a less-space taking method to format floats into strings? I only care about how simple the method is.
I use MPLABX IDE with XC8 compiler on a PIC16F877a 8-bit MCU.
Thanks a lot in advance.
Is there a less-space taking way than using sprintf to format floats to strings?
... code takes input from sensors and outputs the data from the sensors along with other strings on an alphanumeric character
Do not use floating point at all. @user3386109
The reading from the sensor is certainly an integer. Convert that reading to deci-degrees C using integer math and then print.
TMP235 example
Temperature Output
-40 C 100
0 C 500
150 C 2000
#define SCALE_NUM ((int32_t)(150 - -40) * 10)
#define SCALE_DEN (2000 - 100)
#define OFFSET (500)
int temperature_raw = temperature_sensor();
int temperature_decidegreesC = (temperature_raw - OFFSET)*SCALE_NUM/SCALE_DEN;
send_integer(temperature_decidegreesC/10);
send_char('.');
send_char(abs(temperature_decidegreesC/10) + '0');
Other improvements can be had, but avoiding FP variables and math and using integer math is the key.