在论坛上看到不少不错的延时程序,整理如下共同分享:
精确延时计算公式:
延时时间=[(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
;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微秒
;这样算下来应该是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
/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
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
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
用户377235 2012-6-12 19:13