3812 views|1 replies

1379

Posts

0

Resources
The OP
 

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.
Program List
8098特殊功能寄存器预定义
R0 EQU 00H:Word
SBUD EQU 07H:Byte
INT_MASK EQU 08H:Byte
INT_PEND EQU 09H:Byte
BAUD_RT EQU 0EH:Byte
IOP2 EQU 10H:Byte
SP_CON EQU 11H:Byte
SP_STAT EQU 11H:Byte
IOC1 EQU 16H:Byte
SP EQU 18H:Word

;通用寄存器预定义
RSEG AT ICH
AX: DSW 1
DX: DSW 1
AL EQU AX:BYTE
AH EQU (AX+1):BYTE
DL EQU DX:BYTE
DH EQU (DX+1):BYTE
SCRNW EQU 640 ;显示屏宽度
SCRNH EQU 480 ;显示屏高度

;变量区
RSEG AT 20H
M_X :DSW 1 ;光标X值
M_Y :DSW 1 :光标Y值
M_BUF :DSB 4 ;接收缓冲区
M_P : DSW 1 ;接收指针
BX :DSW 1
LRB_OK:DSB 1 ;鼠标状态
;Bit7:存在,Bit5,左键,Bit4:右键

CSEG AT 2000H
DCW INIT
CSEG AT 200CH
DCW SIOINT
DCW INIT
DCW 0
DCB 0,0,0,0,0,0
DCB 08DH
DCB 000H
DCB 027H,0FEH

CSEG AT 2080H
INIT:LD SP,#0100H ;设堆栈指针
LD M_X,#SCRNW/2;初始化指针
LD M_Y,#SCRNH/2
ANDB LRB_OK,#7CH
LD M_P,#M_BUF
CLRB INT_PEND ;清除中断
LDB INT_MASK,#40H ;开串行中断
LDB AL,SP_STAT ;清除RI/TI
LDB SP_CON,#09H ;设串口模式
LDB BAUD_RT,#9BH ;1200,12MHz
LDB BAUD_RT,#80H
EI
ANDB IOP2,#0FEH ;P2.2=0
LD AX,#8000H ;延迟200ms
DLY0:DEC AX
JNE DLY0
LDB AL,LRB_OK
LBS AL,7,M_OK
ANDB INT_MASK,#0BFH
M_OK:NOP
; … … …

;清单二:取鼠标消息,
AL=鼠标及按钮状态,BX=X,DX=Y GET_M:ANDB INT_MASK,#0BFH
LDB AL,LRB_OK ;取鼠标信息
LD BX,M_X
LD DX,M_Y
ORB INT_MASK,#40H
RET

;清单三:串口中断服务程序
SIOINT:PUSHF ;中断服务
PUSH AX
LDB AL,SBUF
LDB AH,SP_STAT
JBS AL,6,ISB0 ;第一个字节
CMP M_P,#M_BUF
JNE SIO_1
SJMP C99 ;缓冲区空,出错
SIO_1:CMP M_P,#M_BUF+2
JH C98 ;缓冲区满,出错
STB AL,[M_P]+ ;存储收到字节
CMP M_P,#M_BUF+3
JNE C99
LD M_P,#M_BUF ;已收到完整命令
GOLR:LDB AL,1[M_P] ;处理X方向位移
SHLB AL,#2
EXTB AL
SHRA AX,#2
ADD M_X,AX
CKL:CMP M_X,#0
JGE CKR
CLR M_X
CKR:CMP M_X ,#SCRNW
JLT GOUD
LD M_X,#SCRNW
GOUD:LDB AL,2[M_P] ;处理Y方向位数
SHLB AL,#2
EXTB AL
SHRA AX,#2
ADD M_Y,AX
CKU:CMP M_Y,#0
JGE CKD
CLR M_Y
CKD:CMP M_Y,#SCRNH
JLT ELRUD
LD M_Y,#SCRNH
ELRUD: SJMP C98
ISB0: STB AL, M_BUF
ANDB AL, #0FH
CMPB AL , #03H
JE C97 ;=X3H,
CMPB AL, #0CH
JE C97 ;=XCH
CMPB AL, #0DH
JNE C98 ;<>XDH
ORB LRB_OK, #80H ;Confirm that the mouse is normal
C97: ANDB AL, M_BUF, #30H
ANDB LRB_OK, #80H
ORB LRB_OK, AL ;Update the left and right button status
LD M_P, #M_BUF+1
SJMP C99
C98: LD M_P, #0000H
C99: POP AX
POPF
RET
;
END
This post is from MCU

Latest reply

Oh, I wish I had seen it earlier. Our internship topic not long ago was similar to his.  Details Published on 2006-7-31 21:50
 

1564

Posts

0

Resources
2
 
Oh, I wish I had seen it earlier. Our internship topic not long ago was similar to his.
This post is from MCU
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list