串口配置 首先对串口进行初始化 包括使能串口时钟,这里我使用的是usart2,使能GPIO时钟,这里我用的是A口,以及GPIO口的配置,这里我的串口输出是PA2,输入是PA3 然后初始化usart2,再使能usart2,具体代码如下:
- /* 串口初始化 */
-
- void STM_EVAL_COMInit(USART_InitTypeDef* USART_InitStruct)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable GPIO clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//GPIOA使能
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);USART2时钟使能
-
- /* Configure USART Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//输出PA2
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* Configure USART Rx as input floating */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//输入PA3
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- /* USART configuration */
- USART_Init(USART2, USART_InitStruct);//USART2初始化
-
- /* Enable USART */
- USART_Cmd(USART2, ENABLE);//使能USART2
- }
|