stm32f030的usart通信
发布网友
发布时间:2022-04-25 12:01
我来回答
共1个回答
热心网友
时间:2024-01-26 15:53
1. 对应USART2的引脚没有配,比如PAx, PBx之类的,也要打开相应GPIO的Clock
GPIO_PinAFConfig(TX_GPIO_PORT, TX_SOURCE, TX_AF);
GPIO_PinAFConfig(RX_GPIO_PORT, RX_SOURCE, RX_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = TX_PIN;
GPIO_Init(TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RX_PIN;
GPIO_Init(RX_GPIO_PORT, &GPIO_InitStructure);
2. 对应USART2_InitStruct的结构体成员未填充
USART2_InitStructure.USART_BaudRate = 115200;
USART2_InitStructure.USART_WordLength = USART_WordLength_8b;
USART2_InitStructure.USART_StopBits = USART_StopBits_1;
USART2_InitStructure.USART_Parity = USART_Parity_No;
USART2_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART2_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_OverSampling8Cmd(USART2, ENABLE);
再加上你的USART_Init(USART2, USART2_InitStructure);
3. 对应USART2的模块没有打开
USART_Cmd(USART2, ENABLE);