DB 0x66,0x6D,0x7D,0x07,0x7F,0x6F
-------------------------zoupp-----------------------------------------------
(1) ORG 0000H
JMP MAIN
ORG 001BH
RETI
ORG 0023H
JMP S_INT
MAIN:MOVR0,#30H;R0发送起始地址
MOV R1,#40H;R1接收起始地址
MOV R2,#0;R2用来判断按键值
MOV R3,#0;R3计按下次数
MOV SP,#60H
MOV TMOD,#20H;T1工作方式2
MOV TL1,#0FAH;在fosc=11.0592MHz时
MOV TH1,#0FAH;波特率为4800b/s
MOV PCON,#00H
MOV SCON,#50H;工作方式1,允许接收
SETB ET1;开中断
SETB TR1
SETB ES
SETB EA
LOP1:MOV P1,#0FFH
ACALL DELAY;防抖
MOV A,P1
CJNEA,#0FFH,LOP2;等待按键按下
JMP LOP1
LOP2: RRC A ;按键按下时计算按键值
JNC LOP3
INC R2
JMP LOP2
LOP3: MOV A,R2 ;按键值送A
MOV @R0,A ;按键值存入发送区
MOV SBUF,A ;发送按键值
INC R0;放送地址指针加一
LOP4:MOV A,P1;等待按键松开
CJNEA,#0FFH,LOP4
MOV R2,#0
INC R3;按键按下个数加一
JMP LOP1
DISP: MOV DPTR,#TB;显示子程序
MOVC A,@A+DPTR
MOV P2,A
RET
TB:db 3fh,06h,5bh,4fh,66h,6DH,7DH,07H;0—7
DELAY: MOV 20H,#100H;延时子程序
DEL1:MOV 21H,#100H
DJNZ 21H,$
DJNZ 20H,DEL1
RET
S_INT: JB TI,SEND ;TI=1则发送结束跳到SEND; 串行口中断程序
MOV A,SBUF;接收取值
MOV @R1,A ;把接收数据存入接收地址
INC R1 ;接收地址指针加一
ACALL DISP ;调用显示子程序
CLR RI;清接收标准位
RETI
SEND: CLR TI;清发送标志位
RETI
(2)ORG 0000H
JMP MAIN
ORG 0023H
JMP S_INT
MAIN:CLR F0;F0 用于区分接收到的数据是
MOV P0,#0;按键次数,还是按键值
MOV P2,#0
MOV R0,#30H;R0发送起始地址
MOV R1,#40H;R1接收起始地址
MOV R2,#0;R2用来判断按键值
MOV R3,#0;R3计按下次数
MOV SP,#60H
MOV TMOD,#20H;T1工作方式2
MOV TL1,#0F4H;在fosc=11.0592MHz时
MOV TH1,#0F4H;波特率为2400b/s
MOV PCON,#00H
MOV SCON,#0D0H;工作方式3,允许接收SM2=0
CLR ET1
SETB TR1;开中断
SETB ES
SETB EA
LOP1:MOV P1,#0FFH
ACALL DELAY;防抖
MOV A,P1
CJNE A,#0FFH,LOP2;等待按键按下
JMP LOP1
LOP2: RRC A;按键按下时计算按键值
JNC LOP3
INC R2
JMP LOP2
LOP3: MOV A,R2;按键值送A
MOV C,P;进行奇校验
CPL C
MOV TB8,C;把奇校验位送入TB8
MOV @R0,A;按键值存入发送区
ACALL DISP
MOV SBUF,@R0;发送按键值
JNB TI,$
CLR TI
INC R0;发送地址指针加一
LOP4: MOV A,P1;等待按键松开
CJNE A,#0FFH,LOP4
MOV R2,#0
INC R3 ;按键按下个数加一
MOV P0,R3 ;按下次数送P0显示
MOV A,R3
MOV C,P ;奇校验
CPL C
MOV TB8,C
MOV SBUF,R3 ;送按下次数
JNB TI,$
CLR TI
JMP LOP1
DISP:MOV DPTR,#TB;显示子程序
MOVC A,@A+DPTR
P2,A
RET
TB:db 3fh,06h,5bh,4fh,66h,6DH,7DH,07H;0—7
DELAY: MOV 20H,#100H;延时子程序
DEL1:MOV 21H,#100H
DJNZ 21H,$
DJNZ20H,DEL1
RET
S_INT: PUSH A;串行口中断程序
JB TI,SEND;TI=1则发送结束跳到SEND
CPL F0
CLR RI;清接收标准位
MOV A,SBUF ;接收取值
MOV C,P;进行奇校验
JNC S1 ;
JB RB8,REP ;
AJMP S2 ;
S1:JNB RB8,REP
S2:JNB F0,GESHU ;F0=0则接收到按键个数据
MOV @R1,A;把接收数据存入接收地址
INC R1;接收地址指针加一
ACALL DISP;调用显示子程序
REP:POP A
RETI
GESHU:MOV P0,SBUF ;把接收到的按键次数送显
POP A
RETI
SEND: POP A ;发送结束
RETI
--------------------------chen----------------------------------------------
ORG 0000H
LJMPMAIN
ORG 0030H//03 0B 13 1B 23
MAIN: //初始化
MOVSP,#70H
MOVSCON,#0D0H//设置串口工作方式3,可接收1101
MOVTMOD,#20H//设置T1,8位重载
MOVPCON,#00H;
MOVTH1,#0F4H//11.0592M,波特2400
MOVTL1,#0F4H//
SETBTR1//启动产生波特率
MOV P0,#0FFH//LED显示开关次数
MOV P2,#00H //数码管显示开关编号
MOVR0,#00H//循环得出开关编号所用
MOVR2,#00H// 开关动作总次数寄存器清0
MOVR1,#40H//建立存放开关编号的RAM的数据指针
//扫描键盘
RDK: MOVP1,#0FFH;
MOVA,P1
CJNEA,#0FFH,AGAIN;//消抖
JMPRDK ;无按键按下,跳回继续扫描
AGAIN: LCALLTIME110MS//延时110毫秒,proteus中10毫秒消抖作用不明显
MOVP1,#0FFH;
MOVA,P1
CJNEA,#0FFH,NEXT;有按键按下,查哪个按键
JMPRDK ;无按键按下,跳回继续扫描
// 开关动作总次数显示
NEXT: INCR2;
MOVP0,R2;
//得出开关编号,并让数码管显示
MOVR0,#00H
LOOP: RRCA
JNCDISPLAY
INCR0;Cy位不为0,变量加一,继续右循环
JMPLOOP
DISPLAY:MOV A,R0
MOVDPTR,#TAB
MOVCA,@A+DPTR
MOVP2,A
//开关编号存入RAM
MOV@R1,A
INCR1
//以下通过串口发送,按键次数和按键号
NUMBE: MOVC,P//奇校验 TB8和P相反
CPLC
MOVTB8,C
MOVSBUF,A
WAIT: JBCTI, RESPOND // 发送完后,接收,看乙机是否正确接收
SJMPWAIT
RESPOND:JB RI,JUDGE
SJMPRESPOND
JUDGE: MOVA,SBUF
CLRRI
CJNEA,#00H, NUMBE//接受到00H,传输没问题,继续发送开关总次数;接受非00H,出问题,再发
MANY: MOVA,R2
MOVC,P
CPLC
MOVTB8,C
MOVSBUF,A
WAIT1: JBCTI,RESPOND2
SJMPWAIT1
RESPOND2:JB RI,JUDGE2
SJMPRESPOND2
JUDGE2: MOVA,SBUF
CLRRI
CJNEA,#00H, MANY//接受到00H,传输没问题,;接受非00H,出问题,再发
JMPRDK
TAB:DB 0x3F,0x06,0x5B,0x4F ;共阴编码表,跟实验电路板不一样
DB 0x66,0x6D,0x7D,0x07,0x7F,0x6F
TIME110MS: MOVR5,#0FFH
TIME110MS_1: MOV R6,#0FFH
DJNZR6,$
DJNZR5,TIME110MS_1
RET
END
//第二小实验乙机程序
ORG 0000H
LJMP MAIN
ORG 0030H
MAIN: MOV SP,#70H
MOVSCON,#0D0H //设置串口工作方式1
MOVPCON,#00H;
MOVTMOD,#20H//设置T1,8位重载
MOVTH1,#0F4H//11.0592M,波特率2400
MOVTL1,#0F4H
SETBTR1
MOV P1,#0FFH //开关动作总次数LED显示清0
MOVP2,#00H//数码管显示 灭
//以下接收通信数据
NUMBE: JBRI,JUDGE
SJMPNUMBE
JUDGE: MOVA,SBUF
MOVC,P
JCODD//8位数据有奇数个1
JBRB8,GO//8位数据有偶数个1
JMPAGAIN//传输出问题
ODD: JNBRB8,GO//传输无问题
AGAIN: MOVA,#05H//传输出问题,请求重发
MOVSBUF,A
WAIT: JBCTI,NUMBE
SJMPWAIT
GO: MOVP2,A
CLRRI
RESPOND:MOV A,#00H
MOVSBUF,A
WAIT1: JBCTI,MANY
JMPWAIT1
MANY: JBRI,JUDGE2
SJMPMANY
JUDGE2: MOVA,SBUF
MOVC,P
JCODD1//8位数据有奇数个1
JBRB8,GO1
JMPAGAIN1//传输出问题
ODD1: JNBRB8,GO1//传输无问题
AGAIN1: MOVA,#05H//传输出问题,请求重发
MOVSBUF,A
WAIT2: JBCTI,MANY
SJMPWAIT2
GO1: MOVP1,A
CLRRI
RESPOND1:MOV A,#00H
MOVSBUF,A
WAIT3: JBCTI,NUMBE
JMPWAIT3
END
ORG 0000H
LJMPMAIN
ORG 0030H//03 0B 13 1B 23
MAIN: //初始化
MOVSP,#70H
MOVSCON,#50H//设置串口工作方式1,可接收
MOVTMOD,#20H//设置T1,8位重载
MOVPCON,#80H;
MOVTH1,#0F4H//11.0592M,波特率4800
MOVTL1,#0F4H//
SETBTR1//启动产生波特率
MOV P0,#0FFH
MOV P2,#00H
MOVR0,#00H//循环得出开关编号
MOVR2,#00H//暂存A中存放的开关编号
MOVR1,#40H//建立存放开关编号的RAM的数据指针
//扫描键盘
RDK: MOVP1,#0FFH;
MOVA,P1
CJNEA,#0FFH,AGAIN;//消抖
JMPRDK ;无按键按下,跳回继续扫描
AGAIN: LCALLTIME110MS//延时110毫秒,proteus中10毫秒消抖作用不明显
MOVP1,#0FFH;
MOVA,P1
CJNEA,#0FFH,NEXT;有按键按下,查哪个按键
JMPRDK ;无按键按下,跳回继续扫描
//得出开关编号,并让数码管显示
NEXT: MOVR0,#00H
LOOP: RRCA
JNCDISPLAY
INCR0;Cy位不为0,变量加一,继续右循环
JMPLOOP
DISPLAY:MOV A,R0
MOVDPTR,#TAB
MOVC A,@A+DPTR
MOVP2,A
MOVR2,A
MOV@R1,A //开关编号存入RAM
INCR1
//以下是通信协议
SHHANDS1:MOV A,#06H //发送“握手码06H”1次
MOVSBUF,A
WAIT1: JBCTI,SHHANDS2 //
SJMPWAIT1
SHHANDS2:MOV A,#06H //发送“握手码06H”2次
MOVSBUF,A
WAIT2: JBCTI,SHHANDS3 //
SJMPWAIT2
SHHANDS3:MOV A,#06H //发送“握手码06H”3次
MOVSBUF,A
WAIT3: JBCTI,RESPOND //
SJMPWAIT3
RESPOND:JB RI,JUDGE//接受“应答码” //RESPOND:JBC RI,JUDGE//接受“应答码”
SJMPRESPOND//
JUDGE: MOVA,SBUF
CLRRI
CJNEA,#00H,SHHANDS1//根据约定(00H正确),判断,如果不是通信对象,重发“握手码”
//以下通过串口发送按键编号
MOVSBUF,R2
WAIT4: JBCTI, RDK //
SJMPWAIT4
TAB:DB 0x3F,0x06,0x5B,0x4F ;共阴编码表,跟实验电路板不一样
DB 0x66,0x6D,0x7D,0x07,0x7F,0x6F
TIME110MS: MOVR5,#0FFH
TIME110MS_1: MOV R6,#0FFH
DJNZR6,$
DJNZR5,TIME110MS_1
RET
END
//第一小实验乙机程序
ORG 0000H
LJMP MAIN
ORG 0030H
MAIN: MOV SP,#70H
MOVSCON,#50H //设置串口工作方式1
MOVPCON,#80H;
MOVTMOD,#20H//设置T1,8位重载
MOVTH1,#0F4H//11.0592M,波特率4800
MOVTL1,#0F4H
SETBTR1
MOV P1,#0FFH // 开关动作总次数LED显示清0
MOVP2,#00H//数码管显示 灭
MOVR1,#50H//保存数据起始地址
STA: SETBF0
//以下是通信协议
SHHANDS1:JB RI,JUDGE1 //接受“握手码06H”
SJMPSHHANDS1
JUDGE1: MOVA,SBUF
CLRRI
CJNEA,#06H,SIGN1//判断
SHHANDS2:JB RI,JUDGE2 //接受“握手码06H”
SJMPSHHANDS2
JUDGE2: MOVA,SBUF
CLRRI
CJNEA,#06H,SIGN2//判断
SHHANDS3:JB RI,JUDGE3 //接受“握手码06H”
SJMPSHHANDS3
JUDGE3: MOVA,SBUF
CLRRI
CJNEA,#06H,SIGN3//判断
LJMPRESULT
SIGN1: CLRF0
LJMPSHHANDS2
SIGN2: CLRF0
LJMPSHHANDS3
SIGN3: CLRF0
RESULT: JBCF0,RIGHT//握手码正确,发应答码00H
MOVA,#05H// 握手码错误,发应答码05H
MOVSBUF,A
WAIT1: JBCTI,STA //等待甲机再发握手码
SJMPWAIT1
RIGHT: MOVA,00H//发送“应答码”
MOVSBUF,A
WAIT: JBCTI,JSHOU
SJMPWAIT
//以下接收通信数据
JSHOU: JBRI,NUMBE
SJMPJSHOU
NUMBE: MOVA,SBUF
MOVP2,A
MOV@R1,A
INCR1
CLRRI
SJMPSTA//回去,等待下次握手
END