Electronic – PIC16: Understanding the bound on the argument to _delay


My PIC16 compiler, XC8, defines a _delay(n) pseudo-function which inserts a delay of n cycles. There is a restriction however (see here, page 59):

The delay argument must be a constant and less than
approximately 179,200 for PIC18 devices and approximately 50,659,000
for other devices.

Where do these upper bounds come from? The factorisation of 50,659,000 (2^3 * 5^3 * 7 * 7237) does not seem to shed any insight.

Best Answer

_delay is generated at compile time, so the code is not necessarily the same for all values, they can pad, or even compound loops to achieve the exact desired number of cycles.

50659000/2^(8*3) is approximately 3. So probably the core loop they are using is a 3 instruction loop (if an 8 bit microcontroller, 3 decrements are needed for a 3*8 bit variable). Since it jumps in 3's, they can pad with one or two nop to get the exact number of cycles. This requires the decrement and jump if/ifnot zero instruction to be possible (and with/without carry).