3 Replies Latest reply on Nov 25, 2018 7:17 PM by bojec_3707286

    Float Print on UART

    bojec_3707286

      Hello friends,

       

      This is first time that i wrote question on community , pls understand and let me know if there is something not enough

       

      I am testing an ADC with PSoc 4 pioneer Kit ( CY8CKIT-042 ) and i think measuring the value is successful, but i can't print float to UART

       

      first i'll add my code

       

      #include "project.h"
      #include "stdio.h"
       
      float32 adout ;
      uint32 channel ;
      int16 output;
      float32 vo ;
      char arr[20] ; 
      char vo_arr[20];
       
      int main(void)
      {
          CyGlobalIntEnable; /* Enable global interrupts. */
       
          UART_Start();
          UART_UartPutString( " ADC TEST START ! \n" );
          
          ADC_Start();
          ADC_StartConvert();
         
          for(;;)
          
          {
                  if( ADC_IsEndConversion(ADC_RETURN_STATUS) == 1)
                  {
                      output = ADC_GetResult16(channel);
                      sprintf(arr, "ADout : %d\r\n" , output );
                      UART_UartPutString(arr);
                      
                      vo = ADC_CountsTo_Volts(channel, output);
                      sprintf(vo_arr, "Vout : %f\r\n" , vo );
                      UART_UartPutString(vo_arr);
                      
                      CyDelay(2000);
                      
                  }
          }
      }
      

       

      This code gives me this outuput on the monitorSerial_Mon_SC1.PNG

       

      So i tried Debug and check the variable , and add some screenshot below

       

      output_conver_done.pngVO_convert_Done.pngoutput_array.png

       

      VO_Db_SC_Arr.png

      I think everything is perferct except, putstring(float) , what should i have to do ?

        • 1. Re: Float Print on UART
          MoTa_728816

          Hi,

           

          I usually use following method to print float value in MCU program

          ======

                      sprintf(str, "(int format) %d.%02d  ", (int)fvalue, ((int)(fvalue * 100.0 + 0.5))%100) ;

                      UART_UartPutString(str) ;

          ======

           

          But in the community someone taught us that there is a linker flag for floating format

          Use_newlib-nano_Float.JPG

           

          So I tried with following program

          =======================

          #include "project.h"

          #include <stdio.h>

          char str[128] ; /* print buffer */

           

          int main(void)

          {

              float32 fvalue ;

             

              CyGlobalIntEnable; /* Enable global interrupts. */

              UART_Start() ;

             

              sprintf(str, "Float Format Test (%s %s)\n", __DATE__, __TIME__) ;

              UART_UartPutString(str) ;

           

              for(;;)

              {

                  for (fvalue = 0.0 ; fvalue < 10.0 ; fvalue += 0.01) {

                      sprintf(str, "(int format) %d.%02d  ", (int)fvalue, ((int)(fvalue * 100.0 + 0.5))%100) ;

                      UART_UartPutString(str) ;

                      sprintf(str, "(float format): %.2f\r\n", fvalue) ;

                      UART_UartPutString(str) ;

                      CyDelay(1000) ;

                  }

              }

          }

          =======================

           

          With "Use newlib-nano Floating Formatting = False",

          Naturally the result was

          without_float_format_option.JPG

           

          Then I tried with "Use newlib-nano Floating Formatting = True"

          Linker_float_option_yes.JPG

           

          The result was

           

          with_float_format_option.JPG

           

          First the good news was that when fvalue = 0.0, yes it printed "0.00".

          Second the bad news was that when fvalue = 0.01, program crashed and ran to exception.

          Probably I was missing some other necessary step(s) to make floating point work,

          but since my usual method is serving me enough, I resigned here.

           

          So for my conclusion, it's safe using the method I wrote at the beginning.

          ==========

          sprintf(str, "(int format) %d.%02d  ", (int)fvalue, ((int)(fvalue * 100.0 + 0.5))%100) ;

          UART_UartPutString(str) ;

          ==========

          moto

          • 2. Re: Float Print on UART
            MoTa_728816

            Hi,

             

            I found following two threads

            https://community.cypress.com/message/44036#44036

            https://community.cypress.com/thread/31761?start=0&tstart=0

             

            So I did

            (1) Linker option

            build_setting_linker_OK.JPG

            (2) Linker Command

            linker_command_line.JPG

            (3) Expand heap

            Edit Generated_Source / cyfitter.h

            CYDEV_HEAP_SIZE from 0x80 -> 0x200

            cyfitter_heap.JPG

             

            Then the result was

            working_log.JPG

             

            So for the first time, I could make "%f" work.

            (Although I'd rather stick with int format method)

             

            moto

            • 3. Re: Float Print on UART
              bojec_3707286

              Hello TanaKa

               

              I really appreciate for your reply, your solutions worked for me

               

              but for me , basic option for "Use newlib-nano Floating Formatting was false , and i tried to change it to true , mcu also goes to crash as your one does,

               

              i think your sprintf argument will be best fit for me,

               

              Thank you very much,

               

              Regard Bongjin,