Module rp2040_hal::pwm [−][src]
Expand description
Pulse Width Modulation (PWM)
To access the PWM pins you must call the ‘split’ method on the PWM. This will return a
_____
struct with access to each PWM pin:
use embedded_hal::PwmPin; use rp2040_hal::{pac, pwm::Pwm0}; let mut peripherals = pac::Peripherals::take().unwrap(); let pin_num = 0; let mut pwm_pin = Pwm0::new(pin_num);
Once you have the PWM pins struct, you can take individual pins and configure them:
pwm_pin.default_config(&mut peripherals.PWM, &mut peripherals.PADS_BANK0, &mut peripherals.IO_BANK0, &mut peripherals.RESETS); pwm_pin.set_duty(32767); pwm_pin.enable();
The following configuration options are also available:
pwm_pin.min_config(&mut peripherals.PWM, &mut peripherals.PADS_BANK0, &mut peripherals.IO_BANK0, &mut peripherals.RESETS); pwm_pin.get_duty(); pwm_pin.get_max_duty(); pwm_pin.set_ph_correct(); // Run in phase correct mode pwm_pin.clr_ph_correct(); // Don't run in phase correct mode pwm_pin.set_div_int(1u8); // To set integer part of clock divider pwm_pin.set_div_frac(0u8); // To set fractional part of clock divider pwm_pin.set_inv(); // Invert the output pwm_pin.clr_inv(); // Don't invert the output pwm_pin.set_top(u16::MAX); // To set the TOP register pwm_pin.divmode_div(); // Default divmode. Counts up at a rate dictated by div. pwm_pin.divmode_level(); // These 3 divmodes can be used with a PWM B pin to read PWM inputs. pwm_pin.divmode_rise(); pwm_pin.divmode_fall();
default_config() sets ph_correct to false, the clock divider to 1, does not invert the output, sets top to 65535, and resets the counter. min_config() leaves those registers in the state they were before it was called (Careful, this can lead to unexpected behavior) It’s recommended to only call min_config() after calling default_config() on a pin that shares a PWM block.