I am not sure if it counts as a programming problem, or if it is something more general, but here it goes:
I need to delay execution of the program. delay(1) works, but the delay is too long. I can't use delayMicroseconds() as I am targetting ATtiny13 (but I have reproduced the problem on Arduino Uno). I tried to use just an empty loop:
void wait(unsigned int t)
{
while (t--);
}
and it didn't work at all, as if the code was ignored. After some digging I have found library function _delay_loop_2(), which technically does exactly the same thing - executes an empty loop - but it is written in assembly, and works:
void
_delay_loop_2(uint16_t __count)
{
__asm__ volatile (
"1: sbiw %0,1" "\n\t"
"brne 1b"
: "=w" (__count)
: "0" (__count)
);
}
Any idea why that's the case? Why my wait() function doesn't work? The only thing I can think of is that it is an effect of compiler optimization - it ignores the code that does nothing. Is that the case? And if so - do I guess correctly that there should exist some #pragmas to switch the optimization locally off? (Never used gcc, I am rather a Microsoft/Visual compilers guy).