Given that HD44780_PrintStr()
prints a string, you have to generate a string representation of the float
value voltage
.
Then you need to set the cursor to the end of the prefix string (column 6), and print the string representation. Trailing spaces at the end of the output will erase digits from any previous longer output.
Assuming the parameter order of HD44780_SetCursor()
are row, column
:
for(;;)
{
HAL_ADC_PollForConversion(&hadc1,1000);
readValue = HAL_ADC_GetValue(&hadc1);
voltage =(float)readValue/4095*16.5 ;
char vstr[16] ;
sprintf( vstr, "%.2f ", voltage ) ;
HD44780_SetCursor(0,6);
HD44780_PrintStr( vstr ) ;
HAL_Delay(100);
}
If you want leading zeros (fixed 2-digit width) in the integer part; that is a little more complex:
unsigned wholev = (int)voltage ;
unsigned fractv = (int)((voltage - wholev) * 100.0f + 0.5f) ;
char vstr[16] ;
sprintf( vstr, "%02u.%02u", wholev, fractv ) ;
HD44780_SetCursor(0,6);
HD44780_PrintStr( vstr ) ;
but in that case you do not need the trailing spaces to delete previous digits, because the string is always a fixed width "xx.yy"
. The code may also end up smaller if floating point support can be omitted from the sprintf
formatter.
Apart from the floating-point formatter adding a great deal of code and probably increase in stack usage, remember that your MCU lacks hardware floating point support so that the floating point operations will both add code and be rather slow. In simple applications it may not matter, but generally you should avoid (and know how to avoid) floating point operations in embedded code, especially if there is no FPU - even if there is an FPU there are reasons to avoid it for trivial uses such as this in any case.
The floating-point code can be removed simply as follows:
unsigned millivolts = readValue * 16500u / 4095u ;
unsigned wholev = millivolts / 1000u ;
unsigned fractv = ((millivolts + 5u) % 1000) / 10u ;
sprintf( vstr, "%02u.%02u", wholev, fractv ) ;
HD44780_SetCursor(0,6);
HD44780_PrintStr( vstr )
readValue
andvoltage
are only used in the while-loop, declare them there. e.g.uint32_t readValue = HAL_ADC_GetValue(&hadc1);
andfloat voltage = readValue / 4095.0f * 16.5f ;
. Please tell me they are not global?