最近在弄单片机的串口,想使用单片机的中断接收字符串,在网上查阅资料,汇总了一下,单片机型号为stm32f103rct6,IDE为keil5版本。
代码示例如下:
首先是串口初始化:里面包含了GPIO、NVIC和USART初始化的结构体及设置。
void MyUSART_Init()
{
/* 定义GPIO、NVIC和USART初始化的结构体 */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 使能GPIO和USART的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 将USART TX(A9)的GPIO设置为推挽复用模式 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 将USART RX(A10)的GPIO设置为浮空输入模式 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 配置串口 */
USART_InitStructure.USART_BaudRate=115200;