uart_init

uart_init()

 1 void uart_init(u32 bound)
 2 {
 3 //GPIO 端口设置
 4 GPIO_InitTypeDef GPIO_InitStructure;
 5 USART_InitTypeDef USART_InitStructure;
 6 NVIC_InitTypeDef NVIC_InitStructure;
 7 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,
 8 ENABLE); //使能 USART1, GPIOA 时钟
 9 
10 
11 
12 //USART1_TX GPIOA.9
13 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
14 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
16 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 GPIOA.9
17 //USART1_RX GPIOA.10 初始化
18 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
19 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
20 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 GPIOA.10
21 
22 
23 //Usart1 NVIC 配置
24 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
25 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=7 ;//抢占优先级 7
26 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级 0
27 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能
28 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化 VIC 寄存器
29 
30 
31 
32 //USART 初始化设置
33 USART_InitStructure.USART_BaudRate = bound; //串口波特率
34 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为 8 位数据格式
35 USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
36 USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
37 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
38 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
39 USART_Init(USART1, &USART_InitStructure); //初始化串口 1
40 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
41 USART_Cmd(USART1, ENABLE); //使能串口 1
42 }

 

上一篇:STM32F103C8T6+FreeRTOS+USART1、3


下一篇:关于串口和定时器冲突的问题