Design of realizing remote communication of single chip microcomputer by pure software

Publisher:NanoScribeLatest update time:2013-02-05 Source: dzsc Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  GMS972051 is a single-chip microcomputer with 2K E2PROM inside and compatible with MCS-51 produced by LG Company of South Korea. GMS97C2051 omits the P0 and P3 ports of MCS-51, but has an internal hardware comparator. Since the communication signal of the single-chip microcomputer is TTL level, if no other measures are taken, the communication distance does not exceed 5 meters when the communication rate is 9600bps. Generally, in order to extend the communication distance of the single-chip microcomputer, RS-232/RS-485 converter, RS-232/RS-422 converter or optical isolation remote transceiver is used.

  To achieve remote communication between GMS97C2051 microcontrollers using pure software methods, the TTL level software method of the microcontroller must first be converted into a differential level, and then ordinary I/O port lines must be used to form a software serial port, and the software must be properly coordinated.

  Pure software method for converting TTL level into differential level

  TTL is a value in the IP protocol packet, which tells the network whether the data packet has been in the network for too long and should be discarded. There are many reasons why a packet cannot be delivered to the destination within a certain period of time. The solution is to discard the packet after a period of time, and then send a message to the sender, who decides whether to resend it. The initial value of TTL is usually the system default value, which is an 8-bit field in the packet header. The original idea of ​​TTL was to determine a time range, and the packet would be discarded if it exceeded this time. Since each router must reduce the TTL field by at least one, TTL usually indicates the maximum number of routers a packet can pass through before being discarded. When the count reaches 0, the router decides to discard the packet and sends an ICMP message to the original sender.

  The signals of GMS97C2051 microcontroller are all TTL level. The transmission distance of TTL level signal is very limited, generally not more than 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. Since the differential level signal can avoid the charge accumulation on the long-distance transmission wire and has a wider level range, the transmission distance is much longer. The RS485 differential level signal can reach a transmission distance of 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 should be realized by two I/O port 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.

  In order to achieve differential level transmission with pure software, the hardware serial port of GMS97C2051 itself cannot be used, but ordinary I/O lines are used to achieve serial communication. Differential level should be achieved with 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 (AIN0) and negative input (AINI) of the on-chip precision analog voltage comparator, 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.

  How to implement software serial port

  Serial port is called serial interface. Nowadays, PC generally has two serial ports, COM 1 and COM 2. The difference between serial port and parallel port is that its data and control information are transmitted one by one. Although the speed is slower, the transmission distance is longer than that of parallel port. Therefore, if you want to communicate over a long distance, you should use serial port. Usually COM 1 uses a 9-pin D-shaped connector, also known as RS-232 interface, while COM 2 sometimes uses the old DB25 pin connector, also known as RS-422 interface, but it is rarely used now.

  Serial port, also known as serial port, serial port, serial port, is mainly used for serial bit-by-bit data transmission. Common ones are RS-232 (using 25-pin or 9-pin connectors) for general computer applications, half-duplex RS-485 and full-duplex RS-422 for industrial computer applications. Serial port is called serial interface, also known as serial communication interface. According to electrical standards and protocols, it includes RS-232-C, RS-422, RS485, USB, etc. RS-232-C, RS-422 and RS-485 standards only stipulate the electrical characteristics of the interface, and do not involve plug-in programs, cables or protocols. USB is a new interface standard developed in recent years, mainly used in the field of high-speed data transmission.

  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 single-chip microcomputers. 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 in the machine. The software serial port generally uses a 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 is in front and the high-order data is in the back. [page]

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

  The transmitter of machine A first sends the start bit ("0" level). 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, the P3.6 status is read in with a bit input instruction. If it is "1" level, it indicates that this interrupt is caused by interference, so the receiving process is canceled and the interrupt returns; if the P3.6 status is "0", it indicates that this interrupt is indeed caused by the over-start bit sent by machine A. After precise delay, sampling is performed in the middle of each data bit sent by machine A to obtain the status of each data bit, and finally a byte is generated. After sending it to the relevant unit, the interrupt returns. 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 for 1200 meters.

  Software serial communication programming

  In the field of communication, there are two modes of data communication: parallel communication and serial communication. With the development of computer networking and hierarchical distributed application systems of microcomputers, the function of communication is becoming more and more important. Communication refers to the transmission of information between computers and the outside world, including both transmission between computers and transmission between computers and external devices such as terminals, printers, and disks. Serial communication refers to the use of a data line to transmit data one bit at a time, with each bit of data occupying a fixed length of time. It only requires a few lines to exchange information between systems, and is especially used for long-distance communication between computers and computers, and between computers and peripherals. Serial communication refers to the serial transmission of data between computer hosts and peripherals, and between host systems and host systems. When using serial communication, each character sent and received is actually transmitted one bit at a time, with each bit being 1 or 0.

  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 in the calculation of determining the sampling time; carefully design and write programs to calculate the actual execution time of each instruction, eliminate other interrupt interference (turn off interrupts), so as to ensure that sampling is performed in the middle of each data bit; repeat sampling multiple times for each bit to determine the state of each data bit, thereby greatly reducing the adverse effects of transient interference common in remote communications (characterized by large amplitude, short action time, and strong randomness) on communication. The interrupt service source program for receiving (1 frame) working in interrupt mode using the above technical measures is shown in the program list):

  The above instructions have a total of 46 machine cycles (T), plus the average delay of 5 machine cycles for interrupt response, so the program execution has reached this point, that is, machine A has sent the start bit (starting to become low) and has experienced 51T. Since 1 machine cycle (T) of MCS-51 instruction is equivalent to 12 clock cycles, when using a 12MHz crystal oscillator, 1 machine cycle (1T) is equivalent to 1μs. 51T is equivalent to 51μs.

  The total delay from the time when machine A sends the super start bit to this point is 51T+4T+88T=143T, which is equivalent to 143μs.

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

  The above program takes 40T to execute, plus the previous 143T, a total of 183μs. Each time it returns to NEXT3, it loops once and reads in one bit of data. Before the program enters NEXT3, assigning 02H to R0 can ensure the accurate time of sampling three times for each data bit in the future. This interrupt service subroutine segment can not only achieve sampling of each bit of data, but also sample once 11μs before and after the middle moment. Determining the value of the bit with the majority result of the three data can greatly reduce the probability of error due to interference, so it has strong adaptability in remote communications with severe interference.

  Conclusion

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

Reference address:Design of realizing remote communication of single chip microcomputer by pure software

Previous article:Small production of colored lights based on single chip microcomputer
Next article:Design of wireless communication module based on CC1020

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号