# 1 sec delay in embedded c

Status
Not open for further replies.

#### sujeethaa

##### Junior Member level 3
hi all

could any one pls tell me the coding to calculate 1 sec delay in embedded c for 8051 controller without using timer!!!
i.e using for loop and pls explain it

#### jjeevan007

##### Full Member level 5
using one of the timer produce delay of 50m seconds and repeat that for 20 times

#### milind.a.kulkarni

the idea is use interrupt timer for fixed period and then find out integral multiple of 1 sec by fixed time.... use loop to count for multiple factor will solve your problem

#### sujeethaa

##### Junior Member level 3
the idea is use interrupt timer for fixed period and then find out integral multiple of 1 sec by fixed time.... use loop to count for multiple factor will solve your problem
for(i=0;i<1275;i++);

whether this will give 1ms delay!!!

so that i can repeat it for 1000 times

-----
sujeethaa

#### milind.a.kulkarni

if you are using only this code in the polling mode and use proper constant and your crystal is 12 Mhz....but there is a catch as in case of micro-controller like 8051 the instruction clock is 1/12 of clock frequency .....mostly 1 Mhz in case of 12 Mhz.... so better way to exact delay better to use interrupt timer....

Good Luck,

Milind

#### sujeethaa

##### Junior Member level 3
if you are using only this code in the polling mode and use proper constant and your crystal is 12 Mhz....but there is a catch as in case of micro-controller like 8051 the instruction clock is 1/12 of clock frequency .....mostly 1 Mhz in case of 12 Mhz.... so better way to exact delay better to use interrupt timer....

Good Luck,

Milind
I m using 8051 with 11.0859MHz frequency!!!

#### jjeevan007

##### Full Member level 5
ya u can do that........

#### milind.a.kulkarni

as jjeevan said it is possible but in polling mode please keep the loop for 1000 in case of 11.92 Mhz crystal to get 1ms delay i.e. for(i=0;i<1000;i++);

Good Luck

sujeethaa

### sujeethaa

points: 2

##### Member level 1
hi...
with 11.085MHz your instruction clock would be 1.085microsecond long. Take this into consideration and use a loop for generating the 1 sec delay. u will also have to consider the time taken by the increment/decrement instructions while setting the count for the loop.
hope that helps...

cheers...,
Vijay

sujeethaa

### sujeethaa

points: 2

#### ppdr123

##### Member level 3
hi,
the for loop shown above will work but during conversion into assembly, the code will split into different ASM instructions having different execution/machine cycles. so the time dalay may not be exact.
if u are using keil, u can view the assembly code of FOR loop. from that u can calculate the time taken to execute one complete FOR loop. then u can multiply that delay(in for loop) to get 1 milisecond.with this delay will be more accurate. but the best way is to use software interrupt OR timer.
guys, correct me if something is wrong.

##### Member level 1
hi,
thats right ppdr123.

#### sujeethaa

##### Junior Member level 3
thank u all
!!!

I will try in both the methods and get u back!!

#### Tahmid

Use your compiler to measure the time the loop takes. That way, you can accurately measure the time your delay routine takes, and adjust it as required.

Also, check your compiler documentation/manual. It should provide library functions for generating delays. eg in mikroC, you have the delay_ms function. So, to generate a 1s delay, all you'd need would be:
Code:
delay_ms(1000);
Hope this helps.
Tahmid.

#### sujeethaa

##### Junior Member level 3
Use your compiler to measure the time the loop takes. That way, you can accurately measure the time your delay routine takes, and adjust it as required.

Also, check your compiler documentation/manual. It should provide library functions for generating delays. eg in mikroC, you have the delay_ms function. So, to generate a 1s delay, all you'd need would be:
Code:
delay_ms(1000);
Hope this helps.
Tahmid.
hi Tahmid

i m using Keil c compiler .. it dnt have any such library functions

Status
Not open for further replies.