【前提】
实际应用中,芯片型号为STM32F030CCT6,串口1用于模组通讯,串口2用于仪表通讯,串口3用于调试,log输出
串口1配置
void USART1_Config(u32 baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//开启时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); //引脚复用配置
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9| GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //串口的应用波特率最大115200.2M足够,既省电噪声也小
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_Init(&NVIC_InitStructure);
}
串口2配置
方式类似,此处省略…
串口3配置
void USART3_Config(u32 baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4); //引脚复用配置
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_4);
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_Init(USART3,&USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannel = USART3_6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_Init(&NVIC_InitStructure);
}
注意事项
- 开启引脚复用功能时,引脚为 GPIO_PinSourcex,不是 GPIO_pins_define,否则串口会配置失败,进入硬件中断,习惯使用STM32F103系列的,这里很容易出错
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4);
- 启动文件要选择startup_stmf030xc.s
中断向量表中有USART3_6_IRQHandler,否则产生中断时找不到中断入口地址
注:startup_stmf030.s中没有USART3_6_IRQHandler
- 需要打开STM32F030xC宏定义,才会有中断向量USART3_6_IRQn的定义
方式1:在stm32f0xx.h中打开注释 #define STM32F030xC
方式2:在工程魔法棒中添加
5. 因为串口3-6共用一个中断函数,当进入中断时,需要判断具体哪个串口
void USART3_6_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET)
{
USART3_ReceiveLongData();
USART_ClearFlag(USART3,USART_FLAG_RXNE);
}
}
6.在进行串口配置时,最好先初始化串口再进行IO口配置,串口没初始化前可能有浮空电平,导致设备刚上电时串口乱发数据的问题