单片机与单片机串行通讯+键盘扫描+数码管显示
发送的子程序:
MOV TMOD,#20H
MOV SCON,#11010000B
ANL PCON,#01111111B
MOV TH1,#0FDH
MOV TL1,#0FDH
SETB TR1
ALOOP1: INC R7
MOV A,R7
MOV 35H,A
MOV SBUF,#0E1H
JNB TI,$
CLR TI
JNB RI,$
CLR RI
MOV A,SBUF
XRL A,#0E2H
JNZ ALOOP1
ALOOP2: MOV R0,#30H
MOV R7,#06H
MOV R6,#00H
ALOOP3:MOV SBUF,@R0
MOV A,R6
ADD A,@R0
MOV R6,A
INC R0
JNB TI,$
CLR TI
DJNZ R7,ALOOP3
MOV SBUF,R6
JNB TI,$
CLR TI
JNB RI,$
CLR RI
MOV A,SBUF
CJNE A,#00H,ALOOP2
CLR TI
CLR RI
RET
接收子程序:
MOV TMOD,#20H
MOV SCON,#11010000B
ANL PCON,#01111111B
MOV TH1,#0FDH
MOV TL1,#0FDH
SETB TR1
BLOOP1:
JNB RI,$
CLR RI
MOV A,SBUF
XRL A,#0E1H
JNZ BLOOP1
MOV SBUF,#0E2H
JNB TI,$
CLR TI
RETT: MOV R0,#30H
MOV R7,#06H
MOV R6,#00H
BLOOP2:JNB RI,$
CLR RI
MOV A,SBUF
MOV @R0,A
INC R0
ADD A,R6
MOV R6,A
DJNZ R7,BLOOP2
JNB RI,$
CLR RI
MOV A,SBUF
XRL A,R6
JZ END1
MOV SBUF,#0FFH
JNB TI,$
CLR TI
AJMP RETT
END1:MOV SBUF,#00H
JNB TI,$
CLR TI
CLR RI
AJMP BLOOP1
中断显示,数码管显示:
;针对实验板的键盘扫描,显示程序
;使用中断T0进行键盘的扫描和T1进行显示
;显示缓冲区设在30H到35H里
;***********************初始化程序*********************
ORG 0000H
AJMP MAIN
ORG 000BH
AJMP IT00
ORG 0030H
MAIN: MOV R0,#7FH
LOOP: MOV @R0,#00H
DJNZ R0,LOOP
MOV SP,#70H
MOV 37H,#30H ;把30H的一个缓冲区第一位保存,以后作为指针来用
MOV 09H,#30H ;把30H放到第一组工作寄存器R1中
MOV 08H,#30H ;把30H放到第一组工作寄存器R0中
MOV 0CH,#20H
CLR 00H
CLR 01H
CLR 02H ;初始化位地址00H,01H,02H
MOV TMOD,#01H
MOV TH0,#0FCH
MOV TL0,#018H
SETB TR0
SETB ET0
SETB EA ;启动定时
HEAR:
AJMP HEAR
;*******************************中断T0键盘+显示**********************
IT00:
CLR EA
PUSH ACC
PUSH B
PUSH PSW
PUSH DPH
PUSH DPL
SETB RS0
CLR RS1
MOV DPTR,#1FFFH
MOV A,#00H
MOVX @DPTR,A
JB P1.7,PASS0
JB 00H,CP
SETB 00H
AJMP PASS
CP:JB 01H,PASS
SETB 01H
AJMP KEYSELECT
;*********************判断健盘的具体位置********************
KEYSELECT:
MOV A,#0FEH
MOVX @DPTR,A
JNB P1.7,KEYS5
RL A
MOVX @DPTR,A
JNB P1.7,KEYS4
RL A
MOVX @DPTR,A
JNB P1.7,KEYS3
RL A
MOVX @DPTR,A
JNB P1.7,KEYS2
RL A
MOVX @DPTR,A
JNB P1.7,KEYS9
RL A
MOVX @DPTR,A
JNB P1.7,KEYS8
RL A
MOVX @DPTR,A
JNB P1.7,KEYS7
RL A
MOVX @DPTR,A
JNB P1.7,KEYS6
;******************************键盘功能模块**************************
KEYS4:
AJMP PASS
KEYS6:JB 02H,PASS
MOV A,@R0
MOV R5,A
INC R5
MOV A,R5
MOV @R0,A
CJNE R5,#0AH,PASS
MOV @R0,#00H
AJMP PASS
KEYS7:JB 02H,PASS
MOV A,@R0
MOV R5,A
DEC R5
MOV A,R5
MOV @R0,A
CJNE R5,#0FFH,PASS
MOV @R0,#09H
AJMP PASS
KEYS8:JB 02H,PASS
MOV R0,37H
INC R0
MOV 37H,R0
CJNE R0,#36H,PASS
MOV 37H,#30H
MOV R0,37H
AJMP PASS
KEYS2:
AJMP PASS
KEYS5:
CPL 02H
AJMP PASS
KEYS3:AJMP PASS
KEYS9:AJMP PASS
;************************键盘功能结束******************************
PASS0:
CLR 00H
CLR 01H
PASS:
;****************************中断T1显示***************************
MOV A,@R1
mov dptr, #tab
MOVC A,@A+DPTR
MOV R2,A
JB 02H,RE
;*********************闪烁程序**********************
MOV A,R0
CLR C
SUBB A,R1
JNZ RE
INC R3
MOV A,R3
SUBB A,#4FH
JNC RE
MOV A,#0FFH
MOV DPTR, #9FFFH
MOVX @DPTR,A
AJMP RE1
RE:
MOV dptr,#09FFFH
MOV A,R2
MOVX @DPTR,A
RE1: MOV A,R4
MOV DPTR,#7FFFH
MOVX @DPTR,A
INC R1
MOV A,R4
JB ACC.0,FORMAT
RR A
MOV R4,A
AJMP EXIT
FORMAT: MOV R1,#30H
MOV R4,#20H
EXIT: MOV TH0,#0FCH
MOV TL0,#018H
POP DPL
POP DPH
POP PSW
POP B
POP ACC
SETB EA
RETI
TAB: DB 0c0H, 0f9H, 0a4H, 0b0H, 99H, 92H
; 0 1 2 3 4 5
; 00H 01H 02H 03H 04H 05H
;
DB 82H, 0f8H, 80H, 90H 0FFH
; 6 7 8 9
; 7DH 07H 08H 09H 0A
;
end