With stable performance, low price and powerful functions, the single-chip microcomputer has been more and more widely used in intelligent instruments, industrial equipment and daily electronic consumer products. In the input and output control of the single-chip microcomputer, in addition to directly connecting to the small keyboard and LCD display screen, it is generally communicated with the host PC through the serial port. The latter method is particularly important because it can realize remote control and can use the powerful data processing function and friendly control interface of the PC. In general, when using a PC to control the single-chip microcomputer, Windows is used as the host computer platform. Its advantages are friendly interface, easy programming and operation, and its disadvantages are poor stability, which is especially unsuitable for devices that need to run continuously for several days or months. In more demanding occasions, UNIX workstations are generally used as the main control platform. For example, the main control platform of the Hefei Synchrotron Radiation Accelerator uses SUN's Solaris workstation system. However, the expensive price of UNIX workstations has greatly limited its scope of use. In recent years, with the rapid development of Linux, it has gradually changed from a toy for a few people to a mainstream operating system. Linux is a free source code open software that follows the GPL agreement. Anyone can freely obtain its source program from the Internet, and can also modify its source code under the GPL agreement to adapt to specific applications. It runs on an ordinary PC, has stable performance, and is particularly suitable for industrial control. Therefore, it is very meaningful to realize serial communication between Linux and microcontrollers. It can be an optional alternative to expensive UNIX workstations.
1 Hardware Principle
At present, the MCS-51 series of single-chip microcomputers are widely used in China, so the single-chip microcomputer experimental object is an AT89C51. Figure 1 is the hardware schematic diagram. In order to realize serial communication in accordance with RS232C, an ICL232CPE (MAX232) should also be used as a level conversion circuit for serial communication. During the experiment, in order to check whether the communication is successful, in addition to letting the single-chip microcomputer send data back to the host computer, several latches, several LED light-emitting diodes and several small keyboards are also expanded on the periphery of the single-chip microcomputer. Serial communication uses the simplest TxD, RxD, GND three-wire connection. Note that the TxD and RxD sides should be cross-connected.
The host computer is an ordinary PC with two serial ports COM1 and COM2, running Red Hat 8.0. In fact, if you do not need to run a graphical interface such as Gnome or KDE, Linux has very low requirements for system hardware.
Experiments have shown that this circuit is simple and reliable and is very suitable for testing serial communications.
2 Serial Communication Programming
The serial communication program includes the programs of the lower microcontroller and the upper PC. The microcontroller receives the data from the upper computer, puts it into the on-chip RAM, and then sends the data in the RAM to the external expansion latch and the upper computer at the same time, thereby judging whether the communication is successful. This program is written in assembly language, and the baud rate is set to 4800 b/s during initialization, and the communication mode is 8-N-1.
Writing the host computer program is the key part, because there are two ways to operate the serial port hardware device. One is to use the serial port driver that comes with the Linux kernel, and the other is to directly read and write the serial port hardware port. They are introduced below.
2.1 Programming method using serial port driver
Programming with Linux's own serial port driver is actually calling a series of driver functions to complete the setting of serial port communication parameters and the sending and receiving of data. In this method, Linux assigns a file index number to each serial port, with a corresponding file name. In fact, it regards the hardware device as a special file. For example, the files corresponding to COM1 and COM2 are /dev/ttyS0 and /dev/ttyS1 respectively. Operating these two serial ports is actually operating these two files. The operation of hardware device files is no different from that of ordinary files. The same file I/O call functions (open, write, read, close) can be used. The difference is that after using the system call open() to open the serial port and obtain the file descriptor of the corresponding device, it must first be initialized and some specific parameters must be set, such as baud rate, data bits, input and output mode, etc. These parameters are stored in structtermios. The function tcsetattr() can set the serial port structtermios, and tcgetattr() can obtain the serial port struct termios. After setting the communication parameters, the serial port file can be read and written using read and write. When running the program, please pay attention to whether the user has the authority to read and write the serial port file to be operated. You can use the chmod command to modify the file permissions. [page]
The initialization function is as follows:
After initialization, data will be sent and received. First, a string will be sent to the MCU. After receiving the data, the MCU will return the data to the host computer. However, it should be noted that since the host computer is much faster than the MCU, too much data cannot be sent at one time, otherwise it is very likely that the sending buffer will overflow and the data will be lost. After sending, it is necessary to wait for a while for the MCU to send the data completely to the host computer before reading.
2.2 Methods for directly reading and writing serial port hardware ports
When using this method, you must have some understanding of the hardware principles of serial communication. The serial port of a PC is composed of the universal asynchronous receiver and transmitter 8250UART (or 16550) as the core, and the register base addresses are 0x3f8 (COM1) and 0x2f8 (COM2), and there are other registers for control. There are many registers related to the modem, which are not used when using the three-wire system for communication. You only need to care about the registers related to communication. It is more efficient to directly read and write related registers than to use the serial device driver written for general functions.
After initialization, data can be sent and received. Before receiving data, the received data must be ready, which can be judged by D0 of 0x2fd. Before sending data, the sending register must be empty, which can be judged by D5 of 0x2fd. The code is as follows:
3 Conclusion
Experiments show that the two methods used in this system fully realize the point-to-point communication between Linux PC and single-chip microcomputer. The method is simple and reliable. Basically, this method can be used in situations where serial communication between PC and single-chip microcomputer is needed. With the growing application scope of Linux in China in recent years, Linux will be increasingly used in industrial control, data acquisition and other fields. This article can be regarded as a beneficial attempt. Of course, some problems need to be considered in practical applications, such as error handling. A checksum can be sent after a string to be sent. When the checksum received is inconsistent with the checksum sent, it will be resent. For example, the RS232C transmission distance used is very short and the anti-interference ability is very poor. At this time, the bus needs to be converted into RS485/RS422 with differential transmission. In addition, with a little improvement, serial communication between PC and multiple 51 single-chip microcomputers can be realized. At this time, due to the sharing of a bus, an address must be assigned to each single-chip microcomputer, and then the PC will arbitrate the bus. Only the single-chip microcomputer that obtains the right to use the bus can communicate with the host computer. I will not go into details here.
In short, this article only provides a typical example for serial communication between Linux PC and MCU. To apply it to actual projects, it is necessary to consider the actual situation and apply it flexibly, so as to finally form a reliable system based on Linux platform.
References
[1] PhiCornes.Linux from Beginner to Master[M].Translated by Tong Shoubin.Beijing: Publishing House of Electronics Industry, 1998.
[2]Peter Baumann H. Linux Serial-programming - HOWTO Chinese version[M].Translated by Zeng Yuanyou.
[3]Wan Fujun, Pan Songfeng. Single-chip microcomputer principle system design and application[M]. Hefei: University of Science and Technology of China Press, 2001.
[4]Angusson, J. Serial Port Encyclopedia[M]. Translated by Elite Technology. Beijing: China Electric Power Press, 2001.
Previous article:Using event-driven mechanism in single-chip computer programming
Next article:Gas Leakage Alarm and Emergency Processor Based on AT89C51
Recommended ReadingLatest update time:2024-11-16 21:29
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
- Revealed: Tektronix's first oscilloscope for you is now on the market
- Avnet MT3620 module accelerates Azure Sphere IoT security implementation
- [Non-contact automatic disinfection system] Material unpacking - a little surprise
- What microcontroller can be connected to the network via optical fiber using the CAN bus?
- >>See here "New Trend Report: How to Effectively Respond to Current Challenges in the Field of Test and Measurement" and download to win prizes!
- itop4412 development board-QtE4.7-UVC camera usage examples
- Doesn't the EK140P have an EIM interface?
- TIOBE Index October 2022
- C2000 Real-time Control MCU
- 【GD32450I-EVAL】LittleVGL display part transplantation