Common methods and precautions for microcontroller output PWM

Publisher:AlisallllLatest update time:2022-12-08 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The application of PWM can be said to be very wide, controlling motor speed, light brightness, communication modulation and many other fields.

My friends have asked me a lot about PWM, and I have been using PWM recently. Here I will share some information about PWM.

What is PWM?

PWM: Pulse Width Modulation, pulse width modulation.

There are many explanations on the Internet. Through the picture below, you can intuitively understand PWM, which is actually a pulse signal composed of high and low levels.

By changing the frequency (pulse period) and duty cycle, it can be applied in many situations.

Common PWM output methods

From the above description, PWM is an IO port that outputs high and low levels at different time periods.

1. In the novice (rookie) level while loop, blocking delay controls the high and low output of the IO port:

Blocking delay can be: software simulation delay, timer blocking delay, etc.

2. In the entry (junior) level while loop, non-blocking delay controls the high and low output of the IO port:

Non-blocking delays can be: timer identification detection, RTOS (system) delays, etc.

3. Familiar with (intermediate) level timer interrupt control IO high and low level output:

Timer interrupt configuration——>Start timer——>Respond to interrupt and control IO high and low levels...

4. Proficient (intermediate+) level

Timer PWM hardware control output:

Configure the IO corresponding to PWM and timer PWM output——> Start PWM automatic output...

Comparison:
Among the above PWM output methods, the first three will cause the CPU to interfere with the PWM output, which means it will occupy CPU resources. Especially the first two methods, not only occupy the CPU, but also have a relatively large error.

Using the third interrupt method, if the frequency is relatively high, the CPU consumption will be serious. This situation is suitable for microcontrollers without hardware PWM output.

The fourth type is that the microcontroller has its own hardware PWM output function. It only needs simple configuration to automatically output PWM waveforms without CPU intervention.

Hardware output PWM example

Here we take the familiar STM32F1 as an example: I will briefly share with you the hardware timer output PWM waveform.

PWM timer related macro definitions:

PWM configuration:

PWM output function interface:

Initialize the configuration, call the function interface, and directly output the PWM waveform:

Output PWM waveform:

illustrate:

This example uses the STM32 standard peripheral library. If you want to understand the principles in depth, it is recommended to use the standard peripheral library.

Of course, if you want to use the PWM function quickly without understanding its principles, you can directly use the STM32CubeMX configuration to generate code:

Configuration considerations

If you want to control it more accurately and better meet the needs of the application layer, you need to understand the principles step by step.

Let’s talk about some common questions below.

1. Pin mapping

If the pins you use need to be mapped, you need to configure the corresponding parameters.

For example: STM32F1 uses PB11 (need to check the data sheet):

The corresponding "mapping" code needs to be added:

2. Frequency and duty cycle accuracy

If a 32-bit timer is used, the frequency range is wider and the accuracy can be higher. For example: frequency: 0.01Hz, duty cycle 0.01%, etc.

If it is 16 bits, none of the parameters can exceed 16 bits (65535):

You can configure it according to your own situation, such as PWM (timer) counting clock, frequency division value, etc.

For actual application code, it is recommended to add judgment on each parameter to prevent crossing the boundary (here, for the convenience of understanding, it is written relatively simply).

3. More STM32 have hardware PWM output function, but different series may have slightly different configurations. Simply refer to the official routines and manuals.

Nowadays, most microcontrollers have their own hardware PWM output function. The advantage of the hardware is that it does not require CPU intervention. If not, you can try the timer interrupt method mentioned above.


Reference address:Common methods and precautions for microcontroller output PWM

Previous article:How to learn microcontroller data communication? This tool should be used well: serial communication
Next article:The microcontroller firmware is cracked

Recommended ReadingLatest update time:2024-11-23 11:07

STC microcontroller output adjustable pwm
Step 1: First, we need to configure the relevant PCA registers. For example, we configure the CCP0 pin to output PWM. // PCA0 initialization   AUXR1 &= ~0x30;   AUXR1 |= 0x10;   //Switch IO port, 0x00: P1.2 P1.1 P1.0 P3.7, 0x10: P3.4 P3.5 P3.6 P3.7, 0x20: P2.4 P2.5 P2.6 P2.7   CCAPM0 = 0x42; //Working mode PWM   PCA_P
[Microcontroller]
STC microcontroller output adjustable pwm
PWM Control with Raspberry Pi
PWM Overview A PWM signal is a square wave used in electronic control systems to control things like servo position, motor speed, and LED brightness. It is based on two important parameters: frequency and duty cycle. Frequency is the number of repetitive cycles the waveform produces in one second. Duty cycle re
[Embedded]
PWM Control with Raspberry Pi
STM32F407 About TIM1 output PWM
Enable OCx output through the combination of CCxE, CCxNE, MOE, OSSI and OSSR bits (TIMx_CCER and TIMx_BDTR registers). In fact, the MOE and AOE bits of the TIMx_BDTR register need to be set to 1. This is different from the normal configuration of PWM output. The code is as follows↓ /***********************************
[Microcontroller]
AVR microcontroller PWM controls the speed and forward and reverse rotation of DC motor
Controlled by uln2003, the following is the simulation schematic diagram: Source program: /***************************************************** This program was produced by the CodeWizardAVR V2.03.4 Standard Automatic Program Generator ?Copyright 1998-2008 Pavel Haiduc, HP InfoTech srl http://www.hpinfotech.com
[Microcontroller]
AVR microcontroller PWM controls the speed and forward and reverse rotation of DC motor
Considerations when driving a brushed DC motor with PWM: Losses and switching methods
In the previous article, we introduced four current regeneration methods for brushed DC motors under PWM drive. This article will introduce the precautions when PWM driving a brushed DC motor: loss and switching method. Using PWM output to drive a brushed DC motor: Loss thinking Since PWM drive is a
[Embedded]
Considerations when driving a brushed DC motor with PWM: Losses and switching methods
Main circuit design of three-phase PWM inverter
  With the development of power electronics technology , the application of inverters has penetrated into various fields, and generally requires inverters to have high-quality output waveforms. The quality of inverter output waveforms mainly includes two aspects, namely steady-state accuracy and dynamic performance.
[Power Management]
Main circuit design of three-phase PWM inverter
Research on Direct Current Control Strategy of Single-phase PWM Rectifier
1. Introduction With the development of power electronics technology, power electronic devices are being used more and more widely, resulting in a large number of nonlinear loads pouring into the power grid, causing increasingly serious harmonic pollution to the voltage and current of the power system. However, PWM re
[Power Management]
Research on Direct Current Control Strategy of Single-phase PWM Rectifier
STM8S003 Timer 2 generates PWM
It can be used as a reference for beginners //Start the clock  CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);  //Close before assigning  TIM2_DeInit();  TIM2_Cmd(DISABLE);    TIM2_TimeBaseInit(TIM2_PRESCALER_2, 1427);//1427*1us=700HZ  //PWM1 Mode configuration: Channel3,50%  TIM2_OC3Init(TIM2_OCMODE_PWM1, T
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号