;*采用 AT89C52 12MHz晶振 *
;測距范圍5CM-2M,堆棧在4FH以上,20H用于標(biāo)志
;顯示緩沖單元在40H-43H,使用內(nèi)存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 ;代碼倒計數(shù)單元
CODE0 EQU 26H ;表格起始地址,賦0值后與DPTR合成查表指針地址
MUL_ EQU 26H;顯示數(shù)字時,暫存單元(要顯示的數(shù)是幾就在里面存幾)
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為顯示數(shù)據(jù)存放單元(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 ;超聲波肪沖個數(shù)控制(為賦值的一半)
SETB PX1
SETB ET0
SETB EA
SETB TR0 ;開啟測距定時器
START1: LCALL DISPLAY
JNB 00H,$ ;收到反射信號時標(biāo)志位為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 ;啟動計數(shù)器T0,用以計算超聲來回時間
SETB TR1 ;開啟發(fā)超聲波用定時器T1
OUT: RETI
;T1中斷,發(fā)超聲波用
INTT1: CPL VOUT
DJNZ R4,RETIOUT
CLR TR1 ;超聲波發(fā)送完畢,關(guān)T1
CLR ET1 ;
MOV R4,#04H
SETB EX1 ;開啟接收回波中斷
RETIOUT: RETI
;外中斷0,收到回波時進(jìn)入
PINT1: CLR TR0 ;關(guān)計數(shù)器
CLR TR1
CLR ET1
CLR EA ;
CLR EX1 ;
MOV 44H,TL0 ;將計數(shù)值移入處理單元
MOV 45H,TH0 ;
SETB 00H ;接收成功標(biāo)志
RETI
;**************顯示程序*********
; 40H為最高位,43H為最低位,先掃描高位
DISPLAY: ; MOV CODE0,40H
;ACALLDSP1
MOV MUL_,41H
ACALL DSP2
MOV MUL_,42H
ACALL DSP3
MOV MUL_,43H
ACALL DSP4
RET
;*********距離計算程序 (=計數(shù)值*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
;***********兩字節(jié)無符號數(shù)乘法程序
; 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
;*********四字節(jié)/兩字節(jié)無符號數(shù)除法程序
;R7R6R5R4/R3R2=R7R6R5R4(商)...R1R0(余數(shù))
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
;**********************************************************漢字顯示子程序************