**Foreword**
PWM stands for Pulse Width Modulation. In Chinese, it is known as pulse width modulation. One of the key ideas behind PWM is that it uses the digital output of a microprocessor to control analog circuits. In reality, it leverages digital signals to simulate an analog effect. But what exactly does this mean? Let’s break it down step by step.
First, let's look at the name itself: pulse width modulation. It refers to varying the width of the pulses to achieve different outcomes. To better understand this, consider three different sets of pulse signals, as shown below:

**Figure 10-1: PWM Waveform**
This waveform has a period of 10ms, which corresponds to a frequency of 100Hz. However, the pulse widths of the high and low levels vary in each cycle, which is the core concept of PWM. A key term to remember here is "duty cycle." The duty cycle represents the ratio of the time the signal is high compared to the entire period. For example, the first part of the waveform has a 40% duty cycle, the second part 60%, and the third part 80%. This variation in duty cycle is what defines PWM.
But why can PWM be used to control analog circuits? In digital circuits, there are only two states: 0 and 1. For instance, when we light an LED in Chapter 2, if we set LED = 0, the LED turns on; if we set LED = 1, it turns off. If we rapidly switch the LED on and off, our eyes perceive it as a steady light. When the switching frequency exceeds about 100Hz, the human eye cannot detect the flickering, and the LED appears continuously lit.
However, the brightness of the LED depends on the proportion of time it is on versus off. By adjusting the duty cycle—changing how long the LED stays on or off—we can create varying levels of brightness, simulating an analog effect. For example, a higher duty cycle results in a brighter LED, while a lower one makes it dimmer.
To demonstrate this, we can use a timer to generate PWM signals. In this case, we use Timer T0 to control the output on P0.0. Unlike simple timing, we need to reload the timer twice per cycle with two different initial values to control the high and low periods. To make the brightness changes more noticeable, the program uses larger differences in the duty cycle.
Here’s a sample code snippet using a 51 MCU to generate PWM:
```c
#include
sbit PWMOUT = P0^0;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char HighRH = 0;
unsigned char HighRL = 0;
unsigned char LowRH = 0;
unsigned char LowRL = 0;
void ConfigPWM(unsigned int fr, unsigned char dc);
void ClosePWM();
void main() {
EA = 1;
ENLED = 0;
ADDR3 = 1;
ADDR2 = 1;
ADDR1 = 1;
ADDR0 = 0;
while(1) {
ConfigPWM(100, 10); // 100Hz, 10% duty
for(int i=0; i<40000; i++);
ClosePWM();
ConfigPWM(100, 40); // 100Hz, 40% duty
for(int i=0; i<40000; i++);
ClosePWM();
ConfigPWM(100, 90); // 100Hz, 90% duty
for(int i=0; i<40000; i++);
ClosePWM();
}
}
```
This approach allows us to control the brightness of an LED smoothly. Additionally, by combining two timers—one for generating PWM and another for dynamically adjusting the duty cycle—we can create effects like a "breathing light," where the brightness gradually increases and decreases, mimicking a soft, natural glow.
It’s worth noting that modern MCUs often include hardware PWM modules, making it easier to implement without relying on interrupts. However, for systems without such features, using software-based PWM via timers remains a practical and effective solution.
In summary, PWM is a powerful technique that bridges the gap between digital and analog worlds. By controlling the pulse width, we can effectively manage power, brightness, speed, and many other parameters in various applications—from lighting to motor control. Understanding and implementing PWM is a fundamental skill for anyone working with microcontrollers.
12V Power Battery,Electric Car Battery,Lithium Ion Battery,Lithium Rechargeable Battery
Sichuan Liwang New Energy Technology Co. , https://www.myliwang.com