Single-chip RS485 communication interface, schematic, control line

RS485 and RS232 are all based on the serial port communication interface, the data transmission and reception operations are consistent, so the same WinCE underlying driver is used. However, they have a big difference in the communication mode in practical applications. The RS485 interface is a half-duplex data communication mode, and the RS232 interface is a full-duplex data communication mode. Data transmission and reception cannot be performed simultaneously, in order to ensure that data is transmitted and received without conflict. The hardware is implemented by direction switching, and correspondingly, the software must strictly separate the process of sending and receiving.

Rs485 feature summary

Differential signals are used. When we talked about A/D, we talked about the concept of differential signal input. We also introduced the benefits of differential input. The biggest advantage is that it can suppress common mode interference. Especially when the industrial site environment is more complicated and the interference is more, the differential method can effectively improve the communication reliability. RS485 uses two communication lines, usually indicated by A and B or D+ and D-. The logic "1" is represented by a voltage difference between two lines of +(0.2~6)V, and the logic "0" is represented by a voltage difference between two lines of -(0.2~6)V, which is a typical difference. Communication.

RS485 communication speed is fast, and the maximum transmission speed can reach more than 10Mb/s.

The physical structure inside RS485 is a combination of balanced driver and differential receiver, and the anti-interference ability is also greatly increased.

The transmission distance can be up to 1200 meters, but its transmission rate and transmission distance are inversely proportional. Only the transmission speed below 100Kb/s can reach the maximum communication distance. If you need to transmit longer distances, you can use relay. .

It can be networked on the bus to realize multi-machine communication. Multiple transceivers can be connected on the bus. From the perspective of the existing RS485 chip, there are drivers for different devices such as 32, 64, 128, 256.

The RS485 interface is very simple. It is similar to the MAX232 used by RS232. It only needs an RS485 converter to connect directly to the UART serial port of the microcontroller and use the same asynchronous serial communication protocol. However, since RS485 is differential communication, receiving data and transmitting data cannot be performed simultaneously, that is, it is a half-duplex communication.

There are many RS485 conversion chips. In this lesson, we use the typical MAX485 as an example to explain RS485 communication, as shown in Figure 18-1.

Single-chip RS485 communication interface, schematic, control line

Figure 18-1 MAX485 hardware interface

The MAX485 is a popular RS485 converter from Maxim. The 5 pin and the 8 pin are power pins; the 6 pin and the 7 pin are the A and B pins in the RS485 communication; the 1 pin and the 4 pin are respectively connected to the RXD and TXD pins of the MCU, and the MCU is directly used. Data reception and transmission; 2 pin and 3 pin are direction pins, where pin 2 is the low level enable receiver and pin 3 is the high level enable output driver. We connect these two pins together. When not sending data normally, keep these two pins low, let MAX485 be in the receiving state. When it needs to send data, pull this pin high, send data, and then pull this pin low after sending. That's it. In order to improve the anti-interference ability of RS485, it is necessary to connect a resistor between the A and B pins close to MAX485. The resistance of this resistor can be from 100 ohms to 1K.

Here we will also introduce how to use the KST-51 microcontroller development board for peripheral expansion experiments. Our development board can only provide basic functions for students to provide experimental exercises, but students should not stay on this experiment board. If you want to do more experiments, you can extend the experiment through the extended interface of the microcontroller development board. You can see that there are 32 pins around the blue-green MCU. These 32 pins are all the 32 IO pins of the MCU. The four devices J4, J5, J6, and J7 are shown on the schematic, as shown in Figure 18-2.

Single-chip RS485 communication interface, schematic, control line

Figure 18-2 MCU expansion interface

Not all of the 32 IO ports can be used for external expansion. It can be used as both data output and data input pins, such as P3.2, P3.4, and P3.6 pins. These three pins are not available. For example, the pin of P3.2, if we use to expand, if the transmitted signal matches the timing of the DS18B20, it will cause the DS18B20 to pull the pin low, which will affect the communication. In addition to the 29 IO ports, the DuPont cable can be connected to the pin for expansion. Of course, if the current IO port is applied to the extended function, the corresponding function on the board can not be realized, which means that there is a need to choose between the extended function and the onboard function.

In the RS485 experiment, the pins used for communication must be P3.0 and P3.1, in addition to a directional control pin, we connect it to P1.7 using the DuPont line. On the other end of RS485, you can use a USB to RS485 module, connect the development board and A and B on the module with twisted pair cable, plug the USB into the computer, and then communicate.

After learning the practical serial communication method and program in Chapter 13, the method of doing this serial communication is very simple, basically the same. We use the idea of ​​a practical serial communication routine, and do a simple program, send any character through the serial debugging assistant, after the microcontroller receives the end, add the "carriage return + line feed" at the end and then send it back, re-run on the debugging assistant Show it, first paste the program.

One point to note in the program is that since MAX485 is normally set to the receive state, MAX485 is changed to the transmit state only when the data is sent, so the MAX485 direction pin is pulled high at the beginning of the UartWrite() function, and the function exits. Pull it down before. However, there is a detail here that the transmission and reception interrupts of the microcontroller are generated at half the stop bit, that is, whenever the stop bit is transferred halfway, the RI or TI is set and immediately enters the interrupt ( If the interrupt is enabled, the function will not be a problem when it is received, but it will be different when it is sent: When a byte of data is written to SBUF, the UART hardware will complete the last stop bit. After sending, start the transmission of the new byte, but if the next byte is not sent again, but the transmission has been completed, stop sending and pull the MAX485 direction pin low to make the MAX485 re-receive. There is a problem, because at this time the last stop bit is actually only sent half, and has not been completely completed, so there is the operation of DelayX10us(5) in the UartWrite() function, which artificially increases the delay of 50us. This 50us time just happens to let the remaining half stop bits complete, then this time is naturally determined by the communication baud rate, which is the baud rate week. Of the half.

/****************************RS485.c File program source code ************* ***************/

#include 《reg52.h》

#include "intrins.h"

Sbit RS485_DIR = P1^7; //RS485 direction select pin

Bit flagFrame = 0; //Frame reception completion flag, that is, receiving a new frame of data

Bit flagTxd = 0; // single-byte transmit completion flag, used to replace the TXD interrupt flag bit

Unsigned char cntRxd = 0; //receive byte counter

Unsigned char pdata bufRxd[64]; //receive byte buffer

Extern void UartAcTIon(unsigned char *buf, unsigned char len);

/* Serial port configuration function, baud-communication baud rate*/

Void ConfigUART(unsigned int baud){

RS485_DIR = 0; //RS485 is set to receive direction

SCON = 0x50; //Configure the serial port as mode 1

TMOD &= 0x0F; //Clear the control bit of T1

TMOD |= 0x20; //Configure T1 to mode 2

TH1 = 256 - (11059200/12/32)/baud; //calculate T1 reload value

TL1 = TH1; //The initial value is equal to the reload value

ET1 = 0; // disable T1 interrupt

ES = 1; //Enable serial port interrupt

TR1 = 1; //Start T1

}

/* Software delay function, delay time (t*10) us */

Void DelayX10us(unsigned char t){

Do {

_nop_();

_nop_();

_nop_();

_nop_();

_nop_();

_nop_();

_nop_();

_nop_();

} while (--t);

}

/* Serial data write, ie serial port send function, buf - pointer of data to be sent, len-specified send length */

Void UartWrite(unsigned char *buf, unsigned char len){

RS485_DIR = 1; //RS485 is set to send

While (len--){ // loop all bytes

flagTxd = 0; //clear send flag

SBUF = *buf++; //Send one byte of data

While (!flagTxd); //wait for the byte to be sent

}

DelayX10us(5); //wait for the last stop bit to complete, the delay time is determined by the baud rate

RS485_DIR = 0; //RS485 is set to receive

}

/* Serial data read function, buf-receive pointer, len-specified read length, return value - actual read length */

Unsigned char UartRead(unsigned char *buf, unsigned char len){

Unsigned char i;

/ / specify the read length is greater than the actual received data length,

/ / read length is set to the actual length of the received data

If (len 》 cntRxd){

Len = cntRxd;

}

For (i=0; i "len; i++) { // copy the received data to the receiving pointer

*buf++ = bufRxd[i];

}

cntRxd = 0; // Receive counter clear

Return len; / / return the actual read length

}

/* Serial port reception monitoring, the end of the idle time determination frame, need to be called in the timer interrupt, ms-timing interval * /

Void UartRxMonitor(unsigned char ms){

staTIc unsigned char cntbkp = 0;

staTIc unsigned char idletmr = 0;

If (cntRxd 》 0){ // Monitor bus idle time when the receive counter is greater than zero

If (cntbkp != cntRxd){ // Receive counter change, that is, clear idle time when data is just received

Cntbkp = cntRxd;

Idletmr = 0;

}else{ //The receive counter has not changed, ie the free idle time when the bus is idle

If (idletmr " 30) { / / idle time is less than 30ms, continue to accumulate

Idletmr += ms;

If (idletmr 》 = 30) { // When the idle time reaches 30ms, it is determined that one frame has been received.

flagFrame = 1; //Set the frame reception completion flag

}

}

}

}else{

Cntbkp = 0;

}

}

/* Serial port driver function, monitoring the reception of data frames, scheduling function functions, need to be called in the main loop * /

Void UartDriver(){

Unsigned char len;

Unsigned char pdata buf[40];

If (flagFrame){ //Read and process the command when the command arrives

flagFrame = 0;

Len = UartRead(buf, sizeof(buf)-2); //Read the received command into the buffer

UartAction (buf, len); / / pass the data frame, call the action execution function

}

}

/* Serial port interrupt service function */

Void InterruptUART() interrupt 4{

If (RI){ //received new bytes

RI = 0; //Clear the receive interrupt flag bit

/ / Receive buffer has not been used up, save the receive byte, and increment the counter

If (cntRxd sizeof(bufRxd)){

bufRxd[cntRxd++] = SBUF;

}

}

If (TI) { // byte sent

TI = 0; // Clear the transmit interrupt flag bit

flagTxd = 1; //Set the byte send completion flag

}

}

/*****************************main.c file program source code ************* *****************/

#include 《reg52.h》

Unsigned char T0RH = 0; //High byte of the T0 overload value

Unsigned char T0RL = 0; //low byte of the T0 overload value

Void ConfigTimer0(unsigned int ms);

Extern void UartDriver();

Extern void ConfigUART(unsigned int baud);

Extern void UartRxMonitor(unsigned char ms);

Extern void UartWrite(unsigned char *buf, unsigned char len);

Void main(){

EA = 1; //open total interruption

ConfigTimer0(1); //Configure T0 timing 1ms

ConfigUART (9600); //Configure baud rate is 9600

While (1){

UartDriver (); / / call the serial port driver

}

}

/* Serial port action function, perform response action according to the received command frame

Buf- received command frame pointer, len-command frame length */

Void UartAction(unsigned char *buf, unsigned char len){

/ / Add a new line break after the received data frame and send it back

Buf[len++] = '';

Buf[len++] = '';

UartWrite(buf, len);

}

/* Configure and start T0, ms-T0 timing time*/

Void ConfigTimer0(unsigned int ms){

Unsigned long tmp; //temporary variable

Tmp = 11059200 / 12; // timer count frequency

Tmp = (tmp * ms) / 1000; // calculate the required count value

Tmp = 65536 - tmp; //calculate timer reload value

Tmp = tmp + 33; //compensate for errors caused by interrupt response delay

T0RH = (unsigned char)(tmp)"8); //Timer reload value is split into high and low bytes

T0RL = (unsigned char)tmp;

TMOD &= 0xF0; //Clear the control bit of T0

TMOD |= 0x01; //Configure T0 as mode 1

TH0 = T0RH; //Load T0 overload value

TL0 = T0RL;

ET0 = 1; //Enable T0 interrupt

TR0 = 1; //Start T0

}

/* T0 interrupt service function, perform serial port reception monitoring*/

Void InterruptTimer0() interrupt 1{

TH0 = T0RH; //Reload the reload value

TL0 = T0RL;

UartRxMonitor (1); / / serial port receiving monitoring

}

High Speed Blender

High Speed Blender,Ipl Laser Hair Removal,Diy Laser Hair Removal,Best Ipl Hair Removal,Epilator Ipl

SHENZHEN CHONDEKUAI TECHNOLOGY CO.LTD , https://www.szfourinone.com