①用CubeMX配置好Single Wire(Half-Duplex)模式,其他的默认
②用CubeMX配置好Clock
③生成代码后需要将HAL_UART_MspInit函数中的GPIO_Mode改为GPIO_MODE_AF_PP模式,需要将Pull改为GPIO_PULLUP,需要将Speed改为GPIO_SPEED_FREQ_HIGH。
1 /**USART1 GPIO Configuration
2 PA9 ------> USART1_TX
3 */
4 GPIO_InitStruct.Pin = GPIO_PIN_9;
5 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
6 GPIO_InitStruct.Pull = GPIO_PULLUP;
7 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
8 GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
9 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
④发送的时候需要启动HAL_HalfDuplex_EnableTransmitter
⑤接收的时候需要启动HAL_HalfDuplex_EnableReceiver
1 static uint8_t data = 0x55;
2 while (1)
3 {
4 HAL_HalfDuplex_EnableTransmitter(&huart1);
5 HAL_UART_Transmit(&huart1, &data, 1, 0xffff);
6 HAL_HalfDuplex_EnableReceiver(&huart1);
7 HAL_Delay(1000);
8 }
|