Displaying a Float Value on LCD Using PSoC® 3 or PSoC 5LP – KBA94460

Version 1
    Version: **

     

    Question: How do I convert a float value to ASCII and display it on an LCD in PSoC Creator using PSoC 3 or PSoC 5LP?

     

    Answer:

     

    To display a float value on LCD, you first need to convert the float value to a string using the sprintf function.

    Use the following snippet of code to display a float value (say -1.2345) on the LCD.

    #include <project.h>#include <stdio.h> int main() {  /* Place your initialization/startup code here (e.g. MyInst_Start()) */     char tstr[16];  float compValue = -1.2345;  LCD_Start();  LCD_Position(0,0);  sprintf(tstr, "%+1.4f", compValue ); //converts floats to ASCII and stores it in char array tstr LCD_PrintString(tstr);  for(;;)  {  /* Place your application code here. */ } }

    Now, -1.2345 will be printed on the LCD.