AT89C52超声波测距器
扫描二维码
随时随地手机看文章
AT89C52超声波测距器
;测距范围7CM-11M,堆栈在4FH以上,20H用于标志
;显示缓冲单元在40H-43H,使用内存44H、45H、46H用于计算距离
;
VOUTEQUP1.0;红外脉冲输出端口
;
;********************************************
;*中断入口程序*
;********************************************
;
ORG0000H
LJMPSTART
ORG0003H
LJMPPINT0
ORG000BH
LJMPINTT0
ORG0013H
RETI
ORG001BH
LJMPINTT1
ORG0023H
RETI
ORG002BH
RETI
;
;********************************************
;*主程序*
;********************************************
;
START:MOVSP,#4FH
MOVR0,#40H;40H-43H为显示数据存放单元(40H为最高位)
MOVR7,#0BH
CLEARDISP:MOV@R0,#00H
INCR0
DJNZR7,CLEARDISP
MOV20H,#00H
MOVTMOD,#21H;T1为8位自动重装模式,T0为16位定时器
MOVTH0,#00H;65毫秒初值
MOVTL0,#00H
MOVTH1,#0F2H;40KHz初值
MOVTL1,#0F2H
MOVP0,#0FFH
MOVP1,#0FFH
MOVP2,#0FFH
MOVP3,#0FFH
MOVR4,#04H;超声波肪冲个数控制(为赋值的一半)
SETBPX0
SETBET0
SETBEA
SETBTR0;开启测距定时器
start1:LCALLDISPLAY
JNB00H,START1;收到反射信号时标志位为1
CLREA
LCALLWORK;计算距离子程序
SETBEA
CLR00H
SETBTR0;重新开启测距定时器
MOVR2,#64H;测量间隔控制(约4*100=400MS)
LOOP:LCALLDISPLAY
DJNZR2,LOOP
SJMPStart1
;
;****************************************************
;*中断程序**
;****************************************************
;T0中断,65毫秒中断一次
INTT0:CLREA
CLRTR0
MOVTH0,#00H
MOVTL0,#00H
SETBET1
SETBEA
SETBTR0;启动计数器T0,用以计算超声来回时间
SETBTR1;开启发超声波用定时器T1
OUT:RETI
;T1中断,发超声波用
INTT1:CPLVOUT
DJNZR4,RETIOUT
CLRTR1;超声波发送完毕,关T1
CLRET1;
MOVR4,#04H
SETBEX0;开启接收回波中断
RETIOUT:RETI
;外中断0,收到回波时进入
PINT0:CLRTR0;关计数器
CLRTR1
CLRET1
CLREA;
CLREX0;
MOV44H,TL0;将计数值移入处理单元
MOV45H,TH0;
SETB00H;接收成功标志
RETI
;
;****************************************************
;*显示程序*
;****************************************************
;40H为最高位,43H为最低位,先扫描高位
DISPLAY:MOVR1,#40H;G
MOVR5,#0F7H;G
PLAY:MOVA,R5
MOVP0,#0FFH
MOVP2,A
MOVA,@R1
MOVDPTR,#TAB
MOVCA,@A+DPTR
MOVP0,A
LCALLDL1MS
INCR1
MOVA,R5
JNBACC.0,ENDOUT;G
RRA
MOVR5,A
AJMPPLAY
ENDOUT:MOVP2,#0FFH
MOVP0,#0FFH
RET
;
TAB:DB0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0FFH,88H,0BFH
;共阳段码表"0""1""2""3""4""5""6""7""8""9""不亮""A""-"
;
;****************************************************
;*延时程序*
;****************************************************
;
DL1MS:MOVR6,#14H
DL1:MOVR7,#19H
DL2:DJNZR7,DL2
DJNZR6,DL1
RET
;
;****************************************************
;*距离计算程序(=计数值*17/1000cm)*
;****************************************************
;
work:PUSHACC
PUSHPSW
PUSHB
MOVPSW,#18h
MOVR3,45H
MOVR2,44H
MOVR1,#00D
MOVR0,#17D
LCALLMUL2BY2
MOVR3,#03H
MOVR2,#0E8H
LCALLDIV4BY2
LCALLDIV4BY2
MOV40H,R4
MOVA,40H
JNZJJ0
MOV40H,#0AH;最高位为零,不点亮
JJ0:MOVA,R0
MOVR4,A
MOVA,R1
MOVR5,A
MOVR3,#00D
MOVR2,#100D
LCALLDIV4BY2
MOV41H,R4
MOVA,41H
JNZJJ1
MOVA,40H;次高位为0,先看最高位是否为不亮
SUBBA,#0AH
JNZJJ1
MOV41H,#0AH;最高位不亮,次高位也不亮
JJ1:MOVA,R0
MOVR4,A
MOVA,R1
MOVR5,A
MOVR3,#00D
MOVR2,#10D
LCALLDIV4BY2
MOV42H,R4
MOVA,42H
JNZJJ2
MOVA,41H;次次高位为0,先看次高位是否为不亮
SUBBA,#0AH
JNZJJ2
MOV42H,#0AH;次高位不亮,次次高位也不亮
JJ2:MOV43H,R0
POPB
POPPSW
POPACC
RET
;
;****************************************************
;*两字节无符号数乘法程序*
;****************************************************
;R7R6R5R4<=R3R2*R1R0
;
MUL2BY2:CLRA
MOVR7,A
MOVR6,A
MOVR5,A
MOVR4,A
MOV46H,#10H
MULLOOP1:CLRC
MOVA,R4
RLCA
MOVR4,A
MOVA,R5
RLCA
MOVR5,A
MOVA,R6
RLCA
MOVR6,A
MOVA,R7
RLCA
MOVR7,A
MOVA,R0
RLCA
MOVR0,A
MOVA,R1
RLCA
MOVR1,A
JNCMULLOOP2
MOVA,R4
ADDA,R2
MOVR4,A
MOVA,R5
ADDCA,R3
MOVR5,A
MOVA,R6
ADDCA,#00H
MOVR6,A
MOVA,R7
ADDCA,#00H
MOVR7,A
MULLOOP2:DJNZ46H,MULLOOP1
RET
;
;****************************************************
;*四字节/两字节无符号数除法程序*
;****************************************************
;R7R6R5R4/R3R2=R7R6R5R4(商)...R1R0(余数)
;
DIV4BY2:MOV46H,#20H
MOVR0,#00H
MOVR1,#00H
DIVLOOP1:MOVA,R4
RLCA
MOVR4,A
MOVA,R5
RLCA
MOVR5,A
MOVA,R6
RLCA
MOVR6,A
MOVA,R7
RLCA
MOVR7,A
MOVA,R0
RLCA
MOVR0,A
MOVA,R1
RLCA
MOVR1,A
CLRC
MOVA,R0
SUBBA,R2
MOVB,A
MOVA,R1
SUBBA,R3
JCDIVLOOP2
MOVR0,B
MOVR1,A
DIVLOOP2:CPLC
DJNZ46H,DIVLOOP1
MOVA,R4
RLCA
MOVR4,A
MOVA,R5
RLCA
MOVR5,A
MOVA,R6
RLCA
MOVR6,A
MOVA,R7
RLCA
MOVR7,A
RET
;
END