📄 mouse.asm
字号:
movwf TEMP1
PacketOutLoop movf INDF, w ;Get data byte
call ByteOut ; Output that byte
xorlw0xFF ;Test for RTS error
bz PacketIn
xorlw0xFE ^ 0xFF ;Test for inhibit error
bz PacketOut
incf FSR, f ;Point to next byte
decfsz TEMP1, f
goto PacketOutLoop
goto MainLoop
;---------------------------------------------------------------------------------------------
; READ PACKET
;---------------------------------------------------------------------------------------------
PacketIn call ByteIn
xorlw0xFF ;Test for parity/framing error
bz Mouse_ERR
xorlw0xFE ^ 0xFF ;Test for inhibit error
bz MainLoop1
movlw 0xFE ;Test for "Resend" command
xorwf RECEIVE, w
bz PacketOut
Acknowledge movlw 0xFA ;Acknowledge
call ByteOut
goto Command
;---------------------------------------------------------------------------------------------
; READ INPUTS:
;---------------------------------------------------------------------------------------------
ReadInputs movlw DISTANCE
btfss PS2_Xp ;Read inputs
addwf X_COUNTER, f
btfss PS2_Yp
addwf Y_COUNTER, f
btfss PS2_Xn
subwf X_COUNTER, f
btfss PS2_Yn
subwf Y_COUNTER, f
bcf XY_FLAGS, xSIGN
btfsc X_COUNTER, 7
bsf XY_FLAGS, xSIGN
bcf XY_FLAGS, ySIGN
btfsc Y_COUNTER, 7
bsf XY_FLAGS, ySIGN
movf XY_FLAGS, w ;Get previous button states
andlw b'00000111'
btfss PS2_Bl ;Find changes in button states
xorlwb'00000001'
btfss PS2_Br
xorlwb'00000010'
movwf dBUTTONS ;Save *change* in button state
retlw 0x00
;---------------------------------------------------------------------------------------------
; WRAP MODE:
;---------------------------------------------------------------------------------------------
WrapMode btfsc DATA ;Wait for RTS
goto WrapMode
call ByteIn ;Read one byte from host
xorlw0xFE ;Test for aborted transmission
bz WrapMode
movf RECEIVE, w
xorlw0xFF ;Test for "Reset" command
bz Acknowledge
xorlw0xFF^0xEC ;Test for "Reset Wrap Mode" command
bz Acknowledge
xorlw0xEC
call ByteOut ;Else, echo
goto WrapMode
;---------------------------------------------------------------------------------------------
; LOAD DEFAULT VALUES:
;---------------------------------------------------------------------------------------------
LoadDefaults movlw 100 ;Default report rate
movwf REPORT_RATE
movlw 0x02 ;Default resolution
movwf RESOLUTION
clrf FLAGS ;Stream mode, 1:1 scaling, disabled
retlw 0x00
;---------------------------------------------------------------------------------------------
; EMULATE BAT:
;---------------------------------------------------------------------------------------------
BATdelay clrf TEMP0 ;Used for a 400 ms delay at power-on
clrf TEMP1
DelayLoop Delay 6
decfsz TEMP0, f
goto DelayLoop
decfsz TEMP1, f
goto DelayLoop
retlw 0x00
;---------------------------------------------------------------------------------------------
; HANDLE COMMANDS:
;---------------------------------------------------------------------------------------------
SetResolution movf RECEIVE, w
movwf RESOLUTION
goto MainLoop
SetReportRate movf RECEIVE, w
movwf REPORT_RATE
goto MainLoop
;0xE6 - Set Scaling 1:1
Mouse_E6 bcf FLAGS, SCALE
goto MainLoop
;0xE7 - Set Scaling 2:1
Mouse_E7 bsf FLAGS, SCALE
goto MainLoop
;0xE9 - Status Request
Mouse_E9 movf FLAGS, w
btfss PS2_Bl
iorlw 0x04
btfss PS2_Br
iorlw 0x01
movwf SEND1
movf RESOLUTION, w
movwf SEND2
movf REPORT_RATE, w
movwf SEND3
movlw 0x03
movwf LENGTH
goto PacketOut
;0xEA - Set Stream Mode
Mouse_EA bcf FLAGS, MODE
goto MainLoop
;0xF0 - Set Remote Mode
Mouse_F0 bsf FLAGS, MODE
goto MainLoop
;0xF2 - Get Device ID
Mouse_F2 clrf SEND1
movlw 0x01
movwf LENGTH
goto PacketOut
;0xF4 - Enable Reporting
Mouse_F4 bsf FLAGS, ENABLE
goto MainLoop
;0xF5 - Disable Reporting
Mouse_F5 bcf FLAGS, ENABLE
goto MainLoop
;0xF6 - Set Default
Mouse_F6 call LoadDefaults
goto MainLoop
;Invalid command
Mouse_ERR movlw 0xFE
call ByteOut
goto MainLoop
;---------------------------------------------------------------------------------------------
; OUTPUT ONE BYTE: - TIMING IS CRITICAL!!!
;---------------------------------------------------------------------------------------------
ByteOut movwf TEMP0
InhibitLoop btfss CLOCK ;Test for inhibit
goto InhibitLoop
Delay 100 ;(50 microsec = 58 clock cycles, min)
btfss CLOCK
goto InhibitLoop
btfss DATA ;Check for request-to-send
retlw 0xFF
clrf PARITY
movlw 0x08
movwf COUNTER
movlw 0x00
call BitOut ;Start bit (0)
btfss CLOCK ;Test for inhibit
goto ByteOutEnd
Delay 4
ByteOutLoop movf TEMP0, w
xorwf PARITY, f
call BitOut ;Data bits
btfss CLOCK ;Test for inhibit
goto ByteOutEnd
rrf TEMP0, f
decfsz COUNTER, f
goto ByteOutLoop
Delay 2
comf PARITY, w
call BitOut ;Parity bit
btfss CLOCK ;Test for inhibit
goto ByteOutEnd
Delay 5
movlw 0xFF
call BitOut ;Stop bit (1)
Delay 48
retlw 0x00
ByteOutEnd bsf STATUS, RP0
bsf DATA
bsf CLOCK
bcf STATUS, RP0
retlw 0xFE
BitOut bsf STATUS, RP0
andlw 0x01
btfss STATUS, Z
bsf DATA
btfsc STATUS, Z
bcf DATA
Delay 21
bcf CLOCK
Delay 45
bsf CLOCK
bcf STATUS, RP0
Delay 5
return
;---------------------------------------------------------------------------------------------
; READ ONE BYTE: (Takes about 1ms) - TIMING IS CRITICAL!!!
;---------------------------------------------------------------------------------------------
ByteIn btfss CLOCK ;Test for Request-to-send
retlw 0xFE
btfsc DATA
retlw 0xFE
movlw 0x08
movwf COUNTER
clrf PARITY
Delay 28
ByteInLoop call BitIn ;Data bits
btfss CLOCK ;Test for inhibit
retlw 0xFE
bcf STATUS, C
rrf RECEIVE, f
iorwf RECEIVE, f
xorwf PARITY,f
decfsz COUNTER, f
goto ByteInLoop
Delay 1
call BitIn ;Parity bit
btfss CLOCK ;Test for inhibit
retlw 0xFE
xorwf PARITY, f
Delay 5
ByteInLoop1 Delay 1
call BitIn ;Stop bit
btfss CLOCK ;Test for inhibit
retlw 0xFE
xorlw0x00
btfsc STATUS, Z
clrf PARITY
btfsc STATUS, Z ;Stop bit = 1?
goto ByteInLoop1 ; No--keep clocking.
bsf STATUS, RP0 ;Acknowledge
bcf DATA
Delay 11
bcf CLOCK
Delay 45
bsf CLOCK
Delay 7
bsf DATA
bcf STATUS, RP0
btfss PARITY, 7 ;Parity correct?
retlw 0xFF ; No--return error
Delay 45
retlw 0x00
BitIn Delay 8
bsf STATUS, RP0
bcf CLOCK
Delay 45
bsf CLOCK
bcf STATUS, RP0
Delay 21
btfsc DATA
retlw 0x80
retlw 0x00 ;-------------------------------------
--------------------------- ; DELAY: ;-------------
--------------------------------------------------------------------------------
;Delays 4w+4 cycles (including call,return, and movlw) (0=256)
Delay_us addlw -1 ;Precise delays used in I/O
btfss STATUS, Z
goto Delay_us
return
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -