I want to create delay functions in a separate c file. The code should be as perfect as possible without using internal timer. I read somewhere that 'Calling the routine takes about 22us' Though 22uS may be different for my cpu clock, if possible this factor should also be taken into account.
I am using 89S52 with 24Mhz crystal. I tried following code.
void usdelay(unsigned int us){ while (us--){ _nop_(); //0.5uS single-cycle instruction delay _nop_(); //0.5uS single-cycle instruction delay } } void msdelay(unsigned int ms){ unsigned long tm = 1000*ms; while (tm--){ _nop_(); //0.5uS single-cycle instruction delay _nop_(); //0.5uS single-cycle instruction delay } } void secdelay(unsigned int sec){ unsigned long tm = 1000*sec; while (tm--){ msdelay(1); } }
The problem is that the uS & ms delays are proper (may be - I have not measured them), but it takes very long to finish secdelay with 1 sec timing. Please HELP.
from the link: Addendum Specifically for a software delay function, you don't want any interrupts to mess-up your timing. You can achieve this by using the DISABLE directive in the original 'C' source file
THAT would, in most cases, be "playing with fire"
Erik
But then most sw delays (except the very, very short ones in ns to us range for settle/hold while bit-fiddling) can be described as "playing with fire". And when playing with settle/hold times, the absolute time value normally isn't important. What is important is to have at least x ns/us of delay to make sure the processor isn't too fast for the external electronics.
It is way better to make use of a free-running timer and poll the timer value in the while loop. Then the while loop will auto-adapt to losses from interrupts. And many different delays can be handled using the same timer.
It is also way better to see if theere are an peripherials with a baudrate feature that can be abused. Maybe sending SPI or UART communication out in the air, with each byte sent representing x microseconds. Some chips allows this without even mapping the output from the peripherial onto a physical processor pin.
Having longer sw-only delays means that the processor can do zero other operations while waiting. Any other thing (including servicing interrupts) represents a "leakage" of CPU time, making the delay longer than intended. So having a 1-second delay as a busy-loop that is strictly counting seconds means you have millions of processor instructions wasted while the procesor runs at maximum speed without getting anywhere. Besides being inelegant, it also wastes power. Extra bad if the device is battery-operated.