STM32F4XX - uart设置

初始化一个波特率为115200的串口。下面函数参数为115200.
代码如下:

void uart1_init(u32 bound)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);    //Enable the gpio clock //使能GPIO时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Enable the Usart clock //使能USART时钟

        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); 
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource10 ,GPIO_AF_USART1);        
        
        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_Speed=GPIO_Speed_50MHz;       //高速50MHZ
        GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;            //上拉
        GPIO_Init(GPIOA, &GPIO_InitStructure);                            //初始化
        
  //UsartNVIC configuration //UsartNVIC配置
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  //Preempt priority //抢占优先级
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
  //Subpriority //子优先级
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;              
  //Enable the IRQ channel //IRQ通道使能
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  //Initialize the VIC register with the specified parameters 
        //根据指定的参数初始化VIC寄存器 
        NVIC_Init(&NVIC_InitStructure); 
        
  //USART Initialization Settings 初始化设置
        USART_InitStructure.USART_BaudRate = bound; //Port rate //串口波特率
        USART_InitStructure.USART_WordLength = USART_WordLength_8b; //The word length is 8 bit data format //字长为8位数据格式
        USART_InitStructure.USART_StopBits = USART_StopBits_1; //A stop bit //一个停止位
        USART_InitStructure.USART_Parity = USART_Parity_No; //Prosaic parity bits //无奇偶校验位
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control //无硬件数>据流控制
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Sending and receiving mode //收发模式
        USART_Init(USART1, &USART_InitStructure); //Initialize serial port 1 //初始化串口1
        
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Open the serial port to accept interrupts //开启串口接受中断
        USART_Cmd(USART1, ENABLE);                     //Enable serial port 1 //使能串口1
}

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable the gpio clock //使能GPIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Enable the Usart clock //使能USART时钟
设置GPIO和uart时钟(我的理解是GPIO直接挂到AHB上,所以需要基于AHB总线使能对应GPIO时钟。uart控制器是挂到APB上,所以需要基于APB总线使能uart时钟)

GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10 ,GPIO_AF_USART1);
将GPIOA的pin9、pin10配置成功能uart1。配置的寄存器为复用功能寄存器AFR

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_Speed=GPIO_Speed_50MHz; //高速50MHZ
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化
对GPIO引脚进行配置,这个在GPIO端口配置部分有说明。

//UsartNVIC configuration //UsartNVIC配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //Preempt priority //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ; //Subpriority //子优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Enable the IRQ channel //IRQ通道使能
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Initialize the VIC register with the specified parameters
//根据指定的参数初始化VIC寄存器
NVIC_Init(&NVIC_InitStructure);
1. 配置中断通道为USART1_IRQn。
2. 配置抢占中断优先级为1,这个值越小,中断优先级越高。
3. 配置响应中断优先级为0。
4. 使能uart1中断
说明:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);优先级分组设置为group 4即4位抢占优先级即2^4种优先级(0-16),0位响应优先级。所以在设置时只有抢占优先级有效。

//USART Initialization Settings 初始化设置
USART_InitStructure.USART_BaudRate = bound; //Port rate //串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //The word length is 8 bit data format //字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //A stop bit //一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //Prosaic parity bits //无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control //无硬件数>据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Sending and receiving mode //收发模式
USART_Init(USART1, &USART_InitStructure); //Initialize serial port 1 //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Open the serial port to accept interrupts //开启串口接受中断
USART_Cmd(USART1, ENABLE); //Enable serial port 1 //使能串口1
这个比较简单了,就是初始化串口,根据串口协议进行配置,打开中断,使能串口。

  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值