Is it possible to use a PWM interrupt? #12320
Replies: 5 comments 1 reply
-
PWM currently does not allow to specify a handler which is called on counter wrap (which happens at every cycle). I do not know of any plans to do so. It's good that you found a way of achieving it at the same pin. Otherwise another Pin would have to be used for that purpose. Note: If you enclose code into lines with three backticks each ``` it will be printed properly. Or indent the whole code block with a tab. |
Beta Was this translation helpful? Give feedback.
-
@robert-hh thanks for the quick response. I used the "<>" button to enclose the code, which seems to have inserted a single backtick at each end of the code rather than three. |
Beta Was this translation helpful? Give feedback.
-
@FilipSLO thanks for the response. I have had a look at the PIO documentation and found myself hopelessly lost almost immediately. It seems very complex! There is actually a PIO program that does PPM, which I haven't tried yet but it's on the agenda. |
Beta Was this translation helpful? Give feedback.
-
Good luck. I tried PIO recently with breathing LED (pure PIO not PWM HW). It works nicely. |
Beta Was this translation helpful? Give feedback.
-
@FilipSLO yeah, I've managed to run a couple of the examples too, some of them do things similar to what I need to do for my project. But I'm having trouble wrapping my brain around the structure of the code, with it's weird combination of assembler and C, and the requirement to use the SDK which is very C centric. But you can use it to create a python output module of some sort, but I have not yet been able to understand how it all fits together. A really good tutorial about micropython and PIO would be good, but I haven't found one yet. :-( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The PWM module in a Raspberry Pi Pico can trigger an interrupt when the counter wraps back to zero again, and I would like to use that interrupt. But I can't figure out how to access it from within a micropython script. I've found the register addresses for the interrupt registers, but I can't figure out how to connect an interrupt to a function defined in the program.
I've come up with what I consider to be a really kludgy way that works, or at least appears to, but I basically don't trust it. What the program does is define a pin (with a led attached) as an output pin, then defines an interrupt for it on the falling edge. Then redefine the same pin as a PMW pin. The led flashes and I get lots of lines printed, but somehow, I kinda just don't trust it, and I'd rather do it "correctly" if I can.
Here's the code
`from machine import Pin, PWM
from micropython import schedule
from time import sleep
def printit(t):
print("pin ", t)
def cb1(t):
schedule(printit, t)
blue = Pin(12, Pin.OUT)
blue.irq(handler=cb1, trigger=Pin.IRQ_FALLING, hard=True)
blue = PWM(Pin(12), freq=10, duty_u16=30000)
sleep(3)
blue.deinit()
need this to turn the led off again
blue = Pin(12, Pin.OUT)
blue.off()`
All advice gratefully received.
Beta Was this translation helpful? Give feedback.
All reactions