GPIO latency?
I am trying to understand a potentially simple issue that I don't seem to be able to wrap my head around. I am setting up a simple bit banging PWM using system timer (running at 100 KHz or 10uS interval) and a GPIO pin. PWM is 20Hz going back and forth between 20% DC and 80% DC. The code is as simple as:
GPIO_OFF;
SETTIMEOUT(10ms); // 40ms for 20% DC
while(!TimedOut);
GPIO_ON;
SETTIMEOUT(40ms); // 10ms for 20% DC
while(!TimedOut);
(then loop back if necessary)
The frequency seems to be consistent at 20Hz for both duty cycles so I know the system timer isn't a problem. But there appears to be some latency wiggling the GPIO pin (10ms - 8.408ms = 1.592ms or about 16%). With my clock running at 10 uS period I would hope to have accuracy within 2-3% max, not 16% like what I've been seeing. Any insights would be appreciated.

