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