4 Replies Latest reply on Mar 26, 2020 8:38 AM by JoMe_264151

    Unable to print float using uart

    NaAl_4668831

      Hi Team,

       

      I am not able to print a float variable using the UART communication. Printing of integer and String works fine, but not the float. I have followed the build setting using this link : Float Print on UART . But I am not able to receive an output. The target device is CY8C5888LTI-LP097. The code description is as folllows

       

      ++++++++++++++++++++++++++++++++++++++++++++

      #include "project.h"

      #include "stdio.h"

      uint8 x=0;

      char b;

      CY_ISR(interruptRX)

      {

          b = UART_GetChar();

      }

      int main(void)

      {

          CyGlobalIntEnable; /* Enable global interrupts. */

       

       

          /* Place your initialization/startup code here (e.g. MyInst_Start()) */

          UART_Start();

          isrRX_StartEx(interruptRX);

          char to_pri[32];

          char det[] = "detected";

          //int a;

          float y1 = 0.95;

          float y2 = 0.19;

          //float vol;

          //float res;

          //char b;

       

       

          for(;;)

          {

               if(b == '1')

              {

                  //LED_BLINK_Write(!LED_BLINK_Read());

                  //CyDelay(100);

                  LED_BLINK_Write(1);

                  printf(to_pri,"%f\n",y1);

                  UART_PutString(to_pri);

              }

              if(b == '0')

              {

                  LED_BLINK_Write(0);

                  sprintf(to_pri,"%f\n",y2);

                  UART_PutString(to_pri);

              }

      }

       

      +++++++++++++++++++++++++

      This is the output i receive at the terminal:

      Capture1.JPG

       

      Can you guys help me to print the value y1 and y2.

       

      Regards,

      Alexander.

        • 1. Re: Unable to print float using uart
          LePo_1062026

          Alexander,

           

          Can you share your project with us?

           

          I sounds like you followed the forum Float Print on UART recommendations about enabling the printf_float library.  Did you increase the heap space also?

           

          The only thing I noticed is that you used a printf() instead of sprintf() statement in the if( b = '1') conditional.  I assumed it was a typo.

           

          I also notice the third line of your output lists "0.1".

           

          Additionally,  you might want to clear your input character b in each conditional.  Otherwise you will to print to the Terminal continuously without new input.

           

          Len

          • 2. Re: Unable to print float using uart
            MoTa_728816

            Hi,

             

            Could you also read my old memo when you have time?

            printf and float rhapsody (aka, yet another printf and floating topic)

             

            moto

            P.S.

            >             printf(to_pri,"%f\n",y1);

            this also might have been

            <           sprintf(to_pri,"%f¥n",y1);

             

            • 3. Re: Unable to print float using uart
              LePo_1062026

              Alexander,

               

              There were multiple problems with your main() code as explained in my earlier post.  However the biggest regarding the print float issue is that you needed to increase the Heap size.  You will find the heap size parameter in the Design Wide Resource (DWR) System Tab.  Change it to 0x200.  Before changing the heap, I basically received something similar to your output.  Apparently it takes more than 0x80 bytes of heap to process a float in sprintf().

               

              I fixed some of the other issues as well.

              Try this code:

              #include "project.h"
              #include "stdio.h"
              uint8 x=0;
              char b;
              
              CY_ISR(interruptRX)
              {
                  b = UART_GetChar();
              }
              
              int main(void)
              {
                  CyGlobalIntEnable; /* Enable global interrupts. */
                  /* Place your initialization/startup code here (e.g. MyInst_Start()) */
                  UART_Start();
                  isrRX_StartEx(interruptRX);
                  char to_pri[32];
                  char det[] = "detected";
                  //int a;
                  float y1 = 0.95;
                  float y2 = 0.19;
                  //float vol;
                  //float res;
                  //char b;
              
                  UART_PutString("Float Test\r\n");
              
                  for(;;)
                  {
                      if(b == '1')
                      {
                          //LED_BLINK_Write(!LED_BLINK_Read());
                          //CyDelay(100);
                          LED_BLINK_Write(1);
                          sprintf(to_pri,"%f\r\n",y1);
                          UART_PutString(to_pri);
                          b = '\0';    // Reset input. this is to prevent continuous output.
                      }
              
                      if(b == '0')
                      {
                          LED_BLINK_Write(0);
                          sprintf(to_pri,"%f\r\n",y2);
                          UART_PutString(to_pri);
                          b = '\0';    // Reset input. this is to prevent continuous output.
                      }
                  }
              }
              

               

              Len

              • 4. Re: Unable to print float using uart
                JoMe_264151

                Most errors regarding printf of a float can be removed by increasing the heap size (Creator ->ystem_Heap size) to at least 256 bytes.

                 

                Bob