在stm32中如何设置波特率?即波特率是如何计算的?在设置波特率的过程中出现问题,求解答,谢谢!
以串口1的为例,库函数操作:
//串口初始化函数,输入参数波特率,一般是9600
void USART1_InitConfig(uint32 BaudRate)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//使能串口的RCC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
//串口使用的GPIO口配置
//设置USART1 Rx (PA.10)为悬浮输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//设置 USART1 Tx (PA.09)为上拉推出
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);
//配置串口:波特率、数据位、停止位、校验、流控制、模式
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);//配置串口1
//使能串口接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//串口发送中断在发送数据时开启
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
//使能 USART1
USART_Cmd(USART1, ENABLE);
//串口中断配置
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
波特率计算公式以及讲解请参考:http://www.cnblogs.com/cposture/p/4268910.html
非常感谢