[Serial] [Starlight Lightning STM32F407 Development Board] Chapter 10 Serial Communication Experiment
[Copy link]
Chapter 11 Serial Port Communication Experiment 11.1 Purpose Through this experiment, you will master the use of STM32 serial port and realize sending and receiving data through the serial port. For the data sheet, please refer to Chapter 26. 11.2 Experimental Introduction Serial communication is a very common serial communication method between devices. The serial port sends and receives bytes by bit. Although it is slower than parallel communication by byte, the serial port can send data on one line while receiving data on another line. Most electronic devices support this communication device. As the main interface for data exchange between computers and single-chip microcomputers, it is widely used in various instruments, industrial detection and automatic control fields. The communication protocol is an agreement reached by the two parties who need to communicate. It makes unified regulations on issues including data format, synchronization mode, transmission speed, transmission steps, error detection and correction methods, and control character definitions, and both parties must comply with them. Serial communication is generally divided into physical layer and protocol layer. The physical layer specifies the characteristics of the mechanical and electronic functional parts in the communication system to ensure the transmission of the original data in the physical media. The protocol layer mainly specifies the communication logic and unifies the data packaging and unpacking standards of the sender and receiver. Physical layer: It is the lowest level signal transmission. The well-known RS232 and RS485 chips are the level conversion of the physical layer, which converts the TTL level of the microcontroller into the corresponding interface level. The F407 development board has a USB to serial port chip on board, which converts the TTL signal into the differential signal of the USB interface. This is also a physical layer chip. RS232 and RS485 will be introduced in the following experiments. Serial communication is divided into simplex, half-duplex and full-duplex, depending on the transceiver chip of the physical layer: Simplex: can only be transmitted from one party A to the other party B. Half-duplex: can be transmitted in both directions, but not at the same time. Full-duplex: can be transmitted in both directions at the same time. Protocol layer: The format of a character transmitted by the serial port is divided into start bit, data bit, parity bit, stop bit and idle bit. Start bit: sends a logic "0" signal to indicate the start of character transmission. Data bit: Immediately after the start bit, the number of data bits can be 5, 6, 7, or 8. Generally, 8 bits are used, and the lowest bit is transmitted first. Parity bit: Parity is a simple error correction method in serial communication. After the data bit is added with this bit, the number of bits of 1 is even (even parity) or odd (odd parity). Stop bit: It is a sign that a character data transmission has ended. It can be a high level of 1 bit, 1.5 bits, or 2 bits. Idle bit: In the logic "1" state, it means that there is no data on the current transmission line. 11.3 Circuit DesignThe Starlight Lightning F407 development board has a built-in USB serial port. The chip uses CH340G and the interface uses a MINI USB interface. It is connected to PA9 and PA10 of USART1 of STM32. The circuit is shown in Figure 11.3..1: Figure 11.3.1 11.4 Software DesignOpen 3 , Program Source Code->1, Basic Experiment->Experiments8 Serial communication experiment project, open the Usart.c file under the BSP in the left directory tree. This is the driver file of the serial port. The project directory is shown in the figure below: 387302 Figure 11.4.1 Open the Usart.c file, the code is as follows: 387303 387304 387305 387306 11.5 Download verificationUseMINI USB cable to connect the computer and development board through CN3's USB port, flip the power switch to power on the development board, the D3 power indicator lights up, connect the JLINK/ST-LINK download debugger,open the "Experiment 8 Serial Communication Experiment" project code, compile and download, open the serial port debugging assistant, the serial port baud rate115200, press KEY1 once to send a message, and when a message is received, the received content will be sent back. This content is originally created by EEWORLD forum user hejecu. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source
|