1. What is PWM
What is PWM? Professionally speaking, it is pulse width modulation. In layman's terms, it is the continuous output of high and low levels. For example, the microcontroller outputs a high level for 40ms, then a low level for 60ms, and then continuously and periodically outputs high and low levels alternately. This is a typical PWM wave. The high level output time of 40ms is the legendary pulse width, and 40ms+60ms=100ms is the PWM cycle. Assuming that we set the cycle to 100ms, we can increase or decrease the high level time according to our needs. For example, the high level output time is increased to 80ms, then the low level time becomes 20ms. The high level output time can also be reduced to 20ms, then the low level time becomes 80ms. This is the high level time that pulse width modulation adjusts. The proportion of time occupied by the high level (pulse width) in the cycle is called the duty cycle. The mathematical formula expresses that the high level time/cycle time = duty cycle.
2. PWM implementation method
How do we make the microcontroller output PWM waves? There are two ways: one is to use I/O output directly, and the other is to use the module.
2.1 Using I/O port to output PWM
If your microcontroller does not have a PWM output function module, you can write a program to output PWM waves yourself. Because PWM is not just high and low levels. An I/O plus a timer is easy to do for any microcontroller.
2.2 Using the module to output PWM
PIC microcontrollers have a PWM output module. Using the module to output PWM is actually simpler, more convenient and more accurate than using I/O to output PWM.
3. Basic principles of PWM control
Theoretical basis:
When narrow pulses with equal impulses but different shapes are applied to a link with inertia, the effects are basically the same. Impulse refers to the area of the narrow pulse. The effects are basically the same, which means that the output response waveforms of the link are basically the same. The low frequency band is very close, and there are only slight differences in the high frequency band.
Figure 1 Various narrow pulses with different shapes but the same impulse
Area equivalence principle:
The narrow voltage pulses shown in Figure 1 are added to the first-order inertia link (RL circuit) respectively, as shown in Figure 2a. The response waveforms of the output current i(t) to different narrow pulses are shown in Figure 2b. It can be seen from the waveform that the shape of i(t) is slightly different in the rising section of i(t), but its falling section is almost exactly the same. The narrower the pulse, the smaller the difference in the response waveforms of each i(t). If the above pulses are applied periodically, the response i(t) is also periodic. After decomposing it with Fourier series, it can be seen that the characteristics of each i(t) in the low frequency band will be very close, and only differ in the high frequency band.
Figure 2 Response waveforms of various narrow pulses with the same impulse
A series of pulses of equal amplitude and unequal width are used to replace a half-sine wave. The half-sine wave is divided into N equal parts and can be regarded as N connected pulse sequences with equal width but unequal amplitudes. Rectangular pulses are used instead, with equal amplitude, unequal width, overlapping midpoints, equal areas (impulses), and widths that vary according to the sinusoidal law.
SPWM waveform - a PWM waveform whose pulse width changes according to the sinusoidal law and is equivalent to a sine wave.
Figure 3: Using PWM wave instead of half sine wave
4. Analysis of PWM working principle
The pulse width modulation wave is usually composed of a series of rectangular pulses with different duty cycles, and its duty cycle is proportional to the instantaneous sampling value of the signal. The figure below shows the principle block diagram and waveform diagram of the pulse width modulation system. The system consists of a comparator and a sawtooth wave generator with a period of Ts. If the voice signal is greater than the sawtooth wave signal, the comparator outputs a positive constant A, otherwise it outputs 0. Therefore, it can be seen from the figure that the comparator outputs a series of falling edge modulated pulse width modulation waves.
From the analysis of Figure b, it can be seen that the width of the generated rectangular pulse depends on the amplitude value of the voice signal at the falling edge of the pulse tk. Therefore, the time interval between the sampling values is non-uniform. Inserting a sample-and-hold circuit at the input of the system can obtain a uniform sampling signal, but for the actual situation of tk-kTs《《, the difference between uniform sampling and non-uniform sampling is very small. If the sampling is assumed to be uniform sampling, the kth rectangular pulse can be expressed as:
Where x{t} is the discretized speech signal; Ts is the sampling period; is the unmodulated width; and m is the modulation index.
However, if the following approximation is made for the rectangular pulse: the pulse amplitude is A, the center is at t = k Ts, and it changes slowly between adjacent pulses, then the pulse width modulated wave xp(t) can be expressed as:
Where, . Without spectrum analysis, it can be seen from equation (2) that the pulse width signal is composed of the voice signal x(t) plus a DC component and a phase modulation wave. At that time , the signal overlap caused by the phase modulation part can be ignored, so the pulse width modulation wave can be directly demodulated by a low-pass filter.
5. Advantages of PWM
One advantage of PWM is that the signal from the processor to the controlled system is in digital form, without the need for digital-to-analog conversion. Keeping the signal in digital form minimizes the effects of noise. Noise can only affect a digital signal if it is strong enough to change a logic 1 to a logic 0 or a logic 0 to a logic 1.
Improved noise immunity is another advantage of PWM over analog control, and this is the main reason why PWM is sometimes used for communication. Switching from analog signals to PWM can greatly extend the communication distance. At the receiving end, the modulating high-frequency square wave can be filtered out and the signal can be restored to analog form through appropriate RC or LC networks.
In short, PWM is economical, space-saving, and has strong noise immunity. It is an effective technology that is worthy of use by engineers in many design applications.
6. The role of PWM
What can PWM be used for? The more typical application of PWM is LED dimming and motor speed control. How does PWM dim and control? It is easy to understand. Take LED dimming as an example. If the LED is powered on all the time, the LED is the brightest. If the LED is not powered off, the LED is not bright. If the LED is powered on and off repeatedly in a very short period of time, the LED will appear to be bright, but at a low brightness.
If the LED is bright when the microcontroller outputs high level, then the longer the high level output time in the PWM wave, the brighter the LED will be, and vice versa.
7. PIC microcontroller learning PWM signal output
Function: The buzzer is controlled by the keyboard (BUTTON). When the keyboard is pressed, a PWM signal (frequency 880Hz, pulse width 50) is output through the RC2/CCP1 interface to drive the buzzer to sound.
Experimental environment: Proteus
Programming language: Assembly
Programming environment: MPLAB
Microcontroller: PIC16F877
Crystal: 20MHz
The Proteus simulation circuit is as follows:
Experimental source program:
LIST P=16F877
; Set pwm to 880Hz, 50% duty cycle INCLUDE P16F877.INC
ORG
0000H
START BANKSEL TRISC; Enter the BANK BCF where TRISC is located
TRISC, 2; Clear TRIC bit 2 to make the CCP1 pin an output
MOVLW D'70'
MOVWF PR2; write 70 to PR2 to set the PWM period
BANKSEL CCPR1L
MOVLW D'35'
MOVWF CCPR1L
MOVLW 0X06
MOVWF T2CON; bit2 = 1, enable TI mer2, bit1-1, pre-scaling value is 16
CHECKBUTTON BTFSC PORTA,4
GOTO
BUTTONOFF
BUTTONON MOVLW H'0C'
MOVWF CCP1CON; Set CCP1 to PWM mode
GOTO
CHECKBUTTON
BUTTONOFF
CLRF CCP1CON
GOTO CHECKBUTTON
END
Previous article:PIC microcontroller tutorial: the composition of the 13-bit program counter memory
Next article:Design of precise refueling system based on PIC microcontroller
Recommended ReadingLatest update time:2024-11-16 16:29
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Intelligent Control Technology of Permanent Magnet Synchronous Motor (Written by Wang Jun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- ULN 2803a and PLC usage issues
- Features and applications of the MS554X series of serial input, voltage output, 16-bit digital-to-analog converters (DACs)
- pic16f877 tm0 makes the LED flash every 10ms
- Meet the Electrocardiogram (ECG) Si1172 ECG Evaluation Kit
- TI C6678 Multi-core DSP Architecture
- [TI mmWave Radar Review] + Belated AWR1443 EVM Board Unboxing
- Watch Shuige's video for a reward | MIPI D-PHY transmitter physical layer consistency test
- Summary of Problems and Solutions in CCS6 Compilation
- [STM32WB55 review] +thread trial 1
- Guess the question about the list of materials for the undergraduate group of the electronic competition: Sound source localization system