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.

how to delay in C language?

Status
Not open for further replies.

Jackwang

Full Member level 2
Joined
Nov 12, 2004
Messages
134
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,298
Activity points
1,095
c language delay

i want to write code for implementing long time (100ms) delay in C51 with C language, can you give some good advice?
 

delay in c code

Creating a software loop to generate time delays in C depends on the code that your compiler reduces to. You could try something like
for (i=0;i<32;i++)
{
j=i;
}

and then look at the code that is produced. You can then analyze the instruction length and using your processor clock calculate the execution time. You can then tweak the values in the loop to give you the time that you want.

There is a description on how to do this using Excel in my book "Excel by Example : A Microsoft Excel Cookbook for Electronics Engineers"

However this will tie your processor up for the whole time of the delay (with the exception of interrupts which will extend the period) and the approach is genearlly frowned upon. You would do better to use the hardware timer, perhaps in combination with a software counter if the period of the timer cannot be made to extend to the 100mS.

-Aubrey Kagan
 

delay in c language

i think you could use asm commands within C, then use NOP...i think that would create a delay but its too small, try looping it so you could set your desired delay
 

c code delay

Use timer to generate interrupt for timebase u want to choose. eg. 10ms or 1ms
Then use variables as software timer. Initailise it properly (e.g. for delay of 100ms with 10ms time base, initialise it with 10), decrement it on every interrupt and upon being 0,ur delay is over and u can do ur further activity.
 

delay c code

use delay() function of "C". I think delay(1) is 1nSec delay but check for help and you will get the answer.
 

delay c language

If you use delay via 'for' loops do not forget to put volatile for counting variable - otherwise your code could be stripped by compiler optimization .
 

how to use delay in c

First of all, the question is how precise you delay should be?
If not then you can just use a for loop and just experience with trial and error what the loop number should be. Or you can calculate from the ASM code to which the C compiles to (opcode).
You may also use the delay function if your library supports it.

In case you need a very fix delay time, than the best is to use timer interrupt as suryakant already proposed. The only thing I would like to add, that you need a variable (semaphore) in the subrpogram where the delay is needed, and you check its value in a while loop. From the interrupt, when the time is passed, you change the value of this semaphore. Thus the precision is within 10-20 usec (time needed to enter to timer interrupt, decremenet the SW counter, check whether is zero, if yes change the semaphore and exit from interrupt + chack the semaphore in the program).

Belsugului
Belsugului
 

time delay in c language

Person sitting next to me says that it is much better to use assembly subcode for counting time.
#asm
time delay code
#endasm
 

time delay in c

void delay(unsigned int k)
{
unsigned int i;
for(i = 0; i <= k; i++)
}

You can modify k to have an achieve delay().
Then, you can call the delay() function by : delay(100); delay(1000);..... (k<~65.000)
 
delay function in c language

My friend always use assembly code in C to generate delay, the delay function i think works only if you included time.h if im correct. The NOP assembly command will delay one clock cycle, if im correct, or a number of clock cycles, so it depends on your oscillator the number of NOP's to be looped.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top