PWM is the abbreviation of pulse width modulation, that is, pulse width modulation. It modulated the width of a series of pulses to obtain the expected waveform;
1. PWM is a digital coding method for analog signal levels. Through the use of high-resolution counters, the duty cycle of the square wave is modulated to encode the level of a specific analog signal. The equivalent implementation is based on an important conclusion in the sampling theorem: when the impulse is equal and the narrow pulse with different shapes is added to the stage with inertia, the effect is basically the same. Impulse refers to the surface connection of a narrow pulse. The results mentioned here are basically the same, that is, the output response waveforms of this link are basically the same.
2. If the output waveforms are analyzed in Fourier mode, their low-frequency characteristics are very similar and only slightly different in high-frequency bands;
The PWM signal is digital. At any given moment, the DC power supply at Full amplitude is either completely on or off ). A voltage or current source is added to a simulated load using a repeating pulse sequence of on or off. (To put it simply, a digital signal is used to control the load to achieve the effect of simulating the signal control load; the load, such as the steering gear)
Changing the pulse cycle can achieve the FM effect, and changing the pulse width or duty cycle can achieve the pressure regulating effect. Therefore, the appropriate control method can be used to coordinate the voltage or current and frequency changes;
PWM control has many advantages:
1. signals from the processor to the controlled system are in the digital form, and digital-analog conversion is not required;
2. When the signal is kept in digital form, the noise effect can be minimized. The noise is only when it is strong enough to change logical 1 to logical 0 or logical 0 to logical 1, it can also affect the digital signal, which is the main reason for PWM communication;
Stm32 PWM:
In addition to tim6 and 7, the stm32 Timer. Other timers can be used to generate PWM output. Among them, the advanced timer tim1 and tim8 can generate a 7-way PWM output at the same time.
The general-purpose timer can also generate up to four PWM outputs at the same time, so that stm32 can generate up to 30 PWM outputs at the same time; (if re-imaging is added, more can be done)
PWM output mode:
The PWM output of stm32 adopts two modes: Mode 1 and Mode 2, which are determined by the ocxm bit of the timx_ccmrx register ("110" is Mode 1 and "111" is Mode 2 ). the differences between mode 1 and Mode 2 are as follows:
"110": PWM mode 1 ----- when counting up, once timx_cnt <timx_ccr1, Channel 1 is the valid level; otherwise, it is the invalid level; When counting up, once timx_cnt> timx_ccr1, Channel 1 is invalid level (oc1ref = 0), otherwise it is valid level (oc1ref = 1 ).
"111": PWM mode 2 ----- when counting up, once timx_cnt <timx_ccr1, Channel 1 is invalid level; otherwise, it is valid level; When counting down, if timx_cnt> timx_ccr1, Channel 1 is the valid level; otherwise, the level is invalid;
In the counting mode, the PWM and timx functions in the same way as the timer. They also have the upward counting mode, downward counting mode, and central alignment mode;
PWM output pin:
The output pin of PWM is determined:
Different timx instances have different pins, but considering the pin reuse function, stm32 proposes a concept of re-imaging, that is, by setting some related registers, to output PWM on other non-original pins.
For example, in the tim3 2nd channel, when there is no re-image, the specified pin is pa.7. If some re-images are set, the output of tim3_ch2 is mapped to pb.5; if a full re-image is set, the output of tim3_ch2 is mapped to pc.7;
PWM output signal:
PWM outputs a square wave signal. The signal frequency is determined by the timx clock frequency and the timx_arr pre-divider. The specific setting method is described in detail in the previous timer section. The duty cycle of the second output signal is determined by the timx_crrx register. The calculation formula is as follows:
Duty Cycle = (timx_crrx/timx_arr) * 100%
Therefore, you can enter appropriate values into the CRR to output the desired frequency and duty cycle square wave signal;
Procedure:
1. Set the RCC clock;
2. Set the gpio clock. The gpio mode should be set to reuse the push-pull output gpio_model_af_pp. If you need to reset the pin, you need to use the gpio_pinremapconfig () function;
3. Set the registers related to the timx timer, which is the same as the previous timer registers;
4. Set the PWM registers of the timx timer;
A. Set the PWM mode (by default, the PWM is implemented by Dong Jie)
B. Set the duty cycle (formula calculation)
C. Set the polarity of the output (described earlier)
D. The most important thing is to enable the timx output status and the PWM output enabling of timx;
After the related settings are complete, enable the timx timer through timx_cmd () to obtain the PWM output;
PWM for stm32