# Introduction printf and scanf are the first things you will come into contact with when learning C language. These two functions can also be used on the microcontroller to implement the printing function. It needs to be implemented in combination with the serial port and also requires IDE support. Different IDEs implement scanf receiving and printf printing in different ways. This article will explain how to use KEIL to implement printf and scanf. The test environment of this article's experiment: - System: win10 - IDE: KEIL V5.34 - Microcontroller: Jihai APM407IGT6 # 1 Hardware connection First, determine the serial port connection. Download the data sheet of APM407, and you can see that USART1 is connected to PA9 (TX) and PA10 (RX). It happens that the official routine also uses USART1, so just take the official routine and modify it to complete the functions we need.
The connection lines are as follows: | USB-TTL | APM32F407 | | ------- | --------- | | RX | PA9 | | TX | PA10 | | GND | GND | > Note: The official routine is based on the MINI development board. Ours is a TINY development board, so the library used is different and needs to be modified. The method will be described later.
# 2 Software code writing To implement printf and scanf, it is essentially to implement fputc and fgetc functions. It only needs to implement the reception and transmission of a single character. The implementation code is as follows: ```c // fputs for printf or other print function in standard int fputc(int ch, FILE *f) { (void)f; /** Wait until end of transmission */ USART_TxData(TINY_COM1, (uint8_t)ch); while(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_TXBE) == RESET) {}; return ch; } // fgets for scanf or other input function in standard int fgetc(FILE *f) { uint8_t ch; (void)f; while(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_RXBNE) == RESET) {}; ch = USART_RxData(TINY_COM1); return (int)ch; } ``` The main function is implemented as follows: Function: - Initialize LED2 and LED3, which are off when powered on - Initialize serial port USART1 with baud rate of 115200 - Print information to the serial port, then receive an integer from the serial port and print it out through the serial port - After completion, LED2 and LED3 will light up```c int main(void) { APM_TINY_LEDInit(LED2); APM_TINY_LEDInit(LED3); USART_Init(); printf ("APM32F407 printf test\r\n"); printf ("Please input a number with int type\r\n"); int a; scanf ("%d", &a); printf ("The number you input is: %d", a); APM_TINY_LEDOn(LED2); APM_TINY_LEDOn(LED3); while(1) { } } ``` # 3 Notes## 3.1 Check Use MicroLIB If you need to successfully use the `printf` and `scanf` functions, you need to check the `Use MicroLIB` checkbox. MicroLIB` uses KEIL's built-in library, otherwise the program will freeze when using printf and scanf.
## 3.2 Modify the compiler macro The official Demo program uses the code of the MINI board. Our board is a TINY board, so you need to modify the KEIL compiler macro to `APM32F407_TINY` to locate the header file included in the TINY board. > Note: All the parts related to MINI in the code can be changed to TINY.
# 4 The implementation effect is as follows. The test is successful. You can use printf to print and scanf to receive commands later.