Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

1 sec delay in embedded c

Status
Not open for further replies.

sujeethaa

Junior Member level 3
Joined
Nov 18, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
bangalore
Activity points
1,464
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

thanks in advance
 

jjeevan007

Full Member level 5
Joined
Apr 13, 2012
Messages
311
Helped
44
Reputation
88
Reaction score
43
Trophy points
1,308
Location
bangalore
Activity points
2,781
using one of the timer produce delay of 50m seconds and repeat that for 20 times
 

milind.a.kulkarni

Advanced Member level 3
Joined
Oct 6, 2011
Messages
923
Helped
214
Reputation
428
Reaction score
208
Trophy points
1,333
Location
Bangalore
Activity points
7,438
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
Joined
Nov 18, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
bangalore
Activity points
1,464
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

thanks in advance

-----
sujeethaa
 

milind.a.kulkarni

Advanced Member level 3
Joined
Oct 6, 2011
Messages
923
Helped
214
Reputation
428
Reaction score
208
Trophy points
1,333
Location
Bangalore
Activity points
7,438
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
Joined
Nov 18, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
bangalore
Activity points
1,464
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!!!
 

milind.a.kulkarni

Advanced Member level 3
Joined
Oct 6, 2011
Messages
923
Helped
214
Reputation
428
Reaction score
208
Trophy points
1,333
Location
Bangalore
Activity points
7,438
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
 

vijayjadhav.595

Member level 1
Joined
Sep 22, 2012
Messages
40
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Mumbai,India
Activity points
1,537
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
 

ppdr123

Member level 3
Joined
Jul 24, 2010
Messages
61
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,288
Location
Bhaarat
Activity points
1,615
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.
 

sujeethaa

Junior Member level 3
Joined
Nov 18, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
bangalore
Activity points
1,464
thank u all
!!!


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

thanks for your support
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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
Joined
Nov 18, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
bangalore
Activity points
1,464
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top