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 functions of the driver 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, pay attention to whether the user has the authority to read and write the serial port file to be operated. The chmod command can be used to modify the file permissions.
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 8250 UART (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] Ph IC ornes. Linux from Beginner to Master [M]. Translated by Tong Shoubin. Beijing: Electronic Industry Press, 1998.
[2] Peter Baumann H. Linux Serial-Programming-HOWTO Chinese Edition [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] [US] Angus. Serial Port Encyclopedia [M]. Translated by Elite Technology. Beijing: China Electric Power Press, 2001.
Previous article:Design of electronic switch based on RS485 single chip multi-machine serial communication
Next article:Using RS485/RS232 Converter to Realize Serial Communication between PC and MCU
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- quartus18.1
- Why are there always connection points when drawing the wiring of the schematic in KICAD? And the lines are always misplaced. Why is this the case?
- Components Science Popularization: Working Principle Analysis of Thermal Protector
- Who knows the principle of voltage doubling circuit? No transformer or inductor is used, just a few diodes and capacitors...
- EEWORLD University Hall----Application of GaN power devices in 1.4KV multi-level system
- 6. DMA implementation of USART1 sending and receiving
- Embedded Temperature Measurement System Based on TMS320F2812DSP
- PICO's Wireless REPL
- EEWORLD University ---- Sitara realizes intelligent servo
- Microchip Live FAQ|Manufacturing Logistics Challenges of Secure Key Provisioning: Advantages of Discrete Secure Elements