Design of Serial Communication between Linux PC and 51 Series Single Chip Microcomputer

Publisher:大伊山人Latest update time:2012-03-08 Source: 现代电子技术 Keywords:Linux Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:Linux Reference address:Design of Serial Communication between Linux PC and 51 Series Single Chip Microcomputer

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

Lesson 005 Linux Advanced Commands (File Search, File Decompression Operations)
Section 001_Linux Advanced Commands_find Command When we search for files in Windows, we generally need to pass in two conditions to find files: 1) Search in those directories; 2) What to search for; In Linux, these two conditions are also required to find files. Unlike Windows, which uses the search box to find
[Microcontroller]
Single chip 8x8LED dot matrix screen display pattern
Single chip 8x8LED dot matrix screen display pattern Hardware Connection: Code: #include reg52.h #include intrins.h #define uchar unsigned char  #define uint unsigned int uchar code M = {   {0x00,0x7e,0x7e,0x7e,0x7e,0x7e,0x7e,0x00}, {0x00,0x38,0x44,0x54,0x44,0x38,0x00,0x00}, {0x00,0x20,0x30,0x38,0x3c,0x
[Microcontroller]
Single chip 8x8LED dot matrix screen display pattern
Single chip timer T0 measures external pulse time C51 program
Digital tube display circuit diagram 51 MCU program code: #include reg51.h #define UCHAR unsigned char #define UINT unsigned int sbit KEY=P3^2; UCHAR table = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; UINT context;   void inittime(void) { KEY=1; conter=
[Microcontroller]
Single chip timer T0 measures external pulse time C51 program
Freescale 16-bit MCU (XII) - IIC module test
1. Introduction to IIC module Currently, many single-chip microcomputers on the market already have hardware IIC bus control units. When such single-chip microcomputers are working, the IIC bus status is monitored by hardware, and no user intervention is required, making operation convenient. The IIC bus is a two-wi
[Microcontroller]
Freescale 16-bit MCU (XII) - IIC module test
Programming Example of Data Acquisition System Composed of 89C51 Single-Chip Microcomputer and 1 ADC0809
In a data acquisition system composed of a 89C51 microcontroller and an ADC0809, the addresses of the eight input channels of ADC0809 are 7FF8H~7FFFH. Try to draw the relevant interface circuit diagram, and write a program to collect data from the eight channels in turn every minute, for a total of 50 samples. The sam
[Microcontroller]
Programming Example of Data Acquisition System Composed of 89C51 Single-Chip Microcomputer and 1 ADC0809
Design of multi-channel calling system based on single chip microcomputer control
With the development of information technology and the deepening reform of medical and health care, the implementation of the National Golden Health Project has greatly accelerated the process of hospital management informatization, and the hospital informatization construction has made great progress. Faced with a
[Microcontroller]
FSMC mechanism FlaSh memory expansion of STM32 microcontroller
introduction STM32 is a 32-bit microcontroller series based on the ARM core Cortex-M3 launched by ST (STMicroelectronics). The Cortex-M3 core is specially designed for low-power and price-sensitive applications, with outstanding energy efficiency and processing speed. By adopting the Thumb-2 high-density in
[Microcontroller]
MCU stopwatch timer
     Use the 51 single-chip microcomputer's timer 0 mode 1 to count, and the result is displayed on a 4-digit digital tube, which is the tens of seconds, the ones of seconds, the hundreds of milliseconds, and the tens of milliseconds. So the maximum total time is 1 minute, accurate to 0.01s. At the same time, add 3 in
[Microcontroller]
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号