STM32串口配置的一种方法
来源:互联网 发布:亚像素边缘提取算法 编辑:程序博客网 时间:2024/06/10 04:28
STM32一个函数配置所有串口的方法
code example:
/** * config usartx * @param USARTx [USART lable, example: USART1, USART2, USART3] * @param baudRate [usart baudRate] */void Usart_InitConfig(USART_TypeDef* USARTx, uint32_t baudRate){ GPIO_InitTypeDef GPIO_InitStruction; USART_InitTypeDef Usartx_InitStruction; NVIC_InitTypeDef NVIC_InitStruction; if(USARTx == USART1) { /***************** usart1 ********************/ /********* set usart1 parameter ***********/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /*************** usart1 GPIO config ************/ GPIO_InitStruction.GPIO_Pin = GPIO_Pin_9; GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStruction); GPIO_InitStruction.GPIO_Pin = GPIO_Pin_10; GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStruction); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // usart1 clock Usartx_InitStruction.USART_BaudRate = baudRate; Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; Usartx_InitStruction.USART_WordLength = USART_WordLength_8b; Usartx_InitStruction.USART_StopBits = USART_StopBits_1; Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None; Usartx_InitStruction.USART_Parity = USART_Parity_No; USART_Init(USART1, &Usartx_InitStruction); /************** set usart1 NVIC *****************/ NVIC_InitStruction.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStruction.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruction); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //set usart1 interrupt type USART_Cmd(USART1, ENABLE); } else if(USARTx == USART2) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /**************** usart2 ***********************/ GPIO_InitStruction.GPIO_Pin = GPIO_Pin_2; GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStruction); GPIO_InitStruction.GPIO_Pin = GPIO_Pin_3; GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStruction); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // usart2 clock Usartx_InitStruction.USART_BaudRate = baudRate; Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; Usartx_InitStruction.USART_WordLength = USART_WordLength_8b; Usartx_InitStruction.USART_StopBits = USART_StopBits_1; Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None; Usartx_InitStruction.USART_Parity = USART_Parity_No; USART_Init(USART2, &Usartx_InitStruction); /************** set usart2 NVIC *****************/ NVIC_InitStruction.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStruction.NVIC_IRQChannelSubPriority = 1; NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruction); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //set usart2 interrupt type USART_Cmd(USART2, ENABLE); } else if(USARTx == USART3) { /**************** usart3 ***********************/ GPIO_InitStruction.GPIO_Pin = GPIO_Pin_10; GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStruction); GPIO_InitStruction.GPIO_Pin = GPIO_Pin_11; GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStruction); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // usart3 clock Usartx_InitStruction.USART_BaudRate = baudRate; Usartx_InitStruction.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; Usartx_InitStruction.USART_WordLength = USART_WordLength_8b; Usartx_InitStruction.USART_StopBits = USART_StopBits_1; Usartx_InitStruction.USART_HardwareFlowControl = USART_HardwareFlowControl_None; Usartx_InitStruction.USART_Parity = USART_Parity_No; USART_Init(USART3, &Usartx_InitStruction); /************** set usart3 NVIC *****************/ NVIC_InitStruction.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStruction.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStruction.NVIC_IRQChannelSubPriority = 2; NVIC_InitStruction.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruction); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //set usart3 interrupt type USART_Cmd(USART3, ENABLE); } /************** usartbuff struct init******************/ memset(UsartBuff.UsartRecBuff, '\0', BUFF_SIZE); memset(UsartBuff.UsartSendBuff, '\0', BUFF_SIZE);}
阅读全文
0 0
- STM32串口配置的一种方法
- 基于STM32的CAN通信网-----一种ID配置方法
- STM32的串口配置步骤
- stm32串口的配置过程
- STM32串口寄存器库函数配置方法
- 一种STM32的串口控制台的实现(非常实用)
- 一种STM32的串口控制台的实现(非常实用)
- STM32的串口配置(中断方式)
- STM32关于串口打印之前的配置
- STM32 串口配置 注意事项
- stm32配置串口步骤
- STM32用串口(USB串口)下载程序的方法
- stm32 通过 stm32 cubemx 配置 串口中断
- stm32之USART串口配置
- STM32 串口发送配置流程
- stm32 DMA 配置 串口程序
- STM32的串口通信
- 关于stm32的串口
- computer applications
- ViewGroup事件分发机制
- 记录我的react之路 第一天
- x86-32汇编语言基础汇总
- iOS学习笔记-108.多线程07——CGD栅栏函数、延时、一次性代码
- STM32串口配置的一种方法
- oracl普通表转换成分区表(使用id作为间隔分区)
- react-native报错[NSTaggedPointerString unsignedIntValue]: unrecognized selector sent to instance的解决办法
- ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
- iOS学习笔记-109.多线程08——CGD快速迭代
- Centos7安装Nginx
- 一、CentOS6.5安装ElasticSearch5.5完整纪录与问题总结
- ORACLE 11G 性能诊断优化之ASH实战分析详解
- iOS学习笔记-110.多线程09——CGD队列组