Can I use printf to print to terminal?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
JeCo_264681
Level 5
Level 5
100 replies posted 50 replies posted 25 replies posted

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

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

View solution in original post

0 Likes
8 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.

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.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

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.

0 Likes

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?"

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
JaOs_1501976
Level 1
Level 1

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

0 Likes
Anonymous
Not applicable

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

   

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

0 Likes