1. Introduction
With the rapid development of microprocessors, computers, and digital communication technologies, computer control has expanded to almost all industrial fields. Among them, PLCs and microcontrollers have found widespread application in small-scale control systems.
In the automatic temperature control of a plastic extruder, an S7-200 series PLC is selected as the main controller, and a PIC16F877 microcontroller is used as the temperature data acquisition and analog-to-digital conversion chip. The microcontroller transmits data to the PLC through a communication port. Due to the adoption of the RS-485 interface standard, the transmission distance can reach 1000m. The PLC controls the heating coil to maintain a constant temperature after PID calculations. The following section introduces the communication implementation between the PLC and the microcontroller using an example.
2. Hardware Components
2.1 PIC16F877 Microcontroller
Microchip's PIC16F877 microcontroller employs a Harvard bus architecture and reduced instruction set technology, featuring low power consumption, high operating speed, strong driving capability, and simple external circuitry. The PIC16F877 microcontroller integrates a Universal Synchronous/Asynchronous Receiver/Transmitter (USART) module, primarily designed for long-distance serial communication between systems. The USART module requires two pins, RC6 and RC7, as shown in Figure 1. When the transmit enable bit TXEN is set to 1, data can be written to the TXREG register to complete the transmission.
2.2 S7-200 series PLC
The S7-200 series PLC communication ports use asynchronous serial communication. The communication port standard adopts the balanced drive, differential reception RS485 interface standard, which can form a half-duplex serial communication network to construct a distributed system with up to 32 stations. The S7-200 supports multiple serial communication protocols. Utilizing the free port mode, user-defined communication protocols can be implemented through statement lists or ladder diagram programming, facilitating connection to intelligent devices from different manufacturers. In free port mode, it can continuously send or receive data up to 255 bytes, which is very convenient for large data communication.
2.3 MAX485E Chip
The MAX485E chip is a dedicated communication chip for the RS-485 interface standard. As shown in Figure 1, pin RO is the data output pin, which receives the RS-485 differential signal VAB and converts it to TTL level for output. Pin RE is the enable pin for RO; a low level enables RO, making the output valid. Pin DI is the data input pin, which converts TTL level data into the differential signal VAB and outputs it through pins A and B. Pin DE is the enable pin for DI; a high level enables DI, making the input valid. Therefore, pins A and B are both RS-485 signal inputs and outputs, determined by the levels of the enable pins RE and DE.
2.4 Hardware Connection Diagram
The S7-200 PLC uses the RS-485 interface standard to receive differential signals, while the PIC16F877 microcontroller outputs TTL level signals. Therefore, a conversion must be performed before communication between the two. This system uses the MAX485E chip as the conversion chip, and the hardware connection is shown in Figure 1. The PIC16F877 microcontroller's RC4 function selects whether to input or output data.
Figure 1 Hardware Connection Diagram
3. Communication Protocol
In this project, since the PIC16F877 microcontroller only transmits data and the PLC only receives data, simplex serial communication is used. The PLC uses a free-port mode protocol, implemented through statement list programming; the microcontroller uses the asynchronous transmission mode of the USART module, implemented through assembly language programming. Due to the PIC16F877 microcontroller's transmit buffer structure, it can only transmit two bytes of data continuously at a time; therefore, character reception by the PLC is more convenient for handling interrupts. The interface standard is RS-485, consistent with the PLC side, using a 9-pin D-type connector, and shielded twisted-pair cable. Data transmitted from the microcontroller side needs to be converted before transmission.
The character information format is: 1 start bit, 8 data bits, no parity bit, and 1 stop bit.
The character information format for asynchronous communication is shown in Figure 2.
Figure 2 Character Information Format
The data bits are sent in the order of least significant bit first, followed by most significant bit last.
The asynchronous communication transmission rate, i.e., the baud rate, is selected as 38400 bit/s. To improve the reliability of data transmission, XOR checksum is used. The message is sent in fixed length, with the first four bytes participating in the XOR checksum, and the last byte of the message being the checksum. The PIC16F877 microcontroller has an analog-to-digital conversion accuracy of 10 bits, so the temperature value is stored in two bytes. The message frame format is shown in Table 1.
Table 1 Message Frame Format
4. Initial Setup
4.1 Initial settings for data transmission from the PIC16F877 microcontroller
The USART module integrated within the PIC16F877 microcontroller should use the same baud rate as the S7-200 PLC. When using a high baud rate, the baud rate register SPBRG is calculated using the following formula:
SPBRG = F / (16 × baud rate) - 1
In the formula: F — microcontroller clock frequency.
The data bits, parity bits, and stop bits of the microcontroller must be consistent with those of the PLC.
The PIC16F877 microcontroller can only send a maximum of two bytes of data consecutively. Its initial setup assembly language program is as follows:
LISTP=16F877A; Pseudo-instruction
INCLUDE "P16F877A.INC"; Pseudo-instruction
;-------------------Body 1 Setting Subroutine-------------------
T1BCFSTATUS, RP1;
BSFSTATUS,RP0;body1
MOVLWD′5′; 38400 bit/s
MOVWFSPBRG;
MOVLWB′00100100′; Asynchronous, send enable.
MOVWFTXSTA; High-speed, 8-bit data
MOVLWB′11101111′;RC6, RC7,
MOVWFTRISC;RC4 communication
CLRFINTCON; disable interrupts
RETURN; Subroutine returns
;-------------------Body 0 Setting Subroutine-------------------
T0BCFSTATUS, RP0; Body 0
BSFPORTC,4;RC4=1 communication
MOVLWB'10000000'; Enable serial port
RETURN; Subroutine returns
4.2 Initial Settings for Receiving Data with S7-200 PLC
When the CPU is in STOP mode, free port mode is disabled, and other communication modes are used, such as communication with programming devices. Free port mode can only be used when the CPU is in RUN mode. If communication port 0 is used, initial setup will be performed through special memory SMB30.
Network1
LDSM0.7 // If in RUN mode
EU // Rising edge
OSM0.1 // or first scan
MOVB16#01, SMB30//38400bit/s, 8, N, 1
ATCHINT0, 8 // Interrupt connection with int0
ENI // Enable interrupts
Network2
LDNSM0.7 // If not in RUN mode
EU // Rising edge
RSM30.0, 1 // Set to PPI protocol
DTCH8 // Disable interrupts
5. Communication program
5.1 PIC16F877 Microcontroller Data Communication Program
The messages are sent in fixed-length format, with each frame consisting of five bytes. Each temperature value is digitally filtered before transmission. Because the temperature changes slowly, the real-time requirements for communication are relatively low, so a significant delay is incorporated into the data transmission program. Since the baud rate clock depends on the system's time base oscillator, asynchronous communication is not possible when the microcontroller is in sleep mode. The flowchart of the microcontroller's communication program for sending temperature data from N measurement points is shown in Figure 3.
Figure 3 Flowchart of the microcontroller program
5.2 S7-200 PLC Data Communication Receiver Program
The PLC uses character reception to complete interrupt data reception. The start byte determines the beginning of the received data frame, and the data length determines the end. XOR checksum is used to improve the reliability of the received data. After receiving a frame of data, the S7-200 PLC calculates the XOR checksum of the received data and compares it with the checksum transmitted from the microcontroller. If they are different, the data is discarded without requiring retransmission. In this application, the transmitted data is a temperature value; after discarding the value, the next data is received. The PLC data reception communication program flowchart is shown in Figures 4 to 6.
6. Conclusion
The serial communication system, composed of a PIC16F877 microcontroller and an S7-200 PLC, adopts the balanced drive, differential reception RS-485 interface standard, is compatible with TTL levels, and has the advantages of simple development and low cost. Trial testing has proven its stable performance, reliable operation, and strong anti-interference capability. If needed, it can also be used for half-duplex communication or, with slight modifications, form a multi-machine communication network.