How to Transmit a String (Hello World Program) with the UART Communication Protocol with an STM32F446 Microcontroller Board in C

Nucleo STM32F446 board

In this article, we show how to transmit a string with the UART communication protocol with an STM32F446 microcontroller board in C.

UART stands for universal asynchronous receiver transmitter, and it is often used to transmit or receive data between a microcontroller and various components.

UART, in its most basic form, only needs 2 pin connections: Tx (for transmission) and Rx (for receiving).

Some pins on the STM32 board are completely dedicated UART pins. Other pins are USART pins, which are UART pins but with the additional capability of being synchronous (in addition to asynchronous). So USART pins have dual capabilities in that they can be run synchronously (with a clock) or asynchronously (without a clock).

If running asynchronously, the transmitting device (such as the STM32 microcontroller) and the receiving device must have the same baud rate (for uniform communication). If they don't have the same baud rate, the data will be corrupted.

UART is best used for short distances.

In this example, we will use the UART2_TX pin, which is realized by pin PA2 in alternate function mode to implement UART communication.

Previously, we used UART communication to transmit a single character. Now we take it a step further and transmit an entire string. We will transmit the classic, "Hello World" in this program.

Below is the alternate function mapping of the GPIO Port A of the STM32F446RE microcontroller.

Alternate function mapping of the GPIO Port A of the STM32F446 microcontroller board

So you can see that if we put pin PA2 in alternate function mode 7 (AF7), it will act as USART2_TX.

The great thing about using the USART2 pins is that we can transmit or receive data using the UART protocol via USB.

So just connecting the STM32F446 board to your computer using a USB, you can intercept UART communication using a software. We will use the RealTerm software, which is a serial and TCP terminal for engineering and debugging.

We need 2 major code files in order for this code to work.

We need our header file, which contains many macros and structures that describe the various registers and needed values.

We then need our main C file, which contains the code that allows us to output the s to the RealTerm terminal repeatedly in an infinite loop.

Below is the header file that we will need for our code, which in this case is named, stm32f446xx.h

So this header file contains all the definitions we need to create our main C file.

The contents of the main.c file is shown below.

The first thing we must do is include the stdint.h header file and our stm32f446xx.h header file in our main.c file.

We then have the function prototypes, so that the compiler knows which functions are present in our program and what they return and take in as parameters.

The __io_putchar() functions will allow us to write multiple characters when we use the printf() function.

Notice how it takes in an integer. This represents the ASCII character in integer form.

This function also returns an integer value, the ASCII character in integer form.

Within this function, we have our uart2_write() function, which prints each character one at a time.

We then have our main() function.

Within this main() function, we call the usart2_tx_interrupt_init(), which initializes uart communication.

We then have our infinite loop, which prints "Hello World" repeatedly.

Let's now go to our usart2_tx_interrupt_init() function.

Because we use pin PA2 to function in alternate function mode as USART2_TX, we must turn on the clock for GPIO Port A. We then must set PA2 to be in alternate function mode and set pin PA2 to function as USART2_TX.

Next we must enable the USART2 clock which we do by turnin gon bit 17 of the APB1ENR register.

We then set the baud rate using the compute_usart_bd() function. We set the baud rate to 115200

We then anble to the transmitter in the USART2 CR1 register.

We then enable the USART using turning on bit 13 of the USART2 CR1 register.

We then have our compute_usart_bd() function, which computes the baud rate, which we previously put in the USART2 BRR register.

We then have our uart2_write() function, which writes a character at a time to UART terminal.

As stated before, we use the RealTerm software to output this "Hello World" string.

Remember to set the Baud Rate to 115200 on the RealTerm software and to select the appropriate USB port on your computer.

You may have to press the 'Open' button to close and reopen the port to reset the software.

When the program is run, you should see the following output below.

RealTerm serial capture program- UART communication of string 'Hello World' in an infinite loop with an STM32F446 microcontroller board

So this is how to transmit a string, in this example "Hello World" with the UART communication protocol with an STM32F446 microcontroller board.

Related Resources

HTML Comment Box is loading comments...