0% found this document useful (0 votes)
79 views3 pages

Printf Function by STM32

This document discusses using the "printf" function to output variables and debug code on an STM32 microcontroller. It first provides an example of echoing serial input from a PC to demonstrate serial communication is working properly. It then shows how to create a "printf" function by overwriting the _write function to transmit output over the UART to the PC. This allows printing variable values or text using printf to debug STM32 code directly from the microcontroller to the computer terminal.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
79 views3 pages

Printf Function by STM32

This document discusses using the "printf" function to output variables and debug code on an STM32 microcontroller. It first provides an example of echoing serial input from a PC to demonstrate serial communication is working properly. It then shows how to create a "printf" function by overwriting the _write function to transmit output over the UART to the PC. This allows printing variable values or text using printf to debug STM32 code directly from the microcontroller to the computer terminal.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

5/4/22, 10:33 AM "printf" function by STM32.

JUNGCHAN LEE Jun 29, 2020 2 min read

"printf" function by STM32.


Updated: Jul 5, 2020

If somebody want to take some variable in the STM32 code, he might use the output devices to check the
variable. However, the output devices such as segment, LCD, or something else might have their own
function; they can not always display the variable. However, for the debugging, the PC, can be use as a output
by serial communication. The post I wrote before is about the serial communication. Using this serial
communication, we can find the variable of the stm32 and debugging easily. Firstly, let's discuss about the
echo test. The echo test is for the test which the devices properly react from the signal of PC.

file:///home/oscarj/Desktop/"printf" function by STM32.html 1/3


5/4/22, 10:33 AM "printf" function by STM32.

*All the code need USART connection with PC. Refer to the previous post.

I give you a code to receive a character from the computer and send the next character of the ASCII when the
serial communication is occurred.

/* USER CODE BEGIN PV */

extern UART_HandleTypeDef huart2; //the variable of huart2.

/* USER CODE END PV */

int main(void)

HAL_UART_Transmit(&huart2, "type the character",18, 1000);

uint8_t ch;

while(1)

if(HAL_UART_Receive(&huart2, &ch, 1, 1000)==HAL_OK) //get


value.

ch++; //increase the ASCII number.

HAL_UART_Transmit(&huart2, ch, 1, 1000); //print the


increased value.

else

This code send the echo of the user's input. If you input "a" on the Tera term, you can get "b" on the screen.
Then, let's make a "printf" function on the stm32.

I give a code of "printf."

int _write (int file, char *ptr, int len)

HAL_UART_Transmit(&huart2, ptr, len, 1000); //print the input


ptr.

return len; //return length.

int main(void)

file:///home/oscarj/Desktop/"printf" function by STM32.html 2/3


5/4/22, 10:33 AM "printf" function by STM32.

printf("Hello world \r\n"); //print hello world on the screen

uint8_t ch;

while(1)

if(HAL_UART_Receive(&huart2, &ch, 1, 1000)==HAL_OK) //get


value.

printf("ur input = %c \r\n", ch); //print the value.

else

it's well work.

file:///home/oscarj/Desktop/"printf" function by STM32.html 3/3

You might also like