Application skills/80C196 single-chip computer mouse interface program design example[Copy link]
Abstract : Through an in-depth analysis of the mouse's underlying communication principles and protocols, this paper discusses the application value and feasibility of the mouse in the 80C196 single-chip microcomputer system, introduces the principles and methods of mouse interface software design, and gives the specific interface program between the 80C196 single-chip microcomputer and the Microsoft mouse.
Keywords : mouse interface technology, software design, interface program
introduction
Due to its low format and strong processing power, the 80C196 single-chip microcomputer has been widely used in many fields such as signal analysis and data acquisition. In the target system, more and more LCD or CRT displays of various specifications are used. Therefore, introducing a mouse in such a single-chip microcomputer system will facilitate operation and improve work efficiency. The mouse core is essentially a two-dimensional angle or displacement signal detection device, which consumes very little power, has high reliability and low price, and may play a role in many occasions. The mouse communicates with the host through the RS-232 standard serial interface, and the information transmission is unidirectional, unconditional, and continuous without response. In addition, the UART of the 80C196 does not directly support the interface protocol of the mouse, which must be paid attention to in program design. This article mainly discusses the design and implementation of the 80C196 single-chip microcomputer and Microsoft compatible mouse interface program.
1 Mouse signal sending protocol and process
The mouse is a small mechatronic system with highly concentrated functions. It first converts linear displacement into angular displacement, then into digital quantity, and then uniformly encodes it with the button status and sends it through the RS-232 serial port. The power required for the mouse to work is stolen from the control line of the RS-232 serial interface (except for PS/2 and USB interface mice). When the mouse is dragged beyond a minimum distance or a button is pressed, it sends the moving distance and button status to the host through one or several messages according to the specified protocol; the mouse driver on the host converts the information into the mouse position and button status for other program modules to call. Every time a movement or button status changes, the mouse sends a message upward. The resolution of a general mouse is usually 400DPI. In theory, every inch dragged in a certain direction (general speed) will generate 400 information sending processes. If the drag is faster, the number of information sending is reduced, but the total moving distance reflected is still 400 steps.
Various serial interface mice generally use standard serial communication protocols at the physical layer, with a baud rate of 1200bps. The frame format is 7 data bits, 2 stop bits, and no parity bit. The upper layer protocol directly sends updated mouse information in hexadecimal form based on this, including: initialization report: moving direction, distance, button status. Its general form is shown in Table 1.
The above three parameters P1, P2, and P3 are described as follows:
P1_D7D6 fixed value 01; D1D0=11 This mouse movement includes left and right movement components; D3D2=11 This mouse movement includes up and down movement components; D4=1 The right mouse button is currently pressed; D5=1 The left mouse button is currently pressed (D4D5=00 means the button is released).
The fixed value of P2_D7D6 is 00; the remaining six bits represent a signed binary number, reflecting the amount of left-right movement. If it is greater than 0, it moves to the right, and if it is less than 0, it moves to the left.
The fixed value of P3_D7D6 is 00; the remaining six bits represent a signed binary number, reflecting the amount of up and down movement. If it is greater than 0, it moves downward, and if it is less than 0, it moves upward.
For example: [6CH 02H 3AH] (P1=6CH, P2=02H, P3=3AH) shows that the mouse has moved 2 units to the right and 6 units upward, and the left button is currently pressed.
2 Mouse interface programming
The 80C196 series microcontroller has an RS-232 transceiver built in, but an interface chip is required to achieve level conversion. Considering that the mouse steals power from the serial port, the interface chip must have a certain driving capability, and a simple quasi-RS-232 level converter cannot be used. Here, MAX232E is used as the interface chip.
The left side of Figure 1 shows the 80C196 microcontroller. P2.0 (TXD) and P2.1 (RXD) form a serial interface that meets the requirements of the mouse through MAX232E, including generating standard RS-232C levels and providing power supply. The rightmost side of Figure 1 shows a 9-pin or 24-pin standard serial connector. Since the power supply of the mouse adopts a power stealing solution and is provided by DTR/RTS, a current limiting resistor is added to DTR here to prevent damage to the mouse. RTS is provided by the MAX232E signal sending end. The level of MAX232E's 10th pin (corresponding to the output pin 7) is controlled by 80C196's P2.0 to change the potential of pin 7, so that the mouse can receive control commands from RTS to detect whether the mouse is installed or not. MAX232E's 11th pin (corresponding to the input pin 14th pin) is set to a high level to ensure that the potential of pin 14 is a negative RS-232 potential, which meets the level requirements when the mouse sends signals.
From the link layer, the four working modes of the 80C196 serial interface do not meet the frame format requirements of the mouse; but the total number of transmission bits of its working mode 1 (1 start bit, 8 data bits, 1 stop bit) is the same as that of the mouse (1 start bit, 7 data bits, 2 stop bits), which is 10 bits. During the receiving process, after receiving 7 data bits, the 80C196 loads the first of the 2 stop bits into the highest bit of the receiving buffer as the data bit. Since the stop bit is a high level at the physical layer, it is equivalent to a logical "0" after being received as data; the remaining second stop bit provides a valid stop bit for the 80C196. Using the working mode 1 of the 80C196 can fully ensure the correct reception of mouse information.
After the MCU starts, the mouse is initialized through the mouse driver module, that is, the RTS level is flipped once through P2.0 to make the mouse send an initialization report to confirm whether the mouse is installed. After that, the mouse can send action information to the MCU as it is dragged or button operated, and the mouse position and button status can be reflected by the interface module when released.
The mouse interface module mainly includes two parts: button state recognition and position recognition. The 80C196 will continuously refresh the mouse information buffer according to the received mouse information. Since the mouse event interval is uncertain, the scanning method will waste CPU time and may also lose information due to lack of time for processing. The effective processing method is to receive in interrupt mode, and the application module obtains the mouse information through the software interface. The complete mouse interface program flow chart is shown in Figure 2. In the initialization stage, first check whether the mouse exists, set the flag bit according to the result, so as to be used for judgment when obtaining mouse information later; set the mouse initialization position and button original state as needed; finally set the serial interface parameters (frame format, etc.) and open the mouse interrupt. When the mouse sends information, the third byte is a complete information report. However, every time the 80C196 receives a byte, it generates an interrupt, and then determines its nature based on whether the current byte is greater than 40H. If it is the first byte of the information report, it must be further saved after passing the validity check; if it is not the first byte, it must be saved after a series of checks. After receiving three bytes, the command is analyzed and executed immediately. For the specific processing process, please refer to the source program. The user module obtains the current position and button status of the mouse through the specific interface module (Figure 2 (b)), and can determine whether the mouse is installed through the carry flag C=0/1.
The following is the program listing corresponding to the program flow charts in Figures 2 and 3. This program requires the operating frequency of the 80C196 microcontroller to be 12MHz; if other operating frequencies are used, the baud rate setting parameters of the serial port and the time constant of the delay program can be modified.