STM32 UART Single-line half-duplex mode (cube version)

Source: Internet
Author: User


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)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.