Simple Interrupt Problem
-
I was working on a significantly more complex programs and having some problems that I traced back to the fact that the interrupt wasn't firing.
I've written a new, extremely simple program just to prove to myself that I can make interrupts work, and its not going so well.
it uses the user button on the expansion board to trigger interrupts to increment a counter (or at least that's the idea)
here's the contents of boot.py, there is no main.py:
import micropython from machine import Pin micropython.alloc_emergency_exception_buf(100) int_counter =0 def int_handler(): global int_counter int_counter+=1 button=Pin('GP17', mode=Pin.IN, pull=Pin.PULL_UP) button.irq(trigger=Pin.IRQ_RISING|Pin.IRQ_FALLING, handler=int_handler)
I know the pin setup is working because when I go to the repl and run button() I get a value that toggles as I press and release the button
I know the handler function works because when I manually run int_handler(), int_counter increments.
Further, I suspect the trigger is working because when I press the button for the first time, the heartbeat LED blinks very rapidly for a couple seconds. However it won't do that again unless the button.irq() method is re-called.
However, when I press the button, int_counter does not increment.
Any idea what's going on here? I'm stumped
-
This works:
import micropython from machine import Pin micropython.alloc_emergency_exception_buf(100) int_counter =0 def int_handler(pin_obj): global int_counter int_counter+=1 button=Pin('GP17', mode=Pin.IN, pull=Pin.PULL_UP) button.irq(trigger=Pin.IRQ_RISING|Pin.IRQ_FALLING, handler=int_handler)
The key is that the "os" passes the pin object to the callback function as an argument. However, if your callback function isnt expecting any arguments, this causes an exception. Since the exception occurs outside of the main program execution its a little confusing to debug.
-
@KMcLoud or @LoneTech Any chance you could share some working interrupt code? I'm stuck trying to make this work myself... Thanks!
-
Thank you so much, that fixed it!
I'm hugely supportive of all the awesome work you guys are doing!
Is there any way I can contribute to the docs to make that more clear? because I read them over a couple times never picked that up.
a note on the "make a post" page indicating you need to use ``` instead of the BBcode would be super helpful too, any way I could help with that?
-
This post is deleted!
-
This post is deleted!
-
Use triple backticks
```
for code blocks.Pin
interrupt handlers are called with the pin as an argument, so you need to add an argument forint_handler
.