Repeatable, periodic glitch in timer
Posted: Thu Mar 03, 2022 7:45 pm
I am trying to use a pyboard to generate a squarewave that is used as a pin interrupt by an Arduino. I have a timer on the pyboard setup with a 500 Hz frequency and in a for loop I check to see if the timer callback function has set a flag variable. The problem I am having is that there is a very repeatable glitch every roughly 196 loop cycles (196/500 Hz = 0.39 seconds). I asked the Arduino to tell me how much time as elapsed since the last pin interrupt and I generated this graph (hopefully inline below):
So, if the squarewave were perfect, it would always be exactly 2000 microseconds between edges, but every 0.39 seconds it goes up to 2600 or so and then makes up the time on the next cycle. The weird thing is that I overlaid 5-10 test runs and they overlay almost perfectly. I don't know if the timer has to reset something, but 0.6 milliseconds is a lot of clock cycles.
Here is my timer code:
def tick(timer):
global isr_happened, nISR
isr_happened = 1
nISR += 1
tim = Timer(1, freq=1000)
tim.counter() # get counter value
tim.freq(500) # 0.5 Hz
tim.callback(tick)
And here is the top of the loop that waits for the flag and generates the squarewave:
for i in range(N):
#pin_A15.on()
while (isr_happened == 0):
# wait for next interrupt
time.sleep_us(100)
#pin_A15.off()
# square wave that toggles each time step
if isr_state == 1:
isr_state = 0
pin_B4.off()
else:
isr_state = 1
pin_B4.on()
Any thoughts on what could cause such a repeatable 0.6ms glitch every 0.4 seconds?
Thanks,
Ryan
So, if the squarewave were perfect, it would always be exactly 2000 microseconds between edges, but every 0.39 seconds it goes up to 2600 or so and then makes up the time on the next cycle. The weird thing is that I overlaid 5-10 test runs and they overlay almost perfectly. I don't know if the timer has to reset something, but 0.6 milliseconds is a lot of clock cycles.
Here is my timer code:
def tick(timer):
global isr_happened, nISR
isr_happened = 1
nISR += 1
tim = Timer(1, freq=1000)
tim.counter() # get counter value
tim.freq(500) # 0.5 Hz
tim.callback(tick)
And here is the top of the loop that waits for the flag and generates the squarewave:
for i in range(N):
#pin_A15.on()
while (isr_happened == 0):
# wait for next interrupt
time.sleep_us(100)
#pin_A15.off()
# square wave that toggles each time step
if isr_state == 1:
isr_state = 0
pin_B4.off()
else:
isr_state = 1
pin_B4.on()
Any thoughts on what could cause such a repeatable 0.6ms glitch every 0.4 seconds?
Thanks,
Ryan