在论坛上看到不少不错的延时程序,整理如下共同分享:
精确延时计算公式:
延时时间=[(2*第一层循环+3)*第二层循环+3]*第三层循环+5
;延时5秒左右
DELAY5S:PUSH 04H
PUSH 05H
PUSH 06H
MOV R4,#50
DELAY5S_0:MOV R5,#200
DELAY5S_1:MOV R6,#245
DJNZ R6,$
DJNZ R5,DELAY5S_1
DJNZ R4,DELAY5S_0
POP 06H
POP 05H
POP 04H
RET
;513微秒延时程序
DELAY: MOV R2,#0FEH
DELAY1: DJNZ R2,DELAY1
RET
;10毫秒延时程序
DL10MS: MOV R3,#14H
DL10MS1:LCALL DELAY
DJNZ R3,DL10MS1
RET
;0.1s延时程序12mhz
DELAY: MOV R6,#250
DL1: MOV R7,#200
DL2: DJNZ R6,DL2
DJNZ R7,DL1
RET
;延时1046549微秒(12mhz)
;具体的计算公式是:
;((((r7*2+1)+2)*r6+1)+2)*r5+1+4 = ((r7*2+3)*r6+3)
*r5+5
DEL : MOV R5,#08H
DEL1: MOV R6,#0FFH
DEL2: MOV R7,#0FFH
DJNZ R7,$
DJNZ R6,DEL2
DJNZ R5,DEL1
RET
;1秒延时子程序是以12MHz晶振
Delay1S:mov r1,#50
del0: mov r2,#91
del1: mov r3,#100
djnz r3,$
djnz r2,del1
djnz r1,del0
Ret
;1秒延时子程序是以12MHz晶振为例算指令周期耗时
KK: MOV R5,#10 ;1指令周期1
K1: MOV R6,#0FFH ;1指令周期10
K2: MOV R7,#80H ;1指令周期256*10=2560
K3: NOP ;1指令周期
128*256*10=327680
DJNZ R7,K3 ;2指令周期
2*128*256*10=655360
DJNZ R6,K2 ;2指令周期
2*256*10=5120
DJNZ R5,K1 ;2指令周期2*10=20
RET
;2指令周期21+10+2560+327680+655360+5120+20+2=990753
;约等于1秒1秒=1000000微秒
;这个算下来也只有0.998抄
T_0: MOV R7,#10;
D1: MOV R6,#200;
D2: MOV R5,#248;
DJNZ R5,$
DJNZ R6,D2;
DJNZ R7,D1;
RET
;这样算下来应该是1.000011秒
T_0: MOV R7,#10;
D1: MOV R6,#200;
D2: NOP
MOV R5,#248;
DJNZ R5,$
DJNZ R6,D2;
DJNZ R7,D1;
RET
DELAY_2S: ;10MS(11.0592mhz)
MOV R3,#200
JMP DELAY10MS
DELAY_100MS: ;100MS(11.0592mhz)
MOV R3,#10
JMP DELAY10MS
DELAY_10MS:
MOV R3,#1
DELAY10MS: ;去抖动10MS
(11.0592mhz)
MOV R4,#20
DELAY10MSA:
MOV R5,#247
DJNZ R5,$
DJNZ R4,DELAY10MSA
DJNZ R3,DELAY10MS
RET
DELAY_500MS: ;500500MS
MOV R2,#208
JMP DELAY_MS
DELAY_175MS: ;175MS
MOV R2,#73
JMP DELAY_MS
delaY_120MS: ;120MS
MOV R2,#50
JMP DELAY_MS
delay_60ms: ;60ms
MOV R2,#25
JMP DELAY_MS
delay_30ms: ;30ms
MOV R2,#12
JMP DELAY_MS
DELAY_5MS: ;5MS
MOV R2,#2
;===================================
DELAY_MS:
CALL DELAY2400
DJNZ R2,DELAY_MS
RET
;===================================
DELAY2400: ;10x244+4=2447
/1.024=2390
MOV R0,#244 ;1
DELAY24001:
MUL AB ;4
MUL AB ;4
DJNZ R0,DELAY24001 ;2
RET
DELAY: ;延时子程序(1秒)
MOV R0,#0AH
DELAY1: MOV R1,#00H
DELAY2: MOV R2,#0B2H
DJNZ R2,$
DJNZ R1,DELAY2
DJNZ R0,DELAY1
RET
MOV R2,#10 ;延时1秒
LCALL DELAY
MOV R2,#50 ;延时5秒
LCALL DELAY
DELAY: ;延时子程序
PUSH R2
PUSH R1
PUSH R0
DELAY1: MOV R1,#00H
DELAY2: MOV R0,#0B2H
DJNZ R0,$
DJNZ R1,DELAY2 ;延时 100 mS
DJNZ R2,DELAY1
POP R0
POP R1
POP R2
RET
1:DEL: MOV R7, #200
DEL1: MOV R6, #123
NOP
DEL2: DJNZ R6, DEL2
DJNZ R7, DEL1
RET
是50.001ms 算法是:
0.001ms+200*0.001ms+200*0.001ms+200*123*0.002ms+200*0.002ms
;(123*2+4)*200+1
2: DEL: MOV R7, #200
DEL1: MOV R6, #123
DEL2:NOP
DJNZ R6,DEL2
DJNZ R7,DEL1
RET
D500MS:
PUSH PSW
SETB RS0
MOV R7,#200
D51: MOV R6,#250
D52: NOP
NOP
NOP
NOP
DJNZ R6,D52
DJNZ R7,D51
POP PSW
RET
DELAY: ;延时1毫秒
PUSH PSW
SETB RS0
MOV R7,#50
D1: MOV R6,#10
D2: DJNZ R6,$
DJNZ R7,D1
POP PSW
RET
ORG 0
LJMP MAIN
ORG 000BH
LJMP CTC0
MAIN: MOV SP, #50H
CLR EA
MOV TMOD, #01H
MOV TH0,#3CH
MOV TL0,#0B0H
MOV R4, #10
SETB ET0
SETB EA
SETB TR0
SJMP $ ;
CTC0: MOV TH0, #3CH
MOV TL0, #0B0H
DJNZ R4, LP
CPL P1.0
MOV R4, #10
LP: RETI
END