5 Replies Latest reply on Jan 27, 2015 9:24 PM by dennis.nguyen

    output a float number onto LCD

    dennis.nguyen
        • 1. Re: output a float number onto LCD
          user_1377889

          Welcome in the forum!

             

          Easiest solution without any formatting is to use ftoa().

             

          More complex but much more universal is sprintf(). Formatting specifiers here: publications.gbdirect.co.uk/c_book/chapter9/formatted_io.html

             

          Internal enabling see here : www.cypress.com/

             

          So increase stack and address the linker addition.

             

           

             

          Bob

          • 2. Re: output a float number onto LCD
            user_14586677

            Usefull formatting discussion, attached.

               

             

               

            Regards, Dana.

            • 3. Re: output a float number onto LCD
              dennis.nguyen

               First, just want say sorry because I created the thread but couldnt type anything on the post due to some weird error on my web browser (Google Chrome).Really appreciate your reply. 

                 
                   I'm new to community and was struggling with the sprinf() whole day. I'm writing a code to implement a stopwatch on PSoC 5LP kit. I used a timer block to count the time between the Start and Stop buttons are pressed. Stopwatch is the Timer block. the code is roughly like below:   
                 
                      
                 
                  
                   #include "stdio.h"    
                  
                   #include <project.h>    
                  
                   #define TRUE 1    
                  
                   #define FALSE 0    
                  
                   #define STOPWATCH_FREQ 10000    
                  
                   asm (".global _printf_float"); //adds ~8000 bytes to the program    
                  
                        
                  
                   int main()    
                  
                   {    
                  
                       //Set to TRUE if the stopwatch has started, else FALSE    
                  
                       uint8 started_b = FALSE;    
                  
                       //Temporary storage for status register    
                  
                       uint8 reg;    
                  
                       //Capture/counter read value    
                  
                       uint32 capture;    
                  
                       //Capture/counter converted to seconds    
                  
                       float seconds;    
                  
                       //Temporary string for sprintf to use    
                  
                       char tstr[16];    
                  
                       /* Place your initialization/startup code here (e.g. MyInst_Start()) */    
                  
                       SecondTimer_Start();    
                  
                       Stopwatch_Start();    
                  
                       Display_Start();    
                  
                       StopwatchStart_Read();    
                  
                       Display_Position(0, 0);    
                  
                       Display_PrintString("Stopped")      ;    
                  
                       /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */    
                  
                       for(;;)    
                  
                       {    
                  
                           if(!started_b)    
                  
                           {    
                  
                               //If the stopwatch isn't known to have started yet,    
                  
                               //see if the start button has been pressed. If it has    
                  
                               //been pressed, the stopwatch has started.    
                  
                               if(StopwatchStart_Read())    
                  
                               {    
                  
                                   started_b = TRUE;    
                  
                                   Display_ClearDisplay();    
                  
                                   Display_Position(0, 0);    
                  
                                   Display_PrintString("Started")      ;    
                  
                               }    
                  
                           }    
                 
                 
                  
                   //Else, check to see if the stopwatch has been stopped.    
                  
                               reg = Stopwatch_ReadStatusRegister()      ;    
                  
                               //If the stopwatch has been stopped, first read the capture    
                  
                               if(reg & Stopwatch_STATUS_CAPTURE)    
                  
                               {    
                  
                                   capture = Stopwatch_ReadCapture();    
                  
                                   started_b = FALSE;    
                  
                               }    
                  
                               //Else, just read the counter    
                  
                               else    
                  
                               {    
                  
                                   capture = Stopwatch_ReadCounter();    
                  
                               }    
                  
                               //Convert the capture/counter to seconds and print it    
                  
                               capture = Stopwatch_ReadPeriod() - capture;    
                  
                               if(capture != 0)    
                  
                               {    
                  
                                   seconds = capture / (float)STOPWATCH_FREQ;    
                  
                                   Display_Position(1, 0);    
                  
                                   sprintf(tstr, "%1.4f", seconds);    
                  
                                   Display_PrintString(tstr);    
                  
                               }    
                  
                               //If the stopwatch has stopped, reset it    
                  
                               if(!started_b)    
                  
                               {    
                  
                                   StopwatchReset_Write(1);    
                  
                                   StopwatchStart_Read();    
                  
                                   Display_Position(0, 0);    
                  
                                   Display_PrintString("Stopped")      ;    
                 
                 
                      
                 
                      
                 
                   For some reasons, the whole project seems to be working but the "tstr" is not displayed on the LCD. I can see the value of "seconds" and "capture" are updated as soon as I press Start and Stop buttons (SWStart, SWStop).   
                 
                   I tried to manually debug it by replacing the "seconds" by an integer and it works. So I suspect that the spintf function just doesnt like the float value.   
              • 4. Re: output a float number onto LCD
                user_14586677

                You have enabled the nano support in the library -

                   

                 

                   

                    

                   

                          

                   

                http://www.cypress.com/?id=4&rID=87354     nano library

                   

                 

                   

                 

                   

                Regards, Dana.

                • 5. Re: output a float number onto LCD
                  dennis.nguyen

                   Hi Dana,

                     

                  Yes I intentionally added another 8K of flash into my code so that it can handle printing or scanning floating point. 

                     

                  The result is same when i adjust the linker files by following your link.

                     

                  Thank you for your help. 

                     

                  Cheers.