-
-
-
-
-
RCC_APB2Periph_AFIO, ENABLE);
然后再配置端口,在配置串口,再使能即可。
-
-
-
-
GPIO_InitTypeDef GPIO_InitStructure;
-
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
-
-
USART_InitTypeDef USART_InitStructure;
-
-
USART_InitStructure.USART_BaudRate = 115200;
-
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_ClockInitTypeDef USART_ClockInitStructure;
-
-
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
-
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
-
-
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
-
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
-
-
USART_ClockInit(USART1, &USART_ClockInitStructure);
-
-
-
USART_Init(USART1, &USART_InitStructure);
-
-
-
USART_Cmd(USART1, ENABLE);
-
-
串口配置完毕,为了使得能够使用 printf 进行打印,需要进行重定向:
在 stm32f10x_usart.c 中添加如下代码:
-
int fputc(int ch, FILE *f)
-
-
-
USART_SendData(USART1, (uint8_t) ch);
-
-
-
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
-
-
-
在 stm32f10x_usart.h 中添加stdio.h头文件,然后,添加声明 int fputc(int ch, FILE *f);
最后,在设置里面 Target 下面 勾选“Use MicroLIB” 就可以了。