单片机课程设计多机通信.docx
《单片机课程设计多机通信.docx》由会员分享,可在线阅读,更多相关《单片机课程设计多机通信.docx(15页珍藏版)》请在冰豆网上搜索。
![单片机课程设计多机通信.docx](https://file1.bdocx.com/fileroot1/2023-2/2/11b5e262-96de-43cc-bcc4-6a9eba8ed5ec/11b5e262-96de-43cc-bcc4-6a9eba8ed5ec1.gif)
单片机课程设计多机通信
单片机多机通信
本程序能实现多机通信,具体功能为:
主机可以写入任何数据,长度可以为1位到6位,可以取不同的发送模式,单机发送可以为1号机或者2号机独立发送也可以广播发送,发送的数据在接收机上同样用数码管显示,子机可以自行清除数据。
通过本训练可以实现多机通信。
;主机程序:
D1EQU10H;为发送1号机
D2EQU20H;为发送2号机
D3EQU30H;为广播发送
MAIN:
MOVTMOD,#20H
MOVTH1,#0F4H
MOVTL1,#0F4H
SETBTR1
MOVSCON,#0D0H
MOVR0,#20H;发送首址20H-25H
MOVR5,#6H
MOVA,#1H
LOOP:
MOV@R0,A
INCA
INCR0
DJNZR5,LOOP;上面为赋值123456到20h-25h
MOVR5,#6H
MOVR0,#20H
MOV26H,#0FEH;显示的初始化
MOV27H,R5
START:
JNBP3.4,CON1;为0则为发送1号机
JNBP3.5,CON2;为0则为发送2号机
JNBP3.6,CON3;为0则发送广播
JNBP3.7,SOP;显示数据加20H单元的这里使用74ls芯片的脉冲所以用JNB,若没有可;用开关为JBP3.7,sop同时下面的改成JNBP3.7,$
JBP3.7,$
MOVR1,#1FH
MOVA,R5
ADDA,R1
MOVR1,A
INC@R1
CJNE@R1,#0AH,SOP
MOV@R1,#0H
SOP:
JBP3.3,STOP
JNBP3.3,$
INCR5
CJNER5,#7H,STOP
MOVR5,#1H
STOP:
ACALLDISPLAY;要传送的数码显示
SJMPSTART
CON1:
JNBP3.4,$;下面为发送的机号
MOV28H,#D1
SJMPBIGI
CON2:
JNBP3.5,$
MOV28H,#D2
SJMPBIGI
CON3:
JNBP3.6,$
MOVR1,#D3
BIGI:
MOV27H,R5;存放长度
MOVR0,#20H
BIGIN:
SETBTB8;要置位
MOVSBUF,28H
WAIT1:
JBCTI,TRS0
AJMPWAIT1;发送机号
TRS0:
JBCRI,WAIT;有没有机接到并返回数据
SJMPTRS0
WAIT:
MOVA,SBUF
CJNEA,#11H,START;为11表示有机接到,不为则从头开始
TRS1:
CLRTB8
MOVSBUF,R5;发送长度
WAIT2:
JBCTI,TRS2
AJMPWAIT2
;*********发送数据*****
TRS2:
MOVA,@R0
CLRTB8;要清
MOVSBUF,A
INCR0
WAIT3:
JBCTI,CONT
AJMPWAIT3
CONT:
DJNZR5,TRS2
;对一些在显示用到的量从新赋值
MOVR0,#20H
MOV26H,#0FEH
MOVR5,27H
SJMPSTART
;下面是显示程序
DISPLAY:
JBP1.0,DISPL
;ACALLCLEAN
DISPL:
MOVP2,#0FFH
MOVDPTR,#DAB
MOVA,@R0
MOVCA,@A+DPTR
MOVP0,A
INCR0
MOVA,26H
MOVP2,A
RLA
MOV26H,A
ACALLDELAY
DJNZ27H,OUT
MOVR0,#20H
MOV26H,#0FEH
MOV27H,R5
OUT:
RET
DELAY:
MOVR6,#10H
LOP1:
MOVR7,#10H
LOP2:
NOP
NOP
DJNZR7,LOP2
DJNZR6,LOP1
RET
DAB:
DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,00H
DB5EH,79H,71H,73H,3EH,31H,6EH,0FFH,00H
END
;通信子机1号
D1EQU10H
D3EQU30H
START:
MOVTMOD,#20H
MOVTH1,#0F4H
MOVTL1,#0F4H
SETBTR1
MOV41H,#6H
MOV27H,#6H
MAIN:
MOVSCON,#0F0H;SM2为1
MOV26H,#0FEH
MOVR0,#20H;存储首址
SETB7FH
SETB7EH
SETBREN
WAIT:
ACALLDISPLAY
JBCRI,READ
SJMPWAIT
READ:
MOVA,SBUF
CJNEA,#D1,LOP;检查机号是否对配
SJMPBINGIN
LOP:
CJNEA,#D3,EXIT;检查是否为广播
SJMPBINGIN
EXIT:
SETBSM2
SJMPWAIT
BINGIN:
MOVR0,#20H
MOVA,#11H
MOVSBUF,A;为了能正确接收导数据,因为在接收时不能及时读取RI(显示功能的时间延迟)
TT1:
JBCTI,BIN
SJMPTT1
BIN:
CLRSM2;清掉
WAIT1:
JBCRI,INTS
SJMPWAIT1
INTS:
JB7FH,CHANG
JB7EH,DAT
SJMPMAIN
CHANG:
MOVA,SBUF
MOV41H,A
MOV27H,A;记录长度初值为6
CLR7FH
SJMPBIN
DAT:
MOVA,SBUF
MOV@R0,A
INCR0
DJNZ41H,BIN
CLR7EH
MOV41H,27H
SJMPMAIN
DISPLAY:
JBP1.0,DISPL
ACALLCLEAN
DISPL:
MOVP2,#0FFH
MOVDPTR,#DAB
MOVA,@R0
MOVCA,@A+DPTR
MOVP0,A
INCR0
MOVA,26H
MOVP2,A
RLA
MOV26H,A
ACALLDELAY
DJNZ27H,OUT
MOVR0,#20H
MOV26H,#0FEH
MOV27H,41H
OUT:
RET
DELAY:
MOVR6,#10H
LOP1:
MOVR7,#10H
LOP2:
NOP
NOP
DJNZR7,LOP2
DJNZR6,LOP1
RET
CLEAN:
MOV42H,#6H
MOVR0,#20H
CLEA1:
MOV@R0,#00H
INCR0
DJNZ42H,CLEA1
MOVR0,#20H
MOV26H,#0FEH
MOV27H,41H
RET
DAB:
DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,00H
DB5EH,79H,71H,73H,3EH,31H,6EH,0FFH,00H
END
;通信子机2号
D1EQU20H
D3EQU30H
START:
MOVTMOD,#20H
MOVTH1,#0F4H
MOVTL1,#0F4H
SETBTR1
MOV41H,#6H
MOV27H,#6H
MAIN:
MOVSCON,#0F0H;SM2为1
MOV26H,#0FEH
MOVR0,#20H;存储首址
SETB7FH
SETB7EH
SETBREN
WAIT:
ACALLDISPLAY
JBCRI,READ
SJMPWAIT
READ:
MOVA,SBUF
CJNEA,#D1,LOP;检查机号是否对配
SJMPBINGIN
LOP:
CJNEA,#D3,EXIT;检查是否为广播
MOVR0,#20H;同样要赋值,下面一行和这一行是不同于1号子机的
SJMPBIN;##############是广播时不发送让1号机发
EXIT:
SETBSM2
SJMPWAIT
BINGIN:
MOVR0,#20H
MOVA,#11H
MOVSBUF,A;为了能正确接收导数据,因为在接收时不能及时读取RI(显示功能的时间延迟)
TT1:
JBCTI,BIN
SJMPTT1
BIN:
CLRSM2;清掉
WAIT1:
JBCRI,INTS
SJMPWAIT1
INTS:
JB7FH,CHANG
JB7EH,DAT
SJMPMAIN
CHANG:
MOVA,SBUF
MOV41H,A
MOV27H,A;记录长度初值为6
CLR7FH
SJMPBIN
DAT:
MOVA,SBUF
MOV@R0,A
INCR0
DJNZ41H,BIN
CLR7EH
MOV41H,27H
SJMPMAIN
DISPLAY:
JBP1.0,DISPL
ACALLCLEAN
DISPL:
MOVP2,#0FFH
MOVDPTR,#DAB
MOVA,@R0
MOVCA,@A+DPTR
MOVP0,A
INCR0
MOVA,26H
MOVP2,A
RLA
MOV26H,A
ACALLDELAY
DJNZ27H,OUT
MOVR0,#20H
MOV26H,#0FEH
MOV27H,41H
OUT:
RET
DELAY:
MOVR6,#10H
LOP1:
MOVR7,#10H
LOP2:
NOP
NOP
DJNZR7,LOP2
DJNZR6,LOP1
RET
CLEAN:
MOV42H,#6H
MOVR0,#20H
CLEA1:
MOV@R0,#00H
INCR0
DJNZ42H,CLEA1
MOVR0,#20H
MOV26H,#0FEH
MOV27H,41H
RET
DAB:
DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,00H
DB5EH,79H,71H,73H,3EH,31H,6EH,0FFH,00H
END
下面为参考资料:
《单片机多机通讯实验》程序清单
;多机通信主机程序
U33CLKEQU0FFF90H
ID1EQU10
ID2EQU20
ORG0000H
MOVSP,#60H
MOVTMOD,#20H;定时器1方式2定时
MOVTL1,#0F4H;计数初值
MOVTH1,#0F4H
SETBTR1
MOVSCON,#0D0H;方式3,允许接收,主机的SM2为0
;loadnumber#00H~#0FHinto30H~3FH单元中
MOVR7,#16
MOVR0,#30H
MOVA,#00H
LP:
MOV@R0,A
INCA
INCR0
DJNZR7,LP
MOV20H,#ID1
MOV21H,#ID2
MOVDPTR,#U33CLK
LP0:
MOVR1,#20H
SENDADD:
MOVA,@R1;send分机的编号ID
SETBTB8;TB8为1
MOVSBUF,A
WAIT:
JBCTI,CONT
SJMPWAIT
CONT:
MOVR7,#16
MOVR0,#30H
SENDDAT:
;starttosenddata
MOVA,@R0
CLRTB8;TB8为0
MOVSBUF,A
WAIT1:
JBCTI,RECEIVE
SJMPWAIT1
RECEIVE:
JBCRI,REC1
SJMPRECEIVE
REC1:
MOVA,SBUF
MOVX@DPTR,A;senddatatoLEDfordisplay
INCR0;R0pointnextdatatobesent
DJNZR7,SENDDAT
MOVA,#55H;SENDENDCODE
CLRTB8
MOVSBUF,A
WAIT2:
JBCTICONT2
SJMPWAIT2
CONT2:
INCR1
CJNER1,#22H,SENDADD;是否循环检查完所有分机
MOVR1,#20H
LP1:
LJMPLP0
END
;多机通信从机程序(编号为10)
CLK273EQU0FF90H
IDEQU10
ORG0000H
MOVSP,#60H
MOVTMOD,#20H;定时器1方式2定时
MOVTL1,#0F4H;计数初值
MOVTH1,#0F4H
SETBTR1
MOVSCON,#0F0H;方式3,允许接收,多机通信
RECEIVE:
;RECEIVEADDRESS
JBCRI,REC1
SJMPRECEIVE
REC1:
MOVA,SBUF
CJNEA,#ID,RECEIVE
CLRSM2
RECDAT:
;STARTTORECEIVEDATA
JBCRI,REC2
SJMPRECDAT
REC2:
MOVA,SBUF
CJNEA,#55H,CONT
SJMPEXIT
CONT:
MOVDPTR,#CLK273;送LED显示
MOVX@DPTR,A
SEND:
MOVSBUF,A;将接收到的数据再送至主机
WAIT:
JBCTI,CONT1
SJMPWAIT
CONT1:
SJMPRECDAT;RECEIVENEXTDATA
EXIT:
SETBSM2
SJMPRECEIVE;JUMPRECEIVEADDRESS
End;程序结束