超声波测离程序
扫描二维码
随时随地手机看文章
;*采用 AT89C52 12MHz晶振 *
;测距范围5CM-2M,堆栈在4FH以上,20H用于标志
;显示缓冲单元在40H-43H,使用内存44H、45H、46H用于计算距离
CS1 BIT P2.3
CS2 BIT P2.4
D_I BIT P3.6 ;4
R_W BIT P2.5 ;5
E BIT P2.6 ;6
DB0_DB7 EQU P0
X EQU 22H;页地址
Y EQU 23H;列地址
COM EQU 24H
DAT EQU 24H
COUNT EQU 25H ;代码倒计数单元
CODE0 EQU 26H ;表格起始地址,赋0值后与DPTR合成查表指针地址
MUL_ EQU 26H;显示数字时,暂存单元(要显示的数是几就在里面存几)
VOUT EQU P3.5 ; 红外脉冲输出端口
ORG 0000H
AJMP START
ORG 0013H
LJMPPINT1
ORG 000BH
LJMP INTT0
ORG 001BH
LJMP INTT1
ORG 0030H
START: MOV SP,#4FH
ACALL INT
SETB CS1
SETB CS2
ACALL CLEAR
ACALL SY
MOV R0,#40H ;40H-43H为显示数据存放单元(40H为最高位)
MOV R7,#0BH
CLEARDISP: MOV @R0,#00H
INC R0
DJNZ R7,CLEARDISP
MOV 20H,#00H
MOV TMOD,#21H ;T1为8位自动重装模式,T0为16位定时器
MOV TH0,#00H ;65毫秒初值
MOV TL0,#00H
MOV TH1,#0F2H ;40KHZ初值
MOV TL1,#0F2H
MOV P0,#0FFH
MOV P1,#0FFH
MOV P2,#0FFH
MOV P3,#0FFH
MOV R4,#04H ;超声波肪冲个数控制(为赋值的一半)
SETB PX1
SETB ET0
SETB EA
SETB TR0 ;开启测距定时器
START1: LCALL DISPLAY
JNB 00H,$ ;收到反射信号时标志位为1
CLR EA
LCALL WORK ;计算距离子程序
SETB EA
CLR 00H
SETB TR0 ;重新开启测距定时器
MOV R2,#0FFH ;测量间隔控制(约4*100=400MS)
LCALL DISPLAY
DJNZ R2,$
SJMP START1
;**********中断程序*
;T0中断,65毫秒中断一次
INTT0: CLR EA
CLR TR0
MOV TH0,#00H
MOV TL0,#00H
SETB ET1
SETB EA
SETB TR0 ;启动计数器T0,用以计算超声来回时间
SETB TR1 ;开启发超声波用定时器T1
OUT: RETI
;T1中断,发超声波用
INTT1: CPL VOUT
DJNZ R4,RETIOUT
CLR TR1 ;超声波发送完毕,关T1
CLR ET1 ;
MOV R4,#04H
SETB EX1 ;开启接收回波中断
RETIOUT: RETI
;外中断0,收到回波时进入
PINT1: CLR TR0 ;关计数器
CLR TR1
CLR ET1
CLR EA ;
CLR EX1 ;
MOV 44H,TL0 ;将计数值移入处理单元
MOV 45H,TH0 ;
SETB 00H ;接收成功标志
RETI
;**************显示程序*********
; 40H为最高位,43H为最低位,先扫描高位
DISPLAY: ; MOV CODE0,40H
;ACALLDSP1
MOV MUL_,41H
ACALL DSP2
MOV MUL_,42H
ACALL DSP3
MOV MUL_,43H
ACALL DSP4
RET
;*********距离计算程序 (=计数值*17/1000CM)
WORK: PUSH ACC
PUSH PSW
PUSH B
MOV PSW, #18H
MOV R3, 45H
MOV R2, 44H
MOV R1, #00D
MOV R0, #17D
LCALL MUL2BY2
MOV R3, #03H
MOV R2, #0E8H
LCALL DIV4BY2
LCALL DIV4BY2
MOV 40H, R4
MOV A,40H
JNZ JJ0
MOV 40H,#0AH ;最高位为零,不点亮
JJ0: MOV A, R0
MOV R4, A
MOV A, R1
MOV R5, A
MOV R3, #00D
MOV R2, #100D
LCALL DIV4BY2
MOV 41H, R4
MOV A,41H
JNZ JJ1
MOV A,40H ;次高位为0,先看最高位是否为不亮
SUBB A,#0AH
JNZ JJ1
MOV 41H,#0AH ;最高位不亮,次高位也不亮
JJ1: MOV A, R0
MOV R4, A
MOV A, R1
MOV R5, A
MOV R3, #00D
MOV R2, #10D
LCALL DIV4BY2
MOV 42H, R4
MOV A,42H
JNZ JJ2
MOV A,41H ;次次高位为0,先看次高位是否为不亮
SUBB A,#0AH
JNZ JJ2
MOV 42H,#0AH ;次高位不亮,次次高位也不亮
JJ2: MOV 43H, R0
POP B
POP PSW
POP ACC
RET
;***********两字节无符号数乘法程序
; R7R6R5R4 <= R3R2 * R1R0
MUL2BY2: CLR A
MOV R7, A
MOV R6, A
MOV R5, A
MOV R4, A
MOV 46H, #10H
MULLOOP1: CLR C
MOV A, R4
RLC A
MOV R4, A
MOV A, R5
RLC A
MOV R5, A
MOV A, R6
RLC A
MOV R6, A
MOV A, R7
RLC A
MOV R7, A
MOV A, R0
RLC A
MOV R0, A
MOV A, R1
RLC A
MOV R1, A
JNC MULLOOP2
MOV A, R4
ADD A, R2
MOV R4, A
MOV A, R5
ADDC A, R3
MOV R5, A
MOV A, R6
ADDC A, #00H
MOV R6, A
MOV A, R7
ADDC A, #00H
MOV R7, A
MULLOOP2: DJNZ 46H, MULLOOP1
RET
;*********四字节/两字节无符号数除法程序
;R7R6R5R4/R3R2=R7R6R5R4(商)...R1R0(余数)
DIV4BY2: MOV 46H, #20H
MOV R0, #00H
MOV R1, #00H
DIVLOOP1: MOV A, R4
RLC A
MOV R4, A
MOV A, R5
RLC A
MOV R5, A
MOV A, R6
RLC A
MOV R6, A
MOV A, R7
RLC A
MOV R7, A
MOV A, R0
RLC A
MOV R0, A
MOV A, R1
RLC A
MOV R1, A
CLR C
MOV A, R0
SUBB A, R2
MOV B, A
MOV A, R1
SUBB A, R3
JC DIVLOOP2
MOV R0, B
MOV R1, A
DIVLOOP2: CPL C
DJNZ 46H, DIVLOOP1
MOV A, R4
RLC A
MOV R4, A
MOV A, R5
RLC A
MOV R5, A
MOV A, R6
RLC A
MOV R6, A
MOV A, R7
RLC A
MOV R7, A
RET
;**********************************************************汉字显示子程序************