[][src]Module stm32f1xx_hal::pwm

Pulse width modulation

The general purpose timers (TIM2, TIM3, and TIM4) can be used to output pulse width modulated signals on some pins. The timers support up to 4 simultaneous pwm outputs in separate Channels

Usage for pre-defined channel combinations

This crate only defines basic channel combinations for default AFIO remappings, where all the channels are enabled. Start by setting all the pins for the timer you want to use to alternate push pull pins:

let gpioa = ..; // Set up and split GPIOA
// Select the pins you want to use
let pins = (
    gpioa.pa0.into_alternate_push_pull(&mut gpioa.crl),
    gpioa.pa1.into_alternate_push_pull(&mut gpioa.crl),
    gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl),
    gpioa.pa3.into_alternate_push_pull(&mut gpioa.crl),
);

// Set up the timer as a PWM output. If selected pins may correspond to different remap options,
// then you must specify the remap generic parameter. Otherwise, if there is no such ambiguity,
// the remap generic parameter can be omitted without complains from the compiler.
let (c1, c2, c3, c4) = Timer::tim2(p.TIM2, &clocks, &mut rcc.apb1)
    .pwm::<Tim2NoRemap, _, _, _>(pins, &mut afio.mapr, 1.khz())
    .3;

// Start using the channels
c1.set_duty(c1.get_max_duty());
// ...

Then call the pwm function on the corresponding timer.

NOTE: In some cases you need to specify remap you need, especially for TIM2 (see Alternate function remapping):

  let device: pac::Peripherals = ..;

  // Put the timer in PWM mode using the specified pins
  // with a frequency of 100 hz.
  let (c0, c1, c2, c3) = Timer::tim2(device.TIM2, &clocks, &mut rcc.apb1)
      .pwm::<Tim2NoRemap, _, _, _>(pins, &mut afio.mapr, 100.hz());

  // Set the duty cycle of channel 0 to 50%
  c0.set_duty(c0.get_max_duty() / 2);
  // PWM outputs are disabled by default
  c0.enable()

Structs

C1
C2
C3
C4
Pwm
PwmChannel

Enums

Channel

Traits

Pins