Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
In MCU is used in the most basic is the serial communication interface, speaking of a serial port will have to mention one of the most commonly used functions in a serial port is print function printf () function, use this function is usually called directly library function, how to use in the single-chip microcomputer printf () function is usually in the serial port mapping. To use the printf() function in serial port 1, you can use the following code for remapping.
// Add the following code to support the printf function without selecting use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
// Support functions required by the library
struct __FILE
{
int handle;
};
FILE __stdout;
// Define _sys_exit() to avoid using semi-host mode
_sys_exit(int x)
{
x = x;
}
// redefine the fputc function
int fputc(int ch, FILE *f)
{
while((USART1->SR & 0X40) = =0); // Loop until finished
USART1->DR = (u8) ch;
return ch;
}
#endif
Copy the code
After adding the above code to the c file in serial port 1, you can print through serial port 1 when you use the printf () function. Can you implement printf () without using a library function? Printf () : printf () : printf () : printf () : printf ()
void UART2_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 1. Serial clock enable GPIO clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// 2. The serial port reset
USART_DeInit(USART2);
// 3. GPIO port Settings
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // reuse push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PA3 RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // Float input
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 4. Initialize the serial port parameters
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
// Initialize NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 6. Enable interrupt
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
// enable the serial port
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) ! = RESET) { res = USART_ReceiveData(USART2); USART_SendData(USART2, res);// Send the received data}}Copy the code
Initialize serial port 2 in the same way as normal, and then customize a function to implement the function printf ().
// Customize the printf function for serial port 2
char UART2_TX_BUF[200];
void u2_printf(char* fmt, ...) An ellipsis can be used to specify the argument list when the types and numbers of all arguments to the transfer function cannot be listed
{
u16 i, j;
va_list ap; //va_list is a character pointer, which can be interpreted as a pointer to the current parameter.
va_start(ap, fmt); // The va_start function is used to obtain the parameters in the parameter list, and then va_end() is called
vsprintf((char*)UART2_TX_BUF, fmt, ap); // Store the generated formatted string here
va_end(ap);
i = strlen((const char*)UART2_TX_BUF); // The length of the data sent this time
for(j = 0; j < i; j++) // Loop to send data
{
while((USART2->SR & 0X40) = =0); // Loop until finishedUSART2->DR = UART2_TX_BUF[j]; }}Copy the code
The function name is defined as: u2_printf(char* FMT…) Ellipsis indicates that the current parameter is a variable parameter. Now you can use this function directly to print the data. Test the function of the serial 1 and serial 2 printf functions with a code in the main function.
int main(void)
{
u8 t;
u8 len;
u16 times = 0;
delay_init(); // Delay function initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
UART2_Init(115200);
printf("USART1 AND USART2 TEST!!!");
while(1)
{
if(USART_RX_STA & 0x8000) { LED1 = ! LED1; len = USART_RX_STA &0x3fff; // Get the length of the received data
printf("\r\n You sent the message :\r\n");
for(t = 0; t < len; t++)
{
USART_SendData(USART1, USART_RX_BUF[t]); // Send data to serial port 1
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) ! = SET);// Wait for the delivery to complete
}
printf("\r\n");
USART_RX_STA = 0;
}
else
{
times++;
if(times % 5000= =0)
{
printf("Serial port experiment \r\n");
u2_printf("Serial port experiment \r\n"); // Call serial 2 printf
}
if(times % 300= =0)
{
printf("Please enter data, end with enter key \r\n");
u2_printf("Please enter data, end with enter key \r\n"); // Call serial 2 printf
}
if(times % 30= =0) LED0 = ! LED0; delay_ms(10); }}}Copy the code
Output the same prompt message through serial port 1 and serial port 2. Then use serial port assistant to send data to serial port 1 and serial port 2 respectively. When serial port 1 and serial port 2 receive data, they will print it out through the serial port. The message output from serial ports 1 and 2 is printed using printf function.
The printf function of serial port 2 is the same as the printf function of serial port 1. You can also print variable values using the printf function in serial port 2, such as adding a variable print line to your code.
Print the value of the variable times every time you output the prompt.
You can see that the value of The Times variable also prints normally.