STM32 UART Single-line half-duplex mode (cube version) 1. Introduction
In some cases, a three-wire serial communication is required (only one of the signal lines), which requires a single-line half-duplex mode for communication. In this case, in the process of data protocol transmission, the signal end needs to switch back and forth in the input and output mode. Alternatively, the transmit and interface of the control port can be short-connected. In this case, STM32 provides the Half-duplex function, as long as in the software to open this function, the chip hardware layer will send and receive ports to short-connect. Specific chip How register operation this side do not repeat, can own manual Usart article to consult. Let's take a look at how to configure with Cubemx.
2. Configuring key steps
This document uses the STM32F0 chip, which opens the CUBEMX in the Pinout label also under the USART1 mode enable Sigle wire (Half-duplex). As shown in.
Other configurations are no different than normal usart configurations. Note that this time the hardware connection needs to be connected to the TX port.
3. Software Authoring
After the production code is configured, test with the add code in the main function.
Each time you send a function that needs to be enabled
HAL_HalfDuplex_EnableTransmitter。
Similarly, it is necessary to enable the corresponding function before receiving the data.
HAL_HalfDuplex_EnableReceiver(&huart1);
while (1)
{
/ * USER CODE END WHILE * /
/ * USER CODE BEGIN 3 * /
static uint8_t test = 6;
// Enable sending function, this function needs to be called before each sending
HAL_HalfDuplex_EnableTransmitter (& huart1);
HAL_UART_Transmit (& huart1, & test, 1, 2000);
// Enable the receiving function. This function needs to be called before each reception
HAL_HalfDuplex_EnableReceiver (& huart1);
HAL_UART_Receive (& huart1, & res, 1, 2000);
if (res == 6)
{
test ++;
}
}
-----------------the author of this article "Zhi-yu Electronics", looking forward to exchange learning with electronic enthusiasts. ----------------
STM32 UART Single-line half-duplex mode (cube version)