I was programming an ATtiny13A when I accidentally noticed that when I change the delay time in _delay_us() my code size changes. Here's the sample code:
#include <util/delay.h>
int main(void)
{
while (1)
{
_delay_us(500);
}
}
And here are the different code sizes I get when changing the delay value:
_delay_us(500): 52 bytes
_delay_us(499): 50 bytes
_delay_us(498): 48 bytes
_delay_us(497): 52 bytes
_delay_us(496): 50 bytes
_delay_us(495): 52 bytes
_delay_us(494): 50 bytes
_delay_us(493): 48 bytes
_delay_us(492): 52 bytes
_delay_us(491): 50 bytes
_delay_us(490): 52 bytes
I have a lot of _delay_us() in my code with different delay times. Now it doesn't matter at all if they are a few micro seconds longer or shorter, but it does matter if I can get each one 2-4 bytes smaller.
So I was wondering if someone can tell me what exactly is going on here, so that I can change all delay values across my code to yield the smallest code size. Like say I have a delay that needs to be around 1000us, how do I come up with the number that has the smallest size? Because trial and error is not an option due to the number of these delays in my code.
This has been tested in Arduino IDE with MicroCore and Atmel Studio.
F_CPU is 9.6MHz
digitalWriteanddelayMicrosecondsalso don't make any difference from pure C either. (If your board is ATtiny13). \$\endgroup\$