Quote Originally Posted by dannybeckett View Post
I have a vague idea, using DECFSZ to decrement the variable passed to it from the C program. Does anyone know how to protect 16 bits of memory from being overwritten by anything else and place a variable in this specific location? This way, I can easily address it in assembly.
I'm not familiar with HITECH-C, but the inline assembly feature of most compilers can access C symbols, so you can refer to it by name:
Code:
#asm
decfsz some_variable, 3
Also, your function could simply wrap the __delay_us() function, like so:
Code:
void my_delay_us(unsigned short delay_amount)
    while (delay_amount > 10000) {
        __delay_us(9998);    // leave 1us for the comparison and 1 for the subtraction 
        delay_amount -= 10000;
    }
    while (delay_amount > 1000) {
        __delay_us(998);    // leave 1us for the loop comparison and 1 for the subtraction 
        delay_amount -= 1000;
    }
...
}
It's a little hackish and may suffer a couple microsecond inaccuracy when you get down below 10us, but it's something. Still, it should be better than a NOP loop.