;使用C口作键盘扫描,7段数码管显示按键号 ,63系列通用寄存器地址:20~7FH, A0~FFH ,未达到预期目的,只用个位数显示按键号时正常工作,增加按键号转换(用2位数显示),则提示“STACK OVERFLOW"--堆栈溢出.编译后机器码容量为1.3KB左右,未超过CALL跳转范围——1页(2K) 。 #include P16C63A.INC
KEYCODE EQU H'0020' KEYNUM EQU H'0021' CC EQU H'0024' TABADD EQU H'0025' SW EQU H'0026' GW EQU H'0027' ;PD EQU H'0028'
ORG H'0000' GOTO MAIN ORG H'0010'
MAIN MOVLW H'00' TRIS PORTA TRIS PORTB ;RB口全为输出,用作驱动7段数码管 MOVWF KEYNUM MOVLW H'F0' TRIS PORTC ; 设置RC低四位为输出,高四位为输入 MOVLW TABLE MOVWF TABADD MOVLW H'0001' ; MOVWF PD ;用来判断按键号是否大于9
LOOP CALL KEYSCAN ; CALL JUDGE ;判断是按键号是否大于9 ; XORLW H'0001' ; BTFSS STATUS,Z CALL XIAO ; GOTO DASHU XS CALL DISP_2 GOTO LOOP ;循环扫描、显示按键号
JUDGE MOVLW H'0A' SUBWF KEYNUM,0 BTFSC STATUS,0 ; C标志 ; RETLW 0 ;小于10 ; RETLW 1 ; 10以上的按键号 GOTO XIAO GOTO DASHU
XIAO MOVLW 0 MOVWF SW MOVF KEYNUM,0 MOVWF GW RETLW 0 ; GOTO XS
DASHU MOVLW H'01' MOVWF SW MOVLW H'0A' SUBWF KEYNUM,0 MOVWF GW RETLW 0 ; GOTO XS
DISP_2 MOVLW H'02' MOVWF PORTA MOVF SW,0 CALL DISP CALL DELAY
MOVLW H'01' MOVWF PORTA MOVF GW,0 CALL DISP CALL DELAY RETLW 0
DELAY MOVLW 0 MOVWF CC LL DECFSZ CC,1 GOTO LL RETLW 0
DISP ; MOVF KEYNUM,0 ;输入W,输出W ADDWF TABADD,0 CALL CONV MOVWF PORTB RETLW 0 CONV MOVWF PCL ;取码,输入W(0~F),输出W TABLE RETLW H'C0' RETLW H'F9' RETLW H'A4' RETLW H'B0' RETLW H'99' RETLW H'92' RETLW H'82' RETLW H'F8' RETLW H'80' RETLW H'90' RETLW H'88' RETLW H'83' RETLW H'C6' RETLW H'A1' RETLW H'86' RETLW H'8E' ;0~F 数码管代码 RETLW H'C0'
KEYSCAN MOVLW H'FE' ;第一行置低(RC0输出0) MOVWF PORTC NOP MOVF PORTC,0 ;读RC各端口状态 ANDLW H'F0' ;屏蔽低四位,只需要低四位来判断 MOVWF KEYCODE ;保存端口状态 MOVLW H'F0' SUBWF KEYCODE,0 ;与初值比较,看是否有键按下 BTFSS STATUS,Z ;有键按下(不等于初值,Z为零)则结束扫描 GOTO GETNUM1
MOVLW H'FD' ;第二行置低(RC1输出0) MOVWF PORTC NOP MOVF PORTC,0 ;读RC各端口值 ANDLW H'F0' ;屏蔽低四位,只需要低四位来判断 MOVWF KEYCODE ;保存端口状态 MOVLW H'F0' SUBWF KEYCODE,0 ;与初值比较,看是否有键按下 BTFSS STATUS,Z ;有键按下(不等于初值,Z为零)则结束扫描 GOTO GETNUM2
MOVLW H'FB' ;第三行置低(RC2输出0) MOVWF PORTC NOP MOVF PORTC,0 ;读RC各端口值 ANDLW H'F0' ;屏蔽低四位,只需要低四位来判断 MOVWF KEYCODE ;保存端口状态 MOVLW H'F0' SUBWF KEYCODE,0 ;与初值比较,看是否有键按下 BTFSS STATUS,Z ;有键按下(不等于初值,Z为零)则结束扫描 GOTO GETNUM3
MOVLW H'F7' ;第四行置低(RC3输出0) MOVWF PORTC NOP MOVF PORTC,0 ;读RC各端口值 ANDLW H'F0' ;屏蔽低四位,只需要低四位来判断 MOVWF KEYCODE ;保存端口状态 MOVLW H'F0' SUBWF KEYCODE,0 ;与初值比较,看是否有键按下 BTFSS STATUS,Z ;有键按下(不等于初值,Z为零)则结束扫描 GOTO GETNUM4 FH RETLW 0 ; 子程序返回
QUDOU MOVF PORTC,0 ;去抖处理 SUBWF KEYCODE,0 BTFSC STATUS,Z GOTO QUDOU GOTO FH
|