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.
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.
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
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.
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 .
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).
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.