Using printf Function in TrueStudio: A Step-by-Step Guide

When using the printf function in the TrueStudio development tool for the first time, you may find it challenging to get started. Here, I will explain the process of configuring STM32CubeMX to generate a TrueStudio project for serial output.

Create a Basic Project in STM32CubeMX: Open the software and select the configuration for Serial Port 1 under the Pinout tab as shown in the figure.

A. Select the external crystal oscillator as the system clock source.

Using printf Function in TrueStudio: A Step-by-Step Guide

B. I am currently using the JLINK SWD mode for program debugging, so I choose the following:

Using printf Function in TrueStudio: A Step-by-Step Guide

C. Configure Serial Port 1 for asynchronous communication mode.

Set the Microcontroller Operating Frequency: Set it to a main frequency of 72MHz as shown in the figure below.

Using printf Function in TrueStudio: A Step-by-Step Guide

Configure Serial Port Parameters in the Configuration Tab:

Open USART1,

Using printf Function in TrueStudio: A Step-by-Step Guide

The basic configuration is complete. Now, start generating the TrueStudio project. Open the project settings to set the path where the program will be saved, the name, the environment support, stack size settings, and the independent creation of .c/.h files, as shown in the figure below:

Using printf Function in TrueStudio: A Step-by-Step Guide

After completing the settings, click the icon shown below to start generating the project.

Using printf Function in TrueStudio: A Step-by-Step Guide

The project is generated and compiled without errors.

Using printf Function in TrueStudio: A Step-by-Step Guide

Start Writing and Setting Up to Support printf Output:

First, I add the following function in usart.c to enable TrueStudio to support printf output. Note: The code must be added in the USER section.

Using printf Function in TrueStudio: A Step-by-Step Guide

// Note that the first parameter is &huart1, as STM32CubeMX automatically generates Serial Port 1.

Using printf Function in TrueStudio: A Step-by-Step Guide

Now you can directly output data using printf, but floating-point numbers cannot be output. We can make the following settings to perfectly support the output of floating-point numbers. In the project properties, find C/C++ Build –Settings–C Linker–Miscellaneous—Other options and fill in: -u_printf_float. At this point, TrueStudio can support all data types for printf output.

Program Output:

Using printf Function in TrueStudio: A Step-by-Step Guide

The effect is shown below:

Using printf Function in TrueStudio: A Step-by-Step Guide

Having found the direction, it’s time to take action.

Using printf Function in TrueStudio: A Step-by-Step Guide

↓↓↓ Click “Read More” for more details and valuable information.

Leave a Comment