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;開啟發(fā)超聲波用定時器T1
OUT:RETI
;T1中斷,發(fā)超聲波用
INTT1:CPLVOUT
DJNZR4,RETIOUT
CLRTR1;超聲波發(fā)送完畢,關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
;
;****************************************************
;*兩字節(jié)無符號數乘法程序*
;****************************************************
;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
;
;****************************************************
;*四字節(jié)/兩字節(jié)無符號數除法程序*
;****************************************************
;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