How to write 485 communication protocol program (51 485 communication program case of single chip microcomputer)

The RS-485 bus interface is a commonly used serial port, which has the advantages of convenient network connection, good anti-interference performance and long transmission distance. The RS-485 transceiver uses balanced transmit and differential receive, so it has the ability to reject common-mode interference. Plus the transceiver's high sensitivity, it can detect voltages as low as 200mv, and reliable communication can reach several kilometers. Using RS-485 bus networking, only a pair of twisted pairs can realize multi-system networking to form a distributed system, simple equipment, low price and long communication distance.

How to write 485 communication protocol program (51 485 communication program case of single chip microcomputer)

485 communication program of 51 single chip microcomputer

#ifndef __485_C__ #define __485_C__

#include 《reg51.h》

#include 《string.h》

#define unsigned char uchar

#define unsigned int uint

/* Communication Command*/

#define __ACTIVE_ 0x01 // The host asks if the slave exists

#define __GETDATA_ 0x02 // The host sends a read device request

#define __OK_ 0x03 // Answer from the slave

#define __STATUS_ 0x04 // The slave sends device status information

#define __MAXSIZE 0x08 // Buffer length

#define __ERRLEN 12 // Any communication frame longer than 12 indicates an error

Uchar dbuf[__MAXSIZE]; // This buffer is used to save device status information

Uchar dev; // This byte is used to save the local device number

Sbit M_DE = P1^0; // drive is enabled, 1 is valid

Sbit M_RE = P1^1; // Receiver enable, 0 is valid

Void get_status(); // Call this function to get device state information, the function code is not given

Void send_data(uchar type, uchar len, uchar *buf); // send data frame

Bit recv_cmd(uchar *type); // Receive host command, host request contains only command information

Void send_byte(uchar da); // This function sends a byte in a frame of data, called by the send_data() function

Void main() {

Uchar type; uchar len;

/* system initialization*/

P1 = 0xff; // read the local device number

Dev = (P1》》2);

TMOD = 0x20; // Timer T1 uses working mode 2

TH1 = 250; // set the initial value

TL1 = 250;

TR1 = 1; // start timing

PCON = 0x80; // SMOD = 1

SCON = 0x50; // Working mode 1, baud rate 9600bps, allowing reception

ES = 0; // turn off the serial port interrupt

IT0 = 0; // External interrupt 0 uses level trigger mode EX0 = 1; // Turns on external interrupt 0 EA = 1; // Turns on interrupt

/* main program flow */

While(1) // main loop {

If(recv_cmd(&type) == 0) // A frame error occurs or the frame address does not match the local address, and the current frame is discarded and returned.

conTInue; switch(type) {

Case __ACTIVE_: // The host asks if the slave exists

Send_data(__OK_, 0, dbuf); // Send a response message, where the contents of buf are not used

Break;

Case __GETDATA_: len = strlen(dbuf);

Send_data(__STATUS_, len, dbuf); // Send device status information

Break; default:

Break; // The command type is incorrect, returning after dropping the current frame

}

}

}

Void READSTATUS() interrupt 0 using 1 // Generates an external interrupt 0 indicating that the device state has changed. This function uses register bank 1

{

Get_status(); // Get the device status information and store it in the memory area pointed to by dbuf. The last byte of the data is set to 0 to indicate the end of the data.

}

/* This function receives a frame of data and detects it. The function will return * function parameter type to save the received command word regardless of whether the frame is wrong or not.

* When a data frame error is received or its address bit is not 0 (non-host sends a frame), the function returns 0, otherwise it returns 1 */

Bit recv_cmd(uchar *type) {

Bit db = 0; // this bit is set when the last byte received is 0xdb

Bit c0 = 0; // when the last byte received is 0xc0, this bit is set.

Uchar data_buf[__ERRLEN]; // save the received frame

Pipe Precision Parts

Pipe fittings are the general name of the parts and components that play the role of connection, control, change direction, shunt, seal and support in the pipeline system. A pipe fitting is a part that joins pipes into a pipe.

It mainly includes the following:

1. Pipe fittings used for connecting pipes with each other are: flange, splice, pipe hoop, clamp, sleeve, throat hoop, etc.

2. Pipe fittings that change the direction of the pipe: elbow and bend.

3. Pipe fittings for changing pipe diameter: reducing pipe (reducing pipe), reducing elbow, branch pipe table and reinforcing pipe.

4, add pipe branch fittings: three, four.

5. Pipe fittings for pipeline sealing: gasket, raw material belt, thread hemp, flange blind plate, pipe plug, blind plate, head, welding plug.

6. Pipe fittings for pipe fixing: clasp, hook, lifting ring, bracket, bracket, pipe card, etc.

Pipe Precision Parts,Custom Pipe Parts,Fingerprint Door Lock,Automobile Exhaust Pipe

Tianhui Machine Co.,Ltd , https://www.thcastings.com

Posted on