Skip to main content
e d
Associate III
July 5, 2018
Question

GPIO latency?

Posted on July 05, 2018 at 19:23

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.

0690X00000604jnQAA.jpg
    This topic has been closed for replies.

    7 replies

    waclawek.jan
    Super User
    July 5, 2018
    Posted on July 05, 2018 at 19:31

    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?

    e d
    e dAuthor
    Associate III
    July 5, 2018
    Posted on July 05, 2018 at 19:46

    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!

    e d
    e dAuthor
    Associate III
    July 5, 2018
    Posted on July 05, 2018 at 20:53

    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!

    waclawek.jan
    Super User
    July 5, 2018
    Posted on July 05, 2018 at 21:06

    Adding 'And was that waveform taken directly at the mcu pin?' to the list, next to 'OS USB ETH LCD'.

    JW

    e d
    e dAuthor
    Associate III
    July 5, 2018
    Posted on July 05, 2018 at 21:45

    Yes, that would help. Thanks man!

    henry.dick
    Associate II
    July 5, 2018
    Posted on July 05, 2018 at 21:34

    '

    Any insights would be appreciated.'

    zero insight as there is zero information provided behind those code pieces are doing.

    e d
    e dAuthor
    Associate III
    July 5, 2018
    Posted on July 05, 2018 at 21:46

    Moving on buddy