The RS485 bus standard is a widely used two-way, balanced transmission standard interface in the industry (attendance, monitoring, data acquisition system), supports multi-point connection, allows the creation of a network of up to 3 nodes; maximum transmission distance of 1200m, supports 1200 m At a high speed of 100kb/s, the anti-interference ability is very strong, and wiring only two lines is very simple. RS485 communication network interface is a kind of bus type structure. The upper computer (taking personal computer as an example) and the lower computer are all hung on the communication bus. The communication protocol of RS485 physical layer is multi-machine communication mode of RS485 standard and PLC.
Typical circuit for traditional optoelectronic isolationVDD and +5V1 (VCC485) are two sets of power sources that are not shared, and are generally implemented by isolated DC-DC. Isolation transmission of signals is achieved by optocoupler isolation. The ISL3152EIBZ and the MCU system are not shared. The complete isolation effectively suppresses the generation of high common-mode voltage, greatly reduces the damage rate of 485, and improves system stability. However, there are also shortcomings such as excessive circuit size, cumbersome circuits, excessive discrete devices, and limited transmission rate by optoelectronic devices, which also have a certain impact on the stability of the entire system.
The first step is to configure the serial port transmit and receive pins and the 485 control pin.Because the RXD1 pin receives external data relative to the STM32 chip, it is set as an input;
The TXD1 pin is externally transmitted with respect to the STM32 chip, so it is set as an output;
The TRE1 pin sends a "1" or "0" high-low command to the outside, so it is set as an output.
/************************************************* ****************
*Function Name: UART2Init
*Function description: Set serial port 2 parameters, 485 control port initialization
*
*Input parameters: none
*Return value: none
*Other notes: none
*current version: v1.0
*------------------------------------------------- ----------------
*
************************************************** ****************/
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);//Enable peripheral clock
//The members of the GPIO structure are set as follows:
/*--------------485 control terminal initialization ------PA1----------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //50M clock speed
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // push-pull output
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //485_TX
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiple push-pull output
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //485_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
GPIO_Init(GPIOA, &GPIO_InitStructure);
//The structural members of the serial port are set as follows:
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);
/* Method 1: Clear the completion flag*/
// USART_ClearFlag(USART3, USART_FLAG_TC);
/* Method 2: Obtain the serial port 1 status flag */
USART_GeTITStatus(USART1, USART_FLAG_TC);
}
Step 2: Send dataThe things to note here are:
/* Small defects of the CPU: The serial port is configured well. If it is directly sent, the first byte cannot be sent out.
The following two method statements solve the problem that the first byte cannot be sent correctly*/
Method 1: USART_ClearFlag (USART3, USART_FLAG_TC); / * Clear the completion flag, Transmission Complete flag * /
Method 2: /* Get the serial port 1 status flag */
USART_GeTITStatus(USART1, USART_FLAG_TC);
The reason for garbled when powering up:
While(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // USART_FLAG_TXE---Detect Transmit Data Register Empty Flag
If USART_FLAG_TC---send completion flag
(1) When set to USART_FLAG_TXE---detect transmit data register empty flag bit - empty, but the transmit shift register is not empty, the data has not been completely sent out, and data is written in, so it will Prone to garbled;
(2) When set to USART_FLAG_TC—Detect Transmit Complete Flag—is empty, that is, the transmit shift register is empty, the data is actually sent out, so there is no data garbled when data is written in.
1
STM32 data transmission has two interrupt flags, one is the transmit data register empty flag, and the other is the send completion flag. Both flags can cause an interruption.
To send a packet in an interrupted manner, the process looks like this:
1. Set the direction of RS485 to send, enable the transmit register empty interrupt, and enable the serial port interrupt when enabled.
2. The serial port interrupt reads the serial port status and fills a data to the transmit data register. The hardware automatically clears the transmit data register empty flag, and the serial data transmission starts.
3. After the serial port sends out a data, the transmit data register becomes empty, and then enters the interrupt, and continues to fill the next data until the last data is filled, enabling the serial port.
The transmission is interrupted.
4. After the last data is sent, enter the interrupt again, clear the send data register empty flag, clear the send completed interrupt flag, clear the two interrupt flags.
The enable bit sets the direction of RS485 to receive.
/************************************************* ****************
* Macro definition
************************************************** ****************/
#define RX_485 GPIO_SetBits(GPIOA, GPIO_Pin_1);
#define TX_485 GPIO_ResetBits(GPIOA, GPIO_Pin_1);
/************************************************* ****************
*Function Name: UART2_TX485_Puts
*Function Description: Send string data
*
*Input parameters: str: the string to be sent
*Return value: none
*Other notes: none
*current version: v1.0
* Author: Liang Yin Xuan
*Completion date: August 3, 2012
*Modify date version number modifier to modify content
*------------------------------------------------- ----------------
*
************************************************** ****************/
Void UART2_TX485_Puts(char * str)
{
While(*str)
{
TX_485; //Open 485 to send DE port, close receiving/RE port
DelayNmS(1);
USART_SendData(USART2, *str++);
/* Loop unTIl the end of transmission */
While(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); //Detect the transmit data register empty flag bit
DelayNmS(1);
RX_485; / / Close the send DE port, open the receiving / RE485 port,
}
}"span style=“font-family:Times New Roman;font-size:14px;â€â€ “/spanâ€
In the communication of the 485 chip, in particular, pay attention to the software programming of the 485 control terminal DE. In order to work reliably, it is necessary to make appropriate delays when switching the 485 bus state, and then send and receive data. The specific method is to set the control terminal to "1" and delay the time of about 1 ms in the data transmission state. After transmitting valid data, after delaying 1 ms after the end of one packet data transmission, the control terminal is set to "0". "This processing will make the bus have a stable working process when the state is switched.
Over time, battery cables have to be replaced due to corrosion or damage. A bad battery cable can cause intermittent starting issues or lack of power to the vehicle, including arcing or power drains.A battery cable consists of multiple stands of wire encased in synthetic material with different types of battery terminals on each end for a reliable connection. Corrosion is the number one cause of battery cable failure, stopping the flow of electricity.
Battery cable, power cable, battery cable assembly, battery wiring
ETOP WIREHARNESS LIMITED , https://www.oemmoldedcables.com