Mail Archives: djgpp/1999/02/22/07:40:57
Engard Ferenc writes:
> My program does it's timing with empty loop:
>
> void do_delay (unsigned long i)
> { while(i--) ; }
I don't think that is a very good way to do a timing delay. This is
incredibly prone to compiler optimisation, and will be affected by
things like the code alignment, processor cache, and if you inline
it within a larger function, the surrounding code will change how gcc
deals with it.
I wouldn't use a delay loop for anything at all critical, which it
sounds like your situation is. If you really insist upon using this
method, though, at the very least you should:
- write it in asm.
- put it a separate .s file, not inlined within C a source.
- use .align directives to make sure the routine cannot move around.
- only have a single copy, used for both calibration and actual delays.
If you do that, it will probably be pretty reliable, as long as you
can guarantee no external interference (eg. interrupts are disabled).
If this needs to be really accurate, though, it would be much better
to use a proper timing chip. The pentium has some hardware registers
specifically for this purpose, but I'm afraid I don't have any
references to hand on exactly how you would access them...
Shawn Hargreaves.
- Raw text -