How to realize serial communication between PC and AT89C51 microcontroller

Publisher:神光骑士Latest update time:2021-05-26 Source: eefocusKeywords:PC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the data acquisition and real-time control system based on single-chip microcomputer, the command and data transmission between the computer and the single-chip microcomputer is carried out through the RS-232 interface in the computer, and the computer can be used to monitor and control the production site. Since the transmission distance of RS-232 on the computer does not exceed 30m, the MAX485 interface conversion chip can be used to convert RS-232 into RS-485 protocol for long-distance transmission during long-distance data transmission and control. After the protocol conversion is performed on both the sending and receiving ends, the RS-485 protocol is relatively transparent to data transmission, so the RS-232 in the computer can still be used for long-distance data transmission and control. In the simplest RS-232 direct transmission communication system, as long as the sending and receiving parties are ready at the same time, only three wires, signal sending end (TXD), signal receiving end (RXD) and signal ground (GND), can be used for communication; if data communication is carried out in response mode, request to send (RTS), clear to send (CTS) or data terminal ready (DTR), data device ready (DSR) can be used for hardware handshake. Under Windows 95, you can easily use Win32 communication API functions to implement these hardware handshakes and data transmission. In the 89C51 single-chip microcomputer system, the serial port lines RXD and TXD are respectively led out from P3.0 and P3.1 and converted into the level of the RS-232 interface standard through a dedicated level conversion chip, so that digital signals can be transmitted between the two through the RS-232 interface. The single-chip microcomputer can also communicate data in the form of direct transmission or response handshake, but because the handshake method occupies other ports and the number of single-chip microcomputer ports is limited, the communication between the computer and the single-chip microcomputer often adopts the direct transmission method, which will be introduced in this article.


1Communication Programming under Windows 95


The Windows 95 communication system provides an improved serial application program interface SAPI for interactive serial communication. Among them, the serial port and other communication devices are treated as files, and the functions used to open, close, read and write the serial port are the same as the functions for operating files.


The communication session begins with a call to the CreateFile function, which opens the serial port for read access or write access. After a successful opening, the serial port handle is returned for use when reading and writing the serial port. The use of the CreateFile function is as follows:


CreateFile(szDevice,fdwAccess,fdwShareMode,lpsa,fdwCreate,fdwAttrsAndFlags,hTemplateFile)


Among them, the first parameter szDevice is the logical name of the serial port to be opened, such as COM1 or COM2; the second parameter fdwAccess specifies the access type of the serial port, such as read, write or both. Most communications are bidirectional, so it is usually set to: GENERICREAD|GENERICWRITE; the third parameter fdwShareMode specifies the sharing attribute of the serial port. The serial port cannot be shared, so it must be 0; the fourth parameter lpsa references the security attribute structure; the fifth parameter fdwCreate specifies what should be done if CreateFile is being called by an existing file. Since the serial port always exists, this parameter must be set to OPENEXISTING. The sixth parameter fdwAttrsAndFlags describes the various attributes of the port. For the serial port, the only meaningful setting is FILEFLAGOVERLAPPED. When this setting is specified, the port I/O can be performed in the background; the last parameter hTemplateFile is a handle to the template file. When the port is opened, this parameter is NULL.


After opening the serial port, you can configure it appropriately under Windows 95. Windows 95 provides the COMMPROP structure, which contains the settings allowed for the serial port, such as baud rate, number of data bits, number of stop bits, and parity check method. If the serial port is connected to a modem, the COMMPROP structure also contains the settings supported by the modem. However, the COMMPROP structure only gives simple information and cannot be used to change the settings of the serial port. Changing the serial port settings under Windows 95 is achieved by changing its DCB structure, which contains all the serial port settings, including hardware handshake, flow control, etc.


Windows 95 provides the GetCommState function to get the current serial port settings. This function receives an open port handle and a pointer to a DCB structure, and returns information in the DCB structure. The complementary function of the GetCommState function is the SetCommState function, which writes the contents of the DCB structure to the serial port settings. The calls of these two functions are as follows:


BOOLGetCommState(hComm,&dcb)


BOOLSetCommState(hComm,&dcb)


Among them, hComm is the handle of opening the serial port, and dcb is a structure pointing to DCB.


The read and write functions of the serial port in Windows 95 are the same as the read and write functions of the file. The usage format of the read and write functions is as follows:


ReadFile(hComm,inbuff,nBytes,&nBytesRead,&overlapped)


WriteFile(hComm,outbuff,nBytes,&nBytesWrite,&overlapped)


Among them, the first parameter is the handle to open the serial port, the second parameter is the buffer used for the data, the third parameter is the number of bytes to be read, and the fourth parameter is the actual number of bytes read. The actual number of bytes read may be less than the number of bytes to be read. The last parameter points to an overlay-like structure. When the dwAttrsAndFlags parameter in CreateFile is set to FILEFLAGOVERLAPPED, this parameter can specify an OVERLAPPED structure to enable data reading and writing operations to be performed in the background.


Reading and writing ports can be achieved through four technologies: query, synchronous I/O, asynchronous I/O (background I/O) and event-driven I/O. The query method is direct and easy to understand, but it takes up a lot of CPU time; synchronous I/O does not return until the specified number of bytes are read or the timeout occurs, which can easily block the thread for a long time; asynchronous I/O can read and write data in the background while doing other things in the foreground; event-driven I/O is when Windows 95 notifies the application when certain events occur, and then operates the serial port according to what happened.


These four different technologies have their own advantages and disadvantages and their own applicable fields. Therefore, in different communication systems, different technologies can be used according to different requirements. In monitoring systems, due to the randomness of events and the real-time transmission requirements, computers often use event-driven I/O methods to perform on-site monitoring.


In event-driven I/O mode, the events reported by Windows 95 to the application are returned by the function GetCommMask. To change the returned events, you can use the SetCommMask function. The calls of these two functions are as follows:


GetCommMask(hComm,&dwMask)


SetCommMask(hComm,dwMask)


The first parameter is the handle of the open serial port, and the second parameter is the mask of one or more events to wait for. After setting the useful events with SetCommMask, the application calls the WaitCommEvent function to wait for the event to occur. WaitCommEvent function returns until the event occurs. The WaitCommEvent function usage format is as follows:


WaitCommEvent(hComm,&dwEvent,&overlapped)


The first parameter is the handle of the opened serial port, the second parameter is the returned event, and the third parameter specifies synchronous or asynchronous operation. When the function returns, the corresponding serial port operation can be performed according to the returned event mask.


After the communication is completed, the serial port should be closed, otherwise, it is always open and other applications cannot open or use it. The function to close the serial port is: CloseHandle(hComm), where hComm is the handle of the opened serial port.


2 Communication programming under single chip microcomputer


The serial port of the single-chip microcomputer 89C51 has four working modes. Through programming design, it can work in any mode to meet the needs of different occasions. Among them, mode 0 is mainly used for external shift registers to expand the I/O circuit of the single-chip microcomputer; mode 1 is mainly used for communication between two machines or peripheral circuits; in addition to the functions of mode 1, modes 2 and 3 can also be used for multi-machine communication to form a multi-microcomputer system. The difference between modes 2 and 3 lies in the different baud rates.


The baud rate of the serial communication of the microcontroller can be set by program control. In different working modes, it is determined by the division value of the clock oscillation frequency or by the timing overflow time of timer T1.


The serial port of the microcontroller has two control registers, which are used to set the working mode, the status of sending or receiving, the characteristic bit, the baud rate of data transmission, and the interrupt flags TI and RI.


The serial port of the microcontroller has a data register SBUF, which is shared by both sending and receiving. Under certain conditions, writing data to SBUF starts the sending process, and reading SBUF starts the receiving process.


The microcontroller can use a loop mode or an interrupt mode to transmit serial data. In the loop mode, the microcontroller reads and writes the data register SBUF in a loop to receive and send data; in the interrupt mode, for modes 1 and 2, after a frame of data is sent or received, TI/RI is automatically set to 1 to request a serial interrupt. If the CPU responds to the interrupt, the serial interrupt service program is executed and TI/RI is cleared to 0 to respond to the interrupt again. For reception in modes 2 and 3, it depends on the setting of the serial port control register SCON to determine whether RI is set and whether the serial port interrupt is open.

[1] [2]
Keywords:PC Reference address:How to realize serial communication between PC and AT89C51 microcontroller

Previous article:Understanding C language - suitable for beginners of C language
Next article:Signal Source System of DBPL Coded Signal Based on AT89LV51 Control

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号