1) You forgot the MOV instructions, once in a while, in your calculation!
2) Read datasheet first instead of assuming things!
3) How I do delays?
Use T0 as a 10ms auto reload timer
Declare 1 RAM byte "Delay"
In the ISR of T0 decrement "Delay" when it's > 0
In your main code check "Delay", as long as it's > 0 your delay is still on
When you need a delay of let say 1870ms load 187 into "Delay"
That way you can make any delay in 10ms increment
Note that you still can use T0 for other 10ms timed things.
u should read about timers in mode 2 that gona help u to understand the auto reload timer .u will find th & tl Reg which contain the number of delay the tf will be setted on. u have to read to understand it correctly.
Timer 0, also called T0, is a free running counter. It counts clock pulses. You can program it for a fixed interval by knowing how many clock pulses it takes to equal that interval. Auto-Reload simply means that when Timer 0 reaches it's terminal count(zero in the case of T0), it will automatically be loaded with the correct value to begin a new timing interval. It will also set an interrupt flag if you wish to do additional processing when the counter overflows.
ISR_T0: mov th0,#High Time_0 ;Reload timer
mov tl0,#Low Time_0
;
;Do all you other timed stuff here
;
LBL_Delay: mov a,Delay ;Delay X * 10 ms
setb c ;X managed by main prgm
subb a,#0
jc LBL_Delay_End
dec Delay ;Delay = Delay - 1 when > 0
LBL_Delay_End:
i suggest use this one better to understand and easy
total delay time is 0.1024 second because 256 X 200 X 2us =0.1024 sec.
below is a subroutine program :-
Code:
DELAY : MOV R0,#00 ; load R0 with # 00 = 256 is maximum number
MOV R1,#200 ;load R1 with 200
LOOP : DJNZ R0,LOOP ; loop it self R0 256 times
DJNZ R1,LOOP ;loop R0 and R1 it self R0=256 , R1=200 ,200X256=51200 times
RET
if u want longer delay time just ADD R2 total delay time 256 X 256 X 200 X 2us =26.2144 second
Code:
DELAY : MOV R0,#00 ; load R0 with # 00 = 256 is maximum number
MOV R1,#00 ;load R1 with 256
MOV R2,#200 ;LOAD R2 WITH 200
LOOP : DJNZ R0,LOOP ; loop it self R0 256 times
DJNZ R1,LOOP ;loop R0 and R1 it self R0=256 , R1=256 ,256X256=65536 times
DJNZ R2,LOOP; 256X256X200 = 13107200 TIMES
RET
change r2 value if
r2 = #152 , total delay time is 152 X 256 X 256 X 2us =19.922944 sec =20 sec .
r2 = # 77 = 10.92544 second .
just take a calculator to calculate the time u want
formula as below:
Code:
r2 X 256 x 256 X 2 us = total delay time .
just replace r2 with a value not more then 255 , and if u want 256 put 0
My previous post on your code was a little raw, maybe a little crude so that you will keep in mind to never, never use time delay's in such way in the future since you hang the µC.
So, to fool around with some led's it's ok but now try the same functionality with the Timer0 from the µC, that's the way to go.
i appreciate ur comment thanks . last question i still not understand why it will hang lecture tell that this code is use for loop it self for delay , or maybe i haven't graduate i don't understand i take diploma.i study the data-sheet atmel 89s52 have build in a 2 timer function is that u use that time as delay ? and why my code hang uc ?
it does nothing else.
No check of inputs, no control of outputs, no control of LCD, ... ... ... nothing except decrementing some registers.
If your project has some push buttons, the µC will never detect that the user pressed a button during your delay code.
When later on you will use interrupts, be aware that this timing will fail since an ISR (interrupt service routine) can interrupt your delay.
So that's why I said that you "hang" your µC during tour delay.