8 Replies Latest reply on Jan 13, 2017 11:14 AM by schutki_2239266

    Can I use printf to print to terminal?

    user_302397898

      Starting with the Example program UART_Tx01 I successfully printed its output to a Teraterm window. I used an FTDI USB to TTL Serial Cable.

         

      But I would like to print floating point numbers to the terminal using printf. The project build okay using:
       printf("Test printf function. float:%f \n",f);
      where f is a float variable. But nothing prints on the terminal.
      The LCD_Char_1_PrintInt8(count); function works fine but I want to format floating point output.

         

      I am using the CY8KIT-050 and PSoC Creator 3.3

        • 1. Re: Can I use printf to print to terminal?
          bob.marlowe

          Set "Use newlib nano Float Formatting" in project settings and increase heap size to at least 0x0200 bytes in .cydwr view, system tab.

             

           

             

          Bob

          • 2. Re: Can I use printf to print to terminal?
            user_302397898

            Thank you, Bob. I tried that but still no fprint to the Teraterm terminal. I increased heap size to 0x0800 in cydwr System tab. For future readers of this search, Bob is saying go to Project tab > Build Settings > Linker > General > Use newlib Float Formatting = True.

               

            The only display in Teraterm terminal is the LCD_Char_1_PrintInt8(count); that displays on the LCD of the development board.

               

            Attached is the project. The only change I made to the Example is to add the fprint statement in a loop. Oh. I also added #include <stdio.h> and included Build Settings > Linker > General > Additional Libraries > m

               

            I cannot find any reference to this problem is the search results. Any help is welcome.

            • 3. Re: Can I use printf to print to terminal?
              bob.marlowe

              Baud rate of your UART is non-standard, use during test phase 9600 baud.

                 

              Let the UART handle the interrupt, set Tx buffer to 80 and uncheck other interrupt sources

                 

              Check connection of port P0_1 with plugs on P5 serial tx line (2)

                 

              You have not provided the needed _write() function for GCC. How should C know that you want to have print going to your UART and not to another device

                 

              /* For GCC compiler revise _write() function for printf functionality */
              int _write(int file, char *ptr, int len)
              {
                  int i;
                  file = file;
                  for (i = 0; i < len; i++)
                  {
                      PuTTY_PutByte(*ptr++);   // send to your device
                  }
                  return len;
              }
              //***********************************************************************************//

                 

               

                 

              Bob

              1 of 1 people found this helpful
              • 4. Re: Can I use printf to print to terminal?
                user_302397898

                Bob, Your comment is quite helpful regarding "how should C know" where to direct print. I will experiment with this and close when I have a working model. Thank you.

                • 5. Re: Can I use printf to print to terminal?
                  user_302397898

                  Thanks again, Bob. I fixed it. I used sprintf()  instead of printf() to print floating point to a string. Then I  redirected output to UART instead of the LCD. I should have thought of that! I also added #include <string.h> but don't know if that's necessary So I have:

                     

                     #define CR (0x0Du) // carriage return character
                     #define LF (0x0Au) // define line feed character
                     float f;
                     ....
                      char num[] = "1.3456789012345"; // initialize the string to receive floating point data
                     char *p;
                          sprintf(num,"%-f", f);     // convert floating to string left justified
                          p = num;  // pointer to start of num string
                          /* The while loop will execute until a null end-of-string character is found */
                          while (*p) UART_1_WriteTxData(*p++); /* Sending the data */
                          UART_1_WriteTxData(CR); // write carriage return
                          UART_1_WriteTxData(LF); // write line feed

                     

                  How do we mark an answer as "Verified?"

                  • 6. Re: Can I use printf to print to terminal?
                    bob.marlowe

                    How do we mark an answer as "Verified?" I don't know, but I am quite content as long as you don't flag it as "offensive" ;-)

                       

                     

                       

                    Bob

                    • 7. Re: Can I use printf to print to terminal?
                      jacek.f.ostrowski_1501976

                      Hi Bob, Thanks for your comment on increasing the heap size, it helped a lot. Jacek

                      • 8. Re: Can I use printf to print to terminal?
                        schutki_2239266

                        Yeah usually strange behavior is indication to suspect on heap..

                           

                        But for terminal window try http://docklight.de/  at least it is my favorite :)