Pure software realizes remote communication of microcontroller

Publisher:乐基儿vosLatest update time:2006-05-07 Source: 电子产品世界 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

GMS972051 is a MCS-51 compatible microcontroller with 2K E2PROM inside the MW produced by South Korea's LG Company. GMS97C2051 eliminates the P0 and P3 ports of MCS-51, but adds an internal hardware comparator. Since the communication signal of the microcontroller is TTL level, if no other measures are taken, the communication distance will not exceed 5 meters when the communication rate is 9600bps. Generally, in order to extend the communication distance of the microcontroller, RS-232/RS-485 converter,RS-232/RS-422 converter or optically isolated remote transceiver. The method introduced in this article can extend the communication distance between GMS97C2051 microcontrollers to more than 1200 meters without adding any hardware measures and is completely implemented in software.

To realize remote communication between GMS97C2051 microcontrollers using a pure software method, first the software method of the TTL level of the microcontroller must be converted into a differential level, and secondly, ordinary I/O port lines must be used to form a software serial port, and in the software Make the correct fit.

Pure software method for converting TTL levels to differential levels

The signals of GMS97C2051 microcontroller are all TTL levels. The transmission distance of TTL level signals is very limited. Generally, the transmission distance does not exceed 5 meters at a transmission rate of 9600bps. The differential level signal depends on the level difference between the two signal lines. If the level of one signal line is higher than the other, the signal is 1, otherwise it is 0. Because differential level signals can avoid charge accumulation on long-distance transmission wires and have a wider level range, the transmission distance is much longer. The RS485 differential level signal has a transmission distance of up to 1200 meters at a transmission rate of 9600bps.

In order to realize differential level transmission with pure software, the hardware serial port of GMS97C2051 itself cannot be used, but ordinary I/O lines are used to realize serial communication. The differential level needs to be implemented using two I/O lines, such as P1.2 and P1.3 (see Figure 1). When the transmission signal is 1, P1.2 is +5V, and P1.3 is 0V. When the transmission signal is 0, P1.2 is 0V and P1.3 is +5V. Note that P1.2 and P1.3 must not be set to +5V or 0V at the same time.

P1.0 and P1.1 of the GMS97C2051 microcontroller can be used as the positive input terminal (AIN0) and the negative input terminal (AINI) of the on-chip precision analog voltage comparator respectively, so they can be used to receive differential level signals. The output of the internal level comparator has been connected to the P3.6 port inside the chip, so the differential level signal can be obtained by reading the value of the P3.6 port.

Implementation method of software serial port

Serial communication is realized by ordinary I/O port lines, which is customarily called software serial port. The hardware wiring diagram of the software serial port using differential level communication is shown in Figure 2. Both machines A and B are GMS97C2051 microcontrollers. P1.2 and P1.3 are used for sending, P1.0 and P1.1 are used for receiving, and P1.0 is also connected to INI0 of the machine. Software serial ports generally use the standard 10-bit asynchronous communication format: 1 start bit (signal 0), 8 data bits, and 1 stop bit (signal 1). When receiving, the low-order data comes first and the high-order data comes last.

The working principle and process of software serial port reception and transmission are as follows: After the microcontroller is reset, when executing the initialization program, the P1 port is defined as the position control mode, where P1.0 and P1.1 are defined as input, and P1.2 and P1. 3 is defined as output. P1.2 is initialized to "1", and P1.3 is initialized to "0", so that the transmission signal is in the stop position (differential level "1"). Define INT0 as negative edge trigger. Allow interrupts and define them as high priority, then enable interrupts, and the two machines will enter a waiting state where serial communication can start at any time. When A and B are not communicating, the sending and receiving of both machines is "1". Once a certain machine (assumed to be A) needs to communicate with the other party, machine A uses the agreed baud rate (assumed to be 9600bps) through P1.2 and P1 .3Send. The process of sending and receiving a byte is as follows:

The sending end of machine A first sends the start bit ("0" level), and after the INT0 pin of machine B generates a falling edge, an interrupt request is generated. After the CPU of machine B responds to this interrupt, it executes the INT0 interrupt service subroutine. At the beginning of the interrupt service subroutine, use the bit input instruction to read the P3.6 status. If it is "1" level, it indicates that the interrupt is caused by interference, so the reception process is canceled and the interrupt returns; if P3. 6. If the status read-in device is "0", it means that this interrupt is indeed caused by the super start bit sent by machine A. After precise delay, samples are taken in the middle of each data bit sent by machine A to obtain the status of each data bit. , finally generate a byte, send it to the relevant unit and then interrupt and return. Although the signal sent to INT0 is TTL level, due to its very low transmission rate and software anti-interference measures, it can still be effectively transmitted up to 1200 meters.

The key to remote communication in software serial is how to reliably send and receive data at a higher baud rate. The following introduces a set of sub-programs designed with a baud rate of 9600bps and written in MCS-51 assembly language that have strong anti-interference capabilities and high reliability for sending and receiving one byte.

Programming of software serial communications

To achieve successful reception in serial communication, the following key technologies must be solved: (1) Accurately and quickly detect the super start bit sent by the other party and the moment when the start bit switches to negative; (2) Ensure sampling in the middle of each data bit; (3) Have effective verification and error checking methods.

We take the following targeted measures: connect the receiving line under the external interrupt pin defined as a high-level interrupt to capture the start bit information in time, and deduct the delay time of the interrupt response lag from the calculation of determining the sampling time; carefully designed , write a program to calculate the actual execution time of instructions one by one, eliminate interference from other interrupts (turn off interrupts), so as to ensure sampling in the middle of each data bit; repeat sampling multiple times for each bit to determine the status of each data bit, which can greatly reduce Common transient interference in long-distance communication (characterized by large amplitude, short action time, and strong randomness) has adverse effects on communication. The interrupt service source program for reception (1 frame) that uses the above technical measures to work in interrupt mode is shown in the program list):

The above instructions total 46 machine cycles (T), plus the average interrupt response delay of 5 machine cycles, so the program has been executed until this point, that is, 51T has passed since machine A sent the start bit (started to go low). Since 1 machine cycle (T) of the MCS-51 instruction is equivalent to 12 clock cycles, when a 12MHz crystal oscillator is used, 1 machine cycle (1T) is equivalent to 1μs. 51T is equivalent to 51μs.

The total delay from machine A sending the super start bit to here is 51T+4T+88T=143T, which is equivalent to 143μs.

When the transmission rate is 9600bps, each bit takes 104μs (ie 1s/9600). From the start bit to the D0 bit in the middle 1.5 bits, it takes 1.5×104μs=156μs. The following program samples the D0 bit a total of 3 times at 156μs and 11μs before and after, and records the D0 sampling result as 0 times. If the number of times the three sampling results are 0 is less than or equal to 1, it is judged that D0=1. If the number of times the three sampling results are 0 is greater than or equal to 2, it is judged that D0=0. This greatly enhances the reliability of serial communications. Then, after a delay, the other bits of data are sampled the same three times until all 8-bit data from D0 to D7 are sampled.

The execution of the above program takes 40T, plus the previous 143T, taking a total of 183μs. Each time it returns to NEXT3, it loops once and reads in one bit of data. Before the program inputs NEXT3, assigning a value of 02H to R0 will ensure the accurate time of each data bit's 3 samplings in the future. This interrupt service subroutine segment can not only sample every bit of data, but also sample once every 11μs before and after the middle time. Determining the value of this bit based on the majority result of 3 times of data can greatly reduce the probability of errors due to interference, so it has strong adaptability in remote communications with severe interference.

Conclusion

The pure software method and program of remote communication of GMS97C2051 microcontroller introduced in this article are also suitable for AT89C2051 microcontroller of ATMEL Company, and also have certain reference value for the remote communication of other microcontrollers. The practical application of the software interface method and source program provided in this article shows that this method can extend the communication distance to 1200 meters (at 9600bps). The shortcoming of this method is that the software programming requirements are relatively high. If you want to achieve simple software programming and good versatility for single-chip remote communication, you still need to use an RS-232/RS-485/RS-422 converter or optical remote control. transceiver.

Reference address:Pure software realizes remote communication of microcontroller

Previous article:Development of distributed intelligent lighting and security systems based on HBS
Next article:Dissimilar microcontrollers share external memory and methods of communicating with microcomputers

Latest Industrial Control Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号