Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

delay calculation help!

Status
Not open for further replies.

alexang

Newbie level 4
Joined
Oct 7, 2010
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,331
void main()
{
unsigned long delay_time=5000;
for(;;)
{
if(SW1==0)
{
out=1;
delay(1000000);
out=0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
this delay(1000000) is around 5 seconds
i m using 20MHz crystal, can i know how is this 5seconds calculation?
 

If the delay is only half what you expected it to be, either you have told the software the clock is 10MHz or the value you passed to the delay() routine is too large for it to handle.

Brian.
 

but it is work!! result show "out" is on for 5 seconds as wat i m expect.
i just wan to noe the calculation part...
 

It uses some kind of loop, for example in avr_gcc the _delay() uses this
avr-libc: <util/delay.h>: Convenience functions for busy-wait delay loops

click on _delay_loop_1() or _delay_loop_2() in the above link to see the explanation.

But usually the number you enter for the delay is us or ms and in your case its not so i assume like said in the previous post that you have the wrong clock setting in your project (i assume 4MHz).
if the setting was correct you would probably use delay(5000000); if the delay is in us.

Alex
 

i sure i m using 20Mhz crystal !!!

1 instruction = 4 clock cycle
time for 1 instruction=20 M/4 = 5 MHz

(unsigned long data) 5,000 x (my delay)1,000,000 = 5,000,000,000

5,000,000,000 / 5,000,000 cycles per second = 1000 seconds!

my calculation correct?
but result show is 5seconds???
 

(unsigned long data) 5,000 x (my delay)1,000,000 = 5,000,000,000

5,000,000,000 / 5,000,000 cycles per second = 1000 seconds!

void main()
{
unsigned long delay_time=5000;
for(;;)
{
if(SW1==0)
{
out=1;
delay(1000000);
out=0;
}

And where do you see that connection between the delay_time variable and the delay() function, your C code shows only a variable that is never used.
When we say that the crystal freq. is not set up correctly we don't mean your physical crystal but the clock define for the code.
We don't even know what microcontroller or application you are using, a simple look at the delay function will solve all questions.

Alex
 
Last edited:

alexan_e is right.
For some reason you are associating delay_time with the actual delay you are asking for. All you are actually doing is creating a new variable called 'delay_time' and assigning it a value of 5000, but you do not use that variable later!

In most compilers there is a 'delay(x)' function where x is a number of seconds, milliseconds or microseconds. Without telling us which compiler you are using we can't tell what the delay units are. Regardless of that, the 5 second delay you have does not match any of those units so one of two things can be wrong:
1. Your crystal is 20MHz but have you told the program that? If the program creates the delay in a software loop it needs to know how long each instruction takes to execute before it can calculate how to make the delay you want. Somewhere, either in your IDE or in the program you have to tell it you are using 20MHz as the clock source.
2. The value 1000000 you use for the delay may be out of range and not used correctly by the delay() routine. For example, if it only accepted unsigned ints as the delay parameter you would not be allowed to use a value greater than 65565.

I would question how you are detecting the delay. Assuming the missing '}' at the end of the program is there, 'out' is only low for a few cycles as the loop restarts.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top