delay subroutine
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..
Tags 8051 time delaytime delay calculationdelay using timersdelay using registers8051 timers
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
Powered by e107 Forum System