2018-07-05 10:23 AM
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.
2018-07-05 10:31 AM
Crystal ball hazy here.
SETTIMEOUT(10ms); // 40ms for 20% DC
while(!TimedOut);
What's behind these?
JW
[EDIT]
Also, seing minimal code, I always naively assume the OP runs this minimal code - and more often than not (and gradually more often) it's not the case. So, is this a minimal code, or are you running any beefy OS, USB, ETH, LCD, any other stuff?
2018-07-05 11:53 AM
Never mind! Forgot I was driving a load (an LED) and it by itself introduced the latency. Took it out and the timing is spot on. Duh!
2018-07-05 12:06 PM
Adding 'And was that waveform taken directly at the mcu pin?' to the list, next to 'OS USB ETH LCD'.
JW
2018-07-05 12:34 PM
'
Any insights would be appreciated.'
zero insight as there is zero information provided behind those code pieces are doing.
2018-07-05 12:46 PM
Bare metal and yes minimal (no OS USB ETH LCD or nothing..). I even disable all interrupts including the systick (serviced by the 100KHz INT or system timer) during this time. The above is pseudo code because it's that simple. Like I said the timing looks consistent at 50ms period or 20Hz. Just the GPIO looking out of wack somehow, especially RESET.
Thanks for the reply!
2018-07-05 02:45 PM
Yes, that would help. Thanks man!
2018-07-05 02:46 PM
Moving on buddy