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

1200 Puff

1200 Puffs Disposable ecig have a completely enclosed design, reducing the need for charging and replacing cartridges. The no-charge design also reduces the occurrence of faults. It is understood that with rechargeable e-cigarettes, each cartridge needs to be charged at least once and the battery efficiency is extremely low, while the design of disposable ecig can solve this problem very well.

1200 Puff E-Cigarette,1200 Puff E-Cigarette For Sale,Best 1500 Puff E-Cigarette,Best 1200 Puff E-Cigarette For Sale

Shenzhen E-wisdom Network Technology Co., Ltd. , https://www.healthy-cigarettes.com