;*采用 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
LJMP PINT1
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
;ACALL DSP1
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
;**********************************************************汉字显示子程序************
DSP2: SETB CS2
CLR CS1
MOV X,#0BAH
MOV Y,#50H
MOV DPTR,#DDB
ACALL DISP_
RET
DSP3: SETB CS2
CLR CS1
MOV X,#0BAH
MOV Y,#58H
MOV DPTR,#DDB
ACALL DISP_
RET
DSP4: SETB CS2
CLR CS1
MOV X,#0BAH
MOV Y,#60H
MOV DPTR,#DDB
ACALL DISP_
RET
;************************8位字宽显示子程序: (CODE0待设)**********************
DISP_: MOV B,#10H ;要显示的数是几就用几乘以16,得到基址起始地址
MOV A,MUL_
MUL AB
MOV CODE0,A
MOV 39H,#02H
MOV COUNT,#08H
LP_: MOV COM,X ;设置页地址
LCALL INST
MOV COM,Y ;设置列地址
LCALL INST
W_: MOV A,CODE0
MOV C A,@A+DPTR
MOV DAT,A
LCALL DATA_
INC CODE0
DJNZ COUNT,W_
DJNZ 39H,LP1_
RET
LP1_: INC X
MOV COUNT,#08H
AJMP LP_
;***************************16位字宽显示子程序**********************
DISP: MOV 39H,#02H
MOV CODE0,#00H
MOV COUNT,#10H ;根据字宽及象素的不同而改变 DISP中的COUNT根据字高及象素改变CODE0
LP: MOV COM,X ;设置页地址
LCALL INST
MOV COM,Y ;设置列地址
LCALL INST
W: MOV A,CODE0
MOV C A,@A+DPTR
MOV DAT,A
LCALL DATA_
INC CODE0
DJNZ COUNT,W
DJNZ 39H,LP1
RET
LP1: INC X
MOV COUNT,#10H
AJMP LP
;***********************以下为字符显示子程序块*************************
SY: SETB CS1 ;开机
CLR CS2
MOV X,#0BAH
MOV Y,#40H
MOV DPTR,#TAB1 ;
CALL DISP
MOV X,#0BAH
MOV Y,#50H
MOV DPTR,#TAB2 ;
CALL DISP
MOV X,#0BAH
MOV Y,#60H
MOV DPTR,#TAB3 ;
CALL DISP
MOV X,#0BAH
MOV Y,#70H
MOV DPTR,#TAB4 ;
CALL DISP
CLR CS1
SETB CS2
MOV X,#0BAH
MOV Y,#40H
MOV DPTR,#TAB5 ;
CALL DISP
MOV X,#0BAH
MOV Y,#70H
MOV DPTR,#TAB6 ;
CALL DISP
RET
INST: LCALL BUSY ;写指令
SETB E
CLR D_I
CLR R_W
MOV DB0_DB7,COM
CLR E
RET
DATA_: LCALL BUSY ;写数据
SETB E
SETB D_I
CLR R_W
MOV DB0_DB7,DAT
CLR E
RET
BUSY: CLR D_I ;测忙
SETB R_W
BUSY1: MOV DB0_DB7,#0FFH
SETB E
MOV A,DB0_DB7
CLR E
JB ACC.7,BUSY1
RET
CLEAR: MOV R4,#00H ;清屏
CLEAR1: MOV A,R4
ORL A,#0B8H
MOV COM,A
ACALL INST
MOV COM,#40H
ACALL INST
MOV R3,#40H
CLEAR2: MOV DAT,#00H
LCALL DATA_
DJNZ R3,CLEAR2
INC R4
CJNE R4,#08H,CLEAR1
RET
INT: MOV COM,#0C0H ;初始化
ACALL INST
MOV COM,#3FH
ACALL INST
RET
DEL: MOV R7,#02H
DEL1: MOV R6,#0FFH
DEL2: MOV R5,#0FFH
DJNZ R5,$
DJNZ R6,DEL2
DJNZ R7,DEL1
RET
DDB:
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,010H,010H,0F8H,000H,000H,000H,000H,000H,020H,020H,03FH,020H,020H,000H,000H ; "1" 1
DB 000H,070H,008H,008H,008H,088H,070H,000H,000H,030H,028H,024H,022H,021H,030H,000H ; "2" 2
DB 000H,030H,008H,088H,088H,048H,030H,000H,000H,018H,020H,020H,020H,011H,00EH,000H ; "3" 3
DB 000H,000H,0C0H,020H,010H,0F8H,000H,000H,000H,007H,004H,024H,024H,03FH,024H,000H ; "4" 4
DB 000H,0F8H,008H,088H,088H,008H,008H,000H,000H,019H,021H,020H,020H,011H,00EH,000H ; "5" 5
DB 000H,0E0H,010H,088H,088H,018H,000H,000H,000H,00FH,011H,020H,020H,011H,00EH,000H ; "6" 6
DB 000H,038H,008H,008H,0C8H,038H,008H,000H,000H,000H,000H,03FH,000H,000H,000H,000H ; "7" 7
DB 000H,070H,088H,008H,008H,088H,070H,000H,000H,01CH,022H,021H,021H,022H,01CH,000H ; "8" 8
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,000H,031H,022H,022H,011H,00FH,000H ; "9" 9
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
DB 000H,0E0H,010H,008H,008H,010H,0E0H,000H,000H,00FH,010H,020H,020H,010H,00FH,000H ; "0" 0
TAB1:
DB 000H,03EH,022H,0E2H,022H,03EH,000H,0FEH,022H,022H,022H,022H,022H,0E2H,002H,000H ;
DB 040H,07FH,020H,03FH,011H,011H,000H,07FH,042H,042H,042H,042H,042H,043H,040H,000H ; "距" 0
TAB2:
DB 000H,004H,004H,0F4H,084H,0CCH,0ADH,096H,094H,0ACH,0CCH,084H,0F4H,006H,004H,000H ;
DB 000H,000H,0FEH,002H,01AH,016H,012H,013H,012H,016H,01AH,072H,082H,07EH,000H,000H ; "离" 1
TAB3:
DB 008H,008H,0E8H,0A8H,0A9H,0AEH,0EAH,008H,008H,0C8H,00CH,00BH,0EAH,008H,008H,000H ;
DB 000H,000H,07FH,004H,024H,044H,03FH,000H,000H,01FH,040H,080H,07FH,000H,000H,000H ; "前" 2
TAB4:
DB 008H,008H,008H,008H,008H,008H,0F9H,04AH,04CH,048H,048H,0C8H,008H,008H,008H,000H ;
DB 040H,040H,020H,010H,00CH,003H,000H,000H,020H,040H,040H,03FH,000H,000H,000H,000H ; "方" 3
TAB5:
DB 000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H ;
DB 000H,000H,036H,036H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H,000H ; ":" 0
TAB6:
DB 0C0H,030H,008H,008H,008H,008H,038H,000H,008H,0F8H,0F8H,000H,0F8H,0F8H,008H,000H ; "C" 1
DB 007H,018H,020H,020H,020H,010H,008H,000H,020H,03FH,000H,03FH,000H,03FH,020H,000H ; "M" 2
END
上一篇:键控门铃程序
下一篇:12864并口方式显示程序
推荐阅读最新更新时间:2024-03-16 15:52