Discussion in "8051 Discussion Forum" started by    cenadius    Oct 5, 2007.
Fri Oct 05 2007, 03:14 am
ajay,give me the example on how to write delay assembly code by using both method,timer and register,thanks
Fri Oct 05 2007, 11:47 am
In timers... there are few things to take care of.. first.. amount of time you want delay for, second which timer mode is suitable for that delay.
Timer increment a count every single machine cycle. so if you are using a clock of say.. 12Mhz then.. no. of times timer count is incremented is:
12000000/12 = 1000000
i.e. timer will be incremented 1,000,000 times in a sec.
Lets say you want a delay of 10mS (0.01S)
and you have to calculate the count which is to be loaded in Timer registers (THx and TLx) to find the count to be loaded.. simply multiply the required delay with the count per sec..
i.e. 0.01 x 1,000,000 = 10,000
so we need to count from 0 to 10,000. Maximum count for 16 bit timer is 65536. So if we load 65536-10000 = 55536 in timer registers it till automatically count 10000 times..
So final value is 55536 (DF80H)
so THx = DFH and TLx = 80H
this will give you the required delay...

in case of registers.. you need to add all the time delay required for an instruction to execute and then multiply it by the loop count.. that will give you the time delay..
e.g. the commonly used instructions in register delay are.. djnz Rx,label (2 machine cycles)
loading value using MOV instruction also need 2 machine cycles. RET instuction and Acall instuction both need 2 machine cycles.
Lets say you want a delay of 100uS then.. you are running a clock of 12Mhz, so 1 machine cycle will be of 1uS. you can divide this according to the instructions as...
100 - 2(RET) - 2(lcall) - 2 (load) = 94uS left
94/2 (for DJNZ) = 47 (the count)

so required loop for 100uS delay is..

```delay100uS:
mov R7,#47
wait:
djnz r7,wait
ret```
youth69shaikmajoka like this.
Tags 8051 time delaytime delay calculationdelay using timersdelay using registers8051 timers
Sat Oct 06 2007, 10:26 am
ok,thanks,i need this in my exam,thanks for ur explaination :-)
Fri Apr 15 2011, 08:59 pm
wow ajay! Nice explaination <3
Sat Apr 16 2011, 02:20 pm
you can also use this simple program to help you in delay with assembly code from here
Tue Apr 19 2011, 01:50 am
@MAAM this program is also available here on website

### Recent

sssrsait
Sun Feb 05 2023, 04:57 am
Richardfap
Sat Feb 04 2023, 01:56 pm
klining_Lob
Sat Feb 04 2023, 11:52 am
Stevenplaix
Sat Feb 04 2023, 02:28 am
ufa16zbnow
Sat Feb 04 2023, 02:14 am
NelsonHeard
Fri Feb 03 2023, 07:52 pm
dracermen
Thu Feb 02 2023, 10:32 pm
RowenaSteve
Thu Feb 02 2023, 12:58 am