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.
Previous article:Small production of colored lights based on single chip microcomputer
Next article:Design of wireless communication module based on CC1020
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Fake news and exaggerated hype are hurting China's IC industry. Silence is not the solution (Reprinted)
- TA0CCR0 interrupt of msp430
- What are the 9 most important applications of the Internet of Things (IoT)?
- MOS tube burnt out
- About CS1256 Analog Chip
- At the same frequency, the impedance of a large capacitor is smaller than that of a small capacitor, and it has better filtering performance for high frequencies. Why do we need to connect a small capacitor in parallel?
- [GD32E503 Review] Part 5: FreeRTOS Project Creation
- I heard that ST has released a LoRa development board, NUCLEO-WL55JC2. Why can't I find any information about it anywhere in the world?
- MT7603EN Supply
- When stm32 writes MQTT, it subscribes to multiple topics. How should the program distinguish different topics when receiving?