Dear Forum,
I have implemented a delay function by this:
void delay_125us(const UINT32 n)
{ volatile UINT32 i=66; // volatile to prevent optimization, value ROUGHLY measured
i = i*n;
do i--;
while (i != 0);
}
I now encountered an occasional problem while running the device. Luckily once the problem came up while I had the debugger connected and I found the program stuck in the delay function with
n=8 and
i= 4294880763
Since n was 8, i should have been only 528, so must have been changed from outside.
The program uses interrupts which also partly run the delay function.
My first guess is that it must be a wrong pointer addressing or array overflow or something like that. Now a colleague brought up that i might get overwritten during interrupt execution because of the fact that it is declared volatile? I learned to improve the delay function by increment i and do i++ while <= max to depend on catching one zero abort condition (or use signed counter), but:
Is there any real error in using such delay function with volatile counter variable, when interrupts are executed in parallel? Or musst this missbehaviour come from wrong use of pointer/arrayoverflow?
Thanks in advance
Chrishen