PWM uses a wide range of applications to characterize analog quantities in a cycle with different duty cycles. A PWM class is provided in the mbed to operate the PWM, which can set duty cycle, period, and pulse width, respectively. because this is the use of single-chip internal Tim to generate a PWM wave, so not all IO support PWM output, specific pin pin support, need to check the datasheet, first step, the first to create a PWM object:
Pwmout MYPWM (pb_0); // PB0 pin output PWM wave
Next you can set the duty ratio of the PWM mypwm.write (0.1); Between 0-->1, representing the duty ratio from 0% to 100%. This time will already have waveform output, the default PWM wave frequency 50Hz, cycle 20ms In addition, we can also set the PWM period by the following three functions, set the unit is S,ms,us. The maximum value is limited to the hardware platform, the maximum is 65536us on the Stm32, that is 0.065536s period () Period_ms () Period_us () The pulse width of the PWM wave can be set by the following three functions , the set unit is also s,ms,us. Also limited to the hardware platform, stm32 on the maximum of 65535 pulsewidth () Pulsewidth_ms () Pulsewidth_us () want to know the current output duty ratio, can be read by reading () 。 Similarly, mbed provides operator overloading, and we can replace write with MYPWM = 0.2来. can also be done by similar i = MYPWM. To obtain the current PWM value fixed frequency, the duty ratio is adjustable pwmout mypwm (PB_0); PB0 pin Output PWM wave Mypwm.period_us (10); Set Frequency MYPWM = 0.1//duty ratio pulse width 1us, frequency 100khz, duty 10% pulse width 3us, frequency 100khz, duty-free 30% pulse width fixed, duty-free , frequency change pwmout MYPWM (PB_0); PB0 pin Output PWM wave Mypwm.pulsewidth_us () (10); Set pulse width mypwm.period_us (100); Set frequency, duty cycle = 10/100 pulse width 10us, period 100us, frequency 10khz, duty10% Pulse width 10us, period 50us, frequency 20khz, duty cycle 20%
PWM uses a wide range of applications to characterize analog quantities in a cycle with different duty cycles. A PWM class is provided in the mbed to operate the PWM, which can set duty cycle, period, and pulse width, respectively.
Because this is the use of single-chip internal Tim to generate a PWM wave, so not all IO support PWM output, specific pin pin support, need to check the datasheet, first step, the first to create a PWM object:
Pwmout MYPWM (PB_0); PB0 PIN Output PWM wave
Next you can set the duty ratio of the PWM
Mypwm.write (0.1); Between 0-->1, representing the duty ratio from 0% to 100%.
This time there will be waveform output, the default PWM wave frequency of 50Hz, the period of 20ms
In addition, we can also set the PWM period by the following three functions, set the unit to S,ms,us. The maximum value is limited to the hardware platform, the maximum is 65536us on the Stm32, that is 0.065536s
Period ()
Period_ms ()
Period_us ()
The pulse width of the PWM wave can be set by the following three functions, and the set unit is also s,ms,us. Also limited to hardware platforms, with a maximum of 65535 on Stm32
Pulsewidth ()
Pulsewidth_ms ()
Pulsewidth_us ()
If you want to know the current output duty ratio, you can read it through read ().
Similarly, mbed provides operator overloading, and we can replace write with MYPWM = 0.2来. can also be done by similar i = MYPWM. To get the current PWM value
Fixed frequency and adjustable duty ratio
Pwmout MYPWM (pb_0); // PB0 pin output PWM wave Mypwm.period_us (ten); // Set Frequency 0.1 // duty ratio
Pulse width 1us, frequency 100khz, duty-free 10%
Pulse width 3us, frequency 100khz, duty-free 30%
Pulse width fixing, duty ratio, frequency change
// PB0 pin Output PWM wave Mypwm.pulsewidth_us () (ten); // set Pulse width mypwm.period_us (+// set frequency, duty ratio = 10/100
Pulse width 10us, period 100us, frequency 10khz, duty cycle 10%
Pulse width 10us, period 50us, frequency 20khz, duty cycle 20%
The Pwmout of mbed learning