#include p16f877a.inc
__config h'3f39'
cblock 0x20
keycode
keycont
keyrelease
keytrag
keyflag
readdata
intnum
keyfifo:8
putptr
getptr
key
endc
cblock 0x70
status_tmp
w_tmp
endc
#DEFINE KEY1 0X01
#DEFINE KEY2 0X02
#DEFINE KEY3 0X04
#DEFINE KEY4 0X08
#define led PORTD
org 0
goto init
org 4
goto int_sv
init
BANKSEL PORTD
clrf portd
clrf portb
BANKSEL TRISD
clrf trisd
movlw 0x0f
movwf trisb
bcf option_reg,not_rbpu
BANKSEL TMR1H
MOVLW 0xd8
MOVWF TMR1H
MOVLW 0xf0
MOVWF TMR1L
BANKSEL INTCON
MOVLW 0xC0
MOVWF INTCON
BANKSEL PIE1
MOVLW 0x01
MOVWF PIE1
BANKSEL PIR1
CLRF PIR1
BANKSEL T1CON
MOVLW 0x01
MOVWF T1CON
main
call _keyscan
call _keypro
call _keyget
movf key,0
movwf led
loop
movf intnum,0
xorlw 0x01
bnz loop
clrf intnum
goto main
_keyscan
MOVF PORTB,0
XORLW 0XFF
andlw 0x0F
MOVWF ReadData
XORWF KEYCont,0
ANDWF ReadData,0
MOVWF KEYTRAG
MOVF ReadData,0
XORWF KEYCont,0
ANDWF KEYCONT,0
MOVWF KEYRELEASE
MOVF ReadData,0
MOVWF KEYCont
RETURN
_keypro
_key1_01
movf keytrag,0
xorlw key1
bnz _key2_01
movlw key1
movwf keycode
goto _keyput
_key2_01
movf keytrag,0
xorlw key2
bnz _key3_01
movlw key2
movwf keycode
goto _keyput
_key3_01
movf keytrag,0
xorlw key3
bnz _key4_01
movlw key3
movwf keycode
goto _keyput
_key4_01
movf keytrag,0
xorlw key4
bnz _key1_10
movlw key4
movwf keycode
goto _keyput
_key1_10
movf keyrelease,0
xorlw key1
bnz _key2_10
movlw key1+0x10
movwf keycode
goto _keyput
_key2_10
movf keyrelease,0
xorlw key2
bnz _key3_10
movlw key2+0x10
movwf keycode
goto _keyput
_key3_10
movf keyrelease,0
xorlw key3
bnz _key4_10
movlw key3+0x10
movwf keycode
goto _keyput
_key4_10
movf keyrelease,0
xorlw key4
bnz _keypro_90
movlw key4+0x10
movwf keycode
goto _keyput
_keyput
bankisel keyfifo
movlw keyfifo&0xff
addwf putptr,0
movwf fsr
movf keycode,0
movwf indf
incf putptr,1
movlw 0x07
andwf putptr,1
_keypro_90
return
_keyget
banksel putptr
movf putptr,0
xorwf getptr,0
bz _keyget90
bankisel keyfifo
movlw keyfifo&0xff
addwf getptr,0
movwf fsr
movf indf,0
movwf key
incf getptr,1
movlw 0x07
andwf getptr,1
_keyget90
return
int_sv
movwf W_TMP
MOVF STATUS,W
MOVWF W_TMP
BANKSEL PIE1
BTFSS PIE1,0
GOTO INT_EXIT
BANKSEL PIR1
BTFSS PIR1,0
GOTO INT_EXIT
BCF PIR1,0
; bsf fkeyscan_en
BANKSEL T1CON
; BCF T1CON,0
BANKSEL TMR1H
movLW 0xb0
addWF TMR1L
clrf tmr1h
skpnc
incf tmr1h
MOVLW 0x3c
addWF TMR1H
; BSF T1CON,0
INCF INTNUM,1
INT_EXIT
MOVF STATUS_TMP,W
MOVWF STATUS
SWAPF W_TMP,F
SWAPF W_TMP,W
RETFIE
end |