The RS485 bus standard is a widely used, two-way, balanced transmission interface in industrial applications such as attendance systems, monitoring, and data acquisition. It supports multi-point connections and allows the creation of a network with up to 3 nodes. The maximum transmission distance is 1200 meters, and it can operate at high speeds of 100kb/s over that distance. Its strong anti-interference capability and simple wiring—using only two lines—make it an efficient and reliable communication protocol. The RS485 communication network operates on a bus structure, where the host computer (such as a personal computer) and the lower-level devices are all connected to the same communication bus. The physical layer of RS485 follows a multi-machine communication mode commonly used with PLCs.
A typical circuit for traditional optoelectronic isolation involves two separate power sources: VDD and +5V1 (VCC485), which are generally implemented using isolated DC-DC converters. Signal isolation is achieved through optocouplers, ensuring that the ISL3152EIBZ and the MCU system remain electrically separated. This complete isolation helps suppress high common-mode voltages, reducing the risk of damage to the 485 interface and improving overall system stability. However, this method has some drawbacks, including a large circuit size, complex design, excessive use of discrete components, and limited transmission speed due to the nature of optoelectronic devices, which can affect system reliability.
To configure the serial port transmit and receive pins and the 485 control pin, the RXD1 pin is set as an input since it receives external data relative to the STM32 chip. The TXD1 pin is set as an output because it transmits data externally. The TRE1 pin sends a “1†or “0†signal to the outside and is also configured as an output.
Here’s a sample initialization function for UART2:
```c
void UART2Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
// 485 control terminal initialization (PA1)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 485_TX (PA2)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 485_RX (PA3)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART2
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
```
When sending data, one important point to note is that after configuring the serial port, the first byte may not be sent correctly. To resolve this issue, you can either clear the transmit complete flag or check the status of the serial port. When powering up, if the data appears garbled, it might be due to checking the wrong flag. The correct approach is to wait until the transmit complete flag (USART_FLAG_TC) is set, ensuring that the data has been fully transmitted before sending the next byte.
STM32 data transmission uses two interrupt flags: the transmit data register empty flag (USART_FLAG_TXE) and the transmit complete flag (USART_FLAG_TC). Both can trigger interrupts. For interrupt-based data transmission, the process typically involves setting the RS485 direction to send, enabling the transmit register empty interrupt, and then filling the data into the transmit register. Once the data is sent, the interrupt will trigger again, allowing the next byte to be sent until all data is transmitted.
Here’s an example of a function to send a string over RS485:
```c
#define RX_485 GPIO_SetBits(GPIOA, GPIO_Pin_1);
#define TX_485 GPIO_ResetBits(GPIOA, GPIO_Pin_1);
void UART2_TX485_Puts(char *str)
{
while(*str)
{
TX_485; // Set DE to send
DelayNmS(1);
USART_SendData(USART2, *str++);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait until the buffer is empty
DelayNmS(1);
RX_485; // Set RE to receive
}
}
```
In RS485 communication, special attention should be given to the software programming of the DE control pin. To ensure reliable operation, appropriate delays must be added when switching the bus state. Typically, a delay of about 1 ms is needed after setting the control pin to "1" during data transmission. After transmitting the data, another 1 ms delay is applied before setting the control pin back to "0". This ensures a stable transition between sending and receiving states, preventing signal corruption and improving system performance.
Product Brand:Sn63/Pb37.Sn60/Pb40.Sn50/Pb50.Sn45/Pb55.Sn40/Pb60.Sn30/Pb70
Lead solder is made lead and tin metal, It enjoys excellent weld ability, reliable welding spot and little corrosion. The anode hemisphere, which is made from extremely high purity raw materials, is of high quality and stable.
Lead Solder Bar,Tin Lead Solder Bar,Lead Free Solder Bar,Lead Tin Soldering Bar
Shaoxing Tianlong Tin Materials Co.,Ltd. , https://www.tianlongspray.com