I wrote some code similar to admin's to send pulses to my servos, however, I used _delay_us() instead of a for loop. This method has been great so far, and I can get very accurate speeds as I desire. However, until now I have been feeding this delay function a constant. I just wrote the rest of my program how I wanted it, but found that the servos constantly rotated at full speed clockwise. Upon investigation I have found that the delay produced by _delay_us() is much longer than the delay I'm asking for.
This code works:
PORTD|=0b00000001;
_delay_us(1000);
PORTD=0b00010000;
This code does not
Ldelay=1000;
PORTD|=0b00000001;
_delay_us(Ldelay);
PORTD=0b00010000;
When stepping through the debugger, the latter code produces a seemingly random delay of 5-8ms. I am sending the correct datatype (double) to the function. I know there are other ways to send a pulse to the servos, but I want to make this work. Is the _delay_us() function broken? Would I likely get the same result going a level down and using _delay_loop_2() ?
It took me about an hour to narrow down the problem. I don't understand why a function is treating a variable any differently than a constant of equal value. Thanks for any help.