以下是串口创建模板
- //串口初始化
- void USART1_Init(u32 bound)
- {
-
- //GPIO 端口设置
- /*引用GPIO_InitTypeDef ,将GPIO_InitTypeDef 命名为GPIO_InitStructure*/
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- /* 配置 GPIO 的模式和 IO 口 */
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口输出 PA9
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_Init(GPIOA,&GPIO_InitStructure); /*初始化串口输入 IO */
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口输入 PA10 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模拟输入 GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化 GPIO */
-
- //USART1 初始化设置
- USART_InitStructure.USART_BaudRate = bound;//波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为 8 位数 据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowContro l_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模 式 USART_Init(USART1, &USART_InitStructure); //初始化串口 1
-
- //Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口 1 中断通道
- //记得中断分组
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级 3 NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级 3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能
- NVIC_Init(&NVIC_InitStructure); //初始化中断
-
- }
- //USARTI中断服务函数
- void USART1_IRQHandler(void) //串口 1 中断服务程序
- {
-
- }
|