Rewrite the serial port experiment program of punctual atomic STM32F103ZET6 today.
The source program uses serial port 1 for experiments, and it is modified to use serial port 3 for experiments.
Because of carelessness, a strange error occurred. It took an hour with the help of a friend to understand the cause of the error. .
(Actually, I have long known that redefining the function fputc from USART1 to USART3 can solve the problem, but I found the cause of the error step by step through the breakpoint in the main function. I have mastered the basic method of program debugging. Here, Thanks friends!)
process:
(1) Replace "USART1" in main.c, usart.h and usart.c with "USART3";
(2) Enable GPIOB and USART3 of serial port 3 in usart.c.
question:
In the redefinition function in usart.c, USART1 has not been changed:
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0)
;//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
results in the program:
else
{
times++;
if(times%5000==0)
{
printf("\r\n串口实验\r\n");
printf("正点原子\r\n\r\n");
}
if(times%200==0)
{
printf("请输入数据,以回车键结束\n");
}
if(times%2==0)LED0=!LED0;//
delay_ms(100);
}
When times == 200, execute the printf statement and enter the redefined function fputc, causing the program to enter an infinite loop.
solve:
Of course, change the USART1 in the redefine function fputc to USART3.