Analysis of PWM Principle of PIC Microcontroller

Publisher:数字翻飞Latest update time:2020-02-25 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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.

Analysis of PWM Principle of PIC Microcontroller

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.

Analysis of PWM Principle of PIC Microcontroller

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.

Analysis of PWM Principle of PIC Microcontroller

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.

Analysis of PWM Principle of PIC Microcontroller

  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:

Analysis of PWM Principle of PIC Microcontroller

  Where x{t} is the discretized speech signal; Ts is the sampling period; is the unmodulated width; and m is the modulation index.

Analysis of PWM Principle of PIC Microcontroller

  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, Analysis of PWM Principle of PIC Microcontroller . 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. Analysis of PWM Principle of PIC MicrocontrollerAt 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:

Analysis of PWM Principle of PIC Microcontroller

  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

Reference address:Analysis of PWM Principle of PIC Microcontroller

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

PIC microcontroller button controls PWM output LED light brightness C language program
#include pic.h //Call the header file. You can go to the PICC software to find the header file of the PIC16F87XA microcontroller. __CONFIG(0x3b31); //Define configuration word, crystal type: XT, enable gate dog, disable low voltage programming //************************************************************************
[Microcontroller]
A brief discussion on PWM modulation technology and PWM output sine and cosine waves
1. PWM PWM modulation technology---------includes adjusting duty cycle and frequency. PWM Applications------It is widely used in power output and motor drive. Power output: adjust voltage output at different frequencies or the same frequency, control current output, etc. Motor drive----I found a
[Embedded]
A brief discussion on PWM modulation technology and PWM output sine and cosine waves
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
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
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
The main discussion is about the Simulink model of PWM comparator
Functionality of PWM Comparator The three-phase two-level inverter used in this series of articles is still used as the control object. The figure below shows the waveform of the three-phase two-level inverter with sinusoidal wave modulation (SPWM). The input is a three-phase sine wave signal. The triangul
[Embedded]
The main discussion is about the Simulink model of PWM comparator
Realization of long-distance transmission of power drive PWM signal in high-voltage inverter
With the development of market economy and the improvement of automation and intelligence, the use of high-voltage inverters to control the speed of pump loads is not only beneficial to improving processes and improving product quality, but also a requirement for energy saving and economical operati
[Power Management]
Realization of long-distance transmission of power drive PWM signal in high-voltage inverter
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号