[STM32F1]
如何STM32标准库串口运行中修改波特率?
[复制链接]
1074|2
电梯直达
楼主
楼主|
肥羊
发表于 2025-3-10 17:04
|
只看该作者
|倒序浏览
|阅读模式
ST, STM, STM32, 串口, 代码
下面是我实现的代码
复制void uart_Set_Bound_Parity(u32 B_index,u16 P_index )
{
USART_InitTypeDef USART_InitStructure;
u32 Bound;
u16 Parity;
u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
Bound=bound_Arry[B_index];
Parity=Parity_Arry[P_index];
//while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_Cmd(USART1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
USART_InitStructure.USART_BaudRate = Bound;//一般设置为9600;
if(USART_Parity_No==Parity)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
}
if(USART_Parity_Even==Parity)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = Parity;//偶校验位
}
if(USART_Parity_Odd==Parity)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = Parity;//奇校验位
}
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
USART_ITConfig(USART1, USART_IT_TC , ENABLE);
USART_Cmd(USART1, ENABLE);
}
//设置波特率和校验位
void uart_Set_Bound_ParityPrioc(void)
{
u8 old_B_index,old_P_index;
u8 bounTemp,ParityTemp;
old_B_index=Oldbound;
old_P_index=OldParity;
bounTemp=CompRum.boun;
ParityTemp=CompRum.Parity;
if((old_B_index!=bounTemp)||(old_P_index!=ParityTemp))
{
u32 Bound;
u16 Parity;
u32 bound_Arry[7]={9600,14400,19200,38400,56000,57600,115200};
u16 Parity_Arry[3]={USART_Parity_No,USART_Parity_Even,USART_Parity_Odd};
Bound=bound_Arry[CompRum.boun];
Parity=Parity_Arry[CompRum.Parity];
Oldbound=CompRum.boun;
OldParity=CompRum.Parity;
uart_Set_Bound_Parity(CompRum.boun,CompRum.Parity);
//uart_init1(Bound,Parity);
}
}
回复
收藏0
举报
相关帖子
• CW32F003F6P7的Flash写入超时
• 使用 SSD1306 驱动时出现 “多重定义” 编译错误
• Ubuntu 24.04 服务器上安装 EMQX
• 意法半导体推车规级碳化硅模块 适配下一代电驱系统
• 如何修改无头构建的选项?
• 使用内部RC振荡时串口波特率偏差明显
• ATFs 文件系统与 MDMA 结合使用 SD 卡时异常
• 如何将某个函数或变量放在固定的地址 ?
沙发
楼主|
肥羊
发表于 2025-3-11 14:15
|
只看该作者
检查了一下自己的程序是其他地方搞错了这程序没有问题
回复
收藏0
举报
板凳
楼主|
肥羊
发表于 2025-3-11 14:16
|
只看该作者
检查了一下自己的程序是其他地方搞错了这程序没有问题
回复
收藏0
举报