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.
Previous article:Understanding C language - suitable for beginners of C language
Next article:Signal Source System of DBPL Coded Signal Based on AT89LV51 Control
- Popular Resources
- Popular amplifiers
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
- 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
- Fan modification based on RSL10
- FAQ Analog Front End and Sensors
- Embedded uboot learning
- Disassembly of an eight-year-old laptop battery!
- Design of greenhouse temperature and humidity monitoring and alarm system based on zigbee
- Keysight Material Dielectric Constant Test Method - Just Read the Content
- Sound pressure, sound intensity and sound power
- Proteus MSP430 MCU simulation example 8-2-bit digital tube countdown
- The TouchGFX Designer is a real pain
- How to implement analog serial communication with TI MSP430