pyb module has a pyb.pwm function, and many of the pyboard pins are circled i.e. indicating support for pwm ... but there's no documentation on micropython.org.
pyb.pwm takes two int arguments, and anecdotal evidence points to it being used by pyb.Servo and pyb.LED on LED4, but otherwise it's a bit of a mystery ... has anyone worked out how to use pyb.pwm? If so, could you share your findings?
pwm
Re: pwm
Is it possible if we create arduino like function such as digitalWrite() and analogWrite()?
Re: pwm
Just landed Timer.channel(Chan, pyb.Timer.PWM, ...)
See: https://github.com/micropython/micropyt ... mer.c#L510
And http://wiki.micropython.org/platforms/b ... r-Examples:
See: https://github.com/micropython/micropyt ... mer.c#L510
And http://wiki.micropython.org/platforms/b ... r-Examples:
Re: pwm
pyb.Pin('X1').value(0/1) is the equivalent to digitalWriteyllumi wrote:Is it possible if we create arduino like function such as digitalWrite() and analogWrite()?
AnalogWrite from arduino is too simplistic. There are 14 timers on the pyboard.
Re: pwm
It looks great! But I rather don't understand. Can you refer me any knowledge base to understading all of it (OC, IC, etc)?dhylands wrote:Just landed Timer.channel(Chan, pyb.Timer.PWM, ...)
See: https://github.com/micropython/micropyt ... mer.c#L510
And http://wiki.micropython.org/platforms/b ... r-Examples:
Re: pwm
OK - there are 3 basic modes: PWM, OC, and IC.
Each timer has a period register which can set either using freq= or prescaler= and period=
Each timer can have multiple channels.
A channel may be exposed on a pin (sometimes more than one channel (usually from different timers) are exposed on the same pin, but the pin can only choose one).
With PWM, the counter runs from 0..period and the pulse_width detemines how long the pulse stays active. So if you look at the first example:
http://wiki.micropython.org/platforms/b ... r-Examples
The top channel is set to generate a 25% duty cycle and the bottom is set to generate a 50% duty cycle. This example is also showing center-aligned PWM, where two channels from the same timer have their PWM pulses aligned in the center. You can also have them edge aligned, although center aligned is better in some cases; it reduces power spikes if you're using the PWM signal to drive a motor, then having both edges go active at the same time produces a larger power spike than having them staggered.
PWM performs 2 actions for each "cycle". When the counter reaches zero, it sets the output active (except when the pulse-width is zero), and when the counter reaches the pulse-width value, it sets the output inactive. This is what forms the output pulse.
With Output Compare mode, you can only perform one action per cycle. These actions are things like: set-active, set-inactive, toggle. The action occurs when the compare value matches the counter. The example was setup to toggle the output 2000 times per second, which should generate a 1 kHz square wave, which is what the logic analyzer capture shows.
With Input Capture mode, you're configuring the channel to record the value of the counter when some event occurs. You can look for rising edges, falling edges, or both (so 3 different types of events). The third example uses PWM to generate a pulse suitable for driving an RC servo. This is done using timer 5, channel 1, which is sent out on pin X1. It assumes you have a jumper wire connecting X1 and X4 and sets up an input capture on timer 2, channel 4 (which is on pin X4).
The input capture interrupt looks fir a rising edge, and when it finds it it saves away the captured value in ic_start. When a falling edge is detected, it subtracts ic_start from the captured value and this then determines the width of the pulse.
5 times per second, the main loop prints out the value of the servo pulse width being generated (in microseonds) and the value of the last captured pulse (also in microseconds). If everything is working properly, they should be the same.
Each timer has a period register which can set either using freq= or prescaler= and period=
Each timer can have multiple channels.
A channel may be exposed on a pin (sometimes more than one channel (usually from different timers) are exposed on the same pin, but the pin can only choose one).
With PWM, the counter runs from 0..period and the pulse_width detemines how long the pulse stays active. So if you look at the first example:
http://wiki.micropython.org/platforms/b ... r-Examples
The top channel is set to generate a 25% duty cycle and the bottom is set to generate a 50% duty cycle. This example is also showing center-aligned PWM, where two channels from the same timer have their PWM pulses aligned in the center. You can also have them edge aligned, although center aligned is better in some cases; it reduces power spikes if you're using the PWM signal to drive a motor, then having both edges go active at the same time produces a larger power spike than having them staggered.
PWM performs 2 actions for each "cycle". When the counter reaches zero, it sets the output active (except when the pulse-width is zero), and when the counter reaches the pulse-width value, it sets the output inactive. This is what forms the output pulse.
With Output Compare mode, you can only perform one action per cycle. These actions are things like: set-active, set-inactive, toggle. The action occurs when the compare value matches the counter. The example was setup to toggle the output 2000 times per second, which should generate a 1 kHz square wave, which is what the logic analyzer capture shows.
With Input Capture mode, you're configuring the channel to record the value of the counter when some event occurs. You can look for rising edges, falling edges, or both (so 3 different types of events). The third example uses PWM to generate a pulse suitable for driving an RC servo. This is done using timer 5, channel 1, which is sent out on pin X1. It assumes you have a jumper wire connecting X1 and X4 and sets up an input capture on timer 2, channel 4 (which is on pin X4).
The input capture interrupt looks fir a rising edge, and when it finds it it saves away the captured value in ic_start. When a falling edge is detected, it subtracts ic_start from the captured value and this then determines the width of the pulse.
5 times per second, the main loop prints out the value of the servo pulse width being generated (in microseonds) and the value of the last captured pulse (also in microseconds). If everything is working properly, they should be the same.
-
- Posts: 118
- Joined: Wed Dec 27, 2017 8:52 am
Re: pwm
All the links in page are showing up Bad gateway
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: pwm
I've reported the wiki outage to @Damien so hopefully it will be fixed shortly.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.