9 - Pulse Width Modulation
9 - Pulse Width Modulation
sT-Embed Training
Ric Kolk
Altair Engineering
rkolk@altair.com
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 2
Topics:
• PWM & Duty Cycle
• ePWM Block – Modules
• Time Base
• Action Qualifier
• Deadband
• Event Time
• Solenoid Modeling, Simulation, Transfer Function Approximation, Processor in the Loop (PIL) Simulation (requires F28069M board to be attached)
• Motor Modeling, Dynamics, Time Constant
• Using the eCap block to record PWM Carrier Period and Frequency (Example requires the F28069M board to be attached)
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 3
Tpwm tOn
On value
Off value
On value
Off value
PWM Generation
In an embedded application, the target processor produces pulses from a Time Based Clock (TBCLK). The Time Based Clock can run at
the CPU speed or a fraction of it. Pulses produced by the TBCLK are counted as they occur forming a staircase signal whose count value
at any time is monitored by a Time Based Counter (TBCTR). When the TBCTR reaches a preset value named the Time Base Period
(TBPRD), the counter resets itself to 0 and the staircase signal repeats. PWM signals are produced based when the TBCTR equals a
Compare (CMP) value.
TBCTR
(counts)
Time Based Clock (TBCLK), (sec)
TBPRD value
CMP value
5
4
3
2
1
0
time
PWM
On value
Off value
time
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 5
PWM Resolution
The resolution, n, of a PWM generator is equal to the number of Time Based pulses present in the PWM period
expressed as a number of bits.
𝑇𝑝𝑤𝑚
Number of Time Base pulses per PWM period =
𝑇𝐵𝐶𝐿𝐾
𝑇𝑝𝑤𝑚
Resolution expressed as a number of bits: 𝑛 = 𝑙𝑜𝑔2
𝑇𝐵𝐶𝐿𝐾
For example, a 20kHz PWM signal is to be generated using an 80MHz CPU. The Time Based Clock
(TBCLK) is set to 1/80Mhz and the resolution is calculated as:
𝑇𝑝𝑤𝑚 1
= 20𝑘 = 4000
𝑇𝐵𝐶𝐿𝐾 1
80𝑀
The High Resolution Timer option, if available on your hardware, decreases the TBCLK to a value of
150e-12 seconds. This is particularly useful if your application requires a high PWM frequency
(NOTE: 250kHz and greater is considered to be a high PWM frequency).
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 7
PWM
A
Time Based Action Qualifier Deadband
Module Module PWM
Module
B
DCA
SOC
DCB A
Event Time
Module SOC
B
The ePWM block consists of four key Modules; (1) Time Based, (2) Action Qualifier, (3) Deadband,
and (4) Event Timer. Each Module is described in this document.
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 8
Explained on the
next slide
To solve this problem, select the “Change Period Dynamically” option which adds a “Period(1,16)” input pin to the PWM block. This
pin accepts a user defined “Fractional Period Multiplier” constant that is calculated as follows:
Experiment with the “Timer Period” value until you obtain a value that produces a PWM Carrier Frequency, F, such that F/2MHz is a
rational fraction between 0 and 1 (Note: 2MHz is the desired PWM Carrier Frequency). The “Fractional Period Multiplier” is set to this
rational fraction.
Power Loss: In a motor, torque is proportional to the average current. Consider a motor with a torque constant k driven by a constant
current source, 𝐼𝑐𝑜𝑛𝑠𝑡 and by a PWM current source that ranges from 0 to 𝐼𝑝𝑒𝑎𝑘 and has a Duty Cycle, DC. To achieve the same
torque, the following is true:
𝑇 = 𝑘 × 𝐼𝑐𝑜𝑛𝑠𝑡 = 𝑘 × 𝐷𝐶 × 𝐼𝑝𝑒𝑎𝑘
1
𝐼𝑝𝑒𝑎𝑘 = ×𝐼 The PWM driven motor requires
𝐷𝐶 𝑐𝑜𝑛𝑠𝑡 more current than the constant
𝑃 = (𝐼𝑝𝑒𝑎𝑘 × 𝐷𝐶)2 × 𝑅 current motor to develop the
same torque.
2
Heat: In a motor winding there is always a small resistance, R. As current is passed through the winding resistance, 𝐼𝑅𝑀𝑆 𝑅 watts of
energy is transferred from electrical to heat energy. For a PWM current signal with a period, T, on-time, tOn, Duty Cycle, DC, and
ranging from 0 to 𝐼𝑝𝑒𝑎𝑘 , the 𝐼𝑅𝑀𝑆 value is calculated as:
𝑡𝑂𝑛
1 2
1 2 𝑡𝑂𝑛
2 𝑡𝑂𝑛 𝑡𝑂𝑛
𝐼𝑅𝑀𝑆 = 𝐼𝑝𝑒𝑎𝑘 𝑑𝜏 → 𝑖𝑅𝑀𝑆 2 = 𝐼 𝑡 = 𝐼𝑝𝑒𝑎𝑘 → 𝐼𝑅𝑀𝑆 = 𝐼𝑝𝑒𝑎𝑘
𝑇 0 𝑇 𝑝𝑒𝑎𝑘 0 𝑇 𝑇
𝐼𝑅𝑀𝑆 = 𝐼𝑝𝑒𝑎𝑘 𝐷𝐶
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 12
For a constant current signal of value DC × 𝐼𝑝𝑒𝑎𝑘 , power loss through R is: 𝑃 = (𝐼𝑝𝑒𝑎𝑘 × 𝐷𝐶)2 × 𝑅
Switching Losses: An ideal switch is either fully on or off which means it never dissipates any power. Real switches don’t switch
instantaneously, they have a transition time during which they dissipate power. The transition time is usually fixed per edge. For
example, using a switch that requires a 1 microsecond transition time in a 25kHz PWM (40 microsecond period) means that the transition
time is 1/40 of the total. If the PWM frequency were increased to 250kHz (4 microseconds), this ratio would increase to ¼ which would
significantly and adversely affect performance.
Vibration and Noise: An electric motor uses wire coils to produce magnetic force. Every length of wire in the motor undergoes lateral
movement proportional to the current being passed through it. When the current is cycled, i.e. a PWM signal, the movement becomes a
regular vibration audible to the human ear. Since the range of human hearing is generally considered to be 20Hz to 20kHz, PWM
frequency is often selected to be 20kHz or greater to eliminate audible noise being produced.
Resolution: Normally a resolution of 10 bits (1/1024 = .001) is adequate. Based on your CPU speed and PWM period, confirm your
resolution is adequate. If not consider using the “High Resolution Timer”, adjusting the PWM period, overclocking, or moving to a faster
CPU.
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 13
Input pin
Output pin
Setup the PWM to produce a 10kHz carrier frequency with “Count Mode” set to “Up. The CPU Speed is 80MHz.
NOTE: there are 8000 discrete levels of duty cycle in this configuration
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 16
Setup the PWM to produce a 25nsec ON and 475nsec OFF signal with “Count Mode” set to “Up/Down”. The CPU Speed is 90MHz.
Based on the ON and OFF times provided, the Carrier Period is calculated as 500nsec OR equivalently, the Carrier Frequency is
2MHz.
“Timer Period” MUST be an integer
value, we cannot achieve the desired
2MHz Carrier Frequency.
To solve this problem, use the “Change Period Dynamically” option
which allows you to define a “Fractional Period Multiplier” and send
to the “ePWM” block through the “Period” input pin.
Operation:
Carrier Frequency
New Carrier Frequency Fractional Period Multiplier
=
New Timer Period = Timer Period ∗ Fractional Period Multiplier
Source Model:
PWM Setup:
Source Model
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 18
Debug Model
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 19
At each Event, any of four Actions can be performed. These actions provide a means to define the PWM edges:
X = Do Nothing
0 = Force the PWM value to 0 (Off Value)
1 = Force the PWM value to 1 (On Value)
T = Toggle the PWM value
if the PWM value = 1, Toggle will set it = 0 and if the PWM value = 0, Toggle will set it = 1
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 20
Events:
Z = When TBCTR = 0
up = When TBCTR = CMP while increasing
down = When TBCTR = CMP while decreasing
PWMA Action, P = When TBCTR = TBPRD
Event, & Pin
Settings
PWMB Action,
Event, & Pin User select: Action dropdown:
Settings X = Do Nothing
0 = Force PWM output to 0
1 = Force PWM output to 1
T = Toggle PWM output
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 21
PWM Examples
TBCTR
CMP
value
PWM time
time
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 22
PWM Deadband
An H bridge is an electronic circuit consisting of 4 switches. One use of an H bridge is to provide bidirectional rotation
control of a DC motor.
In the following sequence of switch conditions, PWMA transitions from ON (Closed) to OFF (Open) and PWMB from OFF to ON. The
sequence uses non-ideal switches, each with a “turn Off” time > “turn On” time.
PWMA commanded
S1 S3 S1 S3 S1 S3 S1 and S4 to Open
but due to their longer
S2 S4 S2 S4 S2 S4 switch “turn Off” times,
they remain closed
PWM Closed PWM Closed PWM while S3 (commanded
A A A Open by PWMB) closes
Closed because of it’s shorter
PWM
PWM Open PWM Open “turn On” time*.
B B B
PWM signal generators have a feature called “Deadband” which provides a method for independently adding delay to the PWMA and
PWMB edges (both rising and falling edges) to accommodate switch transition times and prevent a Shoot Through situation from
occurring.
* S2 was excluded in this explanation for clarity, it too could be in a closed state.
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 24
The input signals to the “Deadband” module are the PWMA and PWMB output signals from the “Action Qualifier” module. Internally, the
“Deadband” module creates the signals “DbA” and “DbB” for channel A and B based on the “Input Select” settings. The “Delay Mode” and
“Polarity” settings are then applied to the “DbA” and “DbB” signals to produce the PWMA and PWMB output signals (below):
Deadband Module
Solenoid Model
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 32
Vin = 5 volts
Fload = 0 Newtons
Simulation Update Time = 1e-5 seconds
To do this we will create a basic motor model, identify its fundamental time constant, and then select
the PWM Carrier Period short enough to produce an acceptable level of motor velocity fluctuation.
To calculate the Mechanical Time Constant, the following simplifications are applied;
Tload = 0 (no load torque)
Motor friction, B = 0
And the electrical dynamics are replaced by their dc-value;
Applying these assumptions, the motor model block diagram simplifies to:
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 42
In successful applications, the Mechanical Time Constant should be the fundamental (or dominant)
time constant, typically 100 to 1000 times slower than the electrical time constant.
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 43
If the initial motor velocity, w(0) = 1, and the dominant time constant of the motor = mechanical time
constant is 0.2 seconds,
The typical range for the PWM Carrier Frequency is 10KHz <= Fpwm <= 40KHz. Using frequencies
less than 10KHz result in unacceptable motor velocity ripple and values greater than 40KHz tend to
increase the transistor switching frequency (in the H Bridge) to a level that causes them to heat up
and prematurely fail.
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 44
Since the PWM Carrier Frequency is 10KHz, the target model update frequency is selected to
be 20 times faster (200KHz), this is equivalent to a target update time = 1/200KHz = 5e-6 sec.
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 45
The motor model transfer function receives the PWMA signal from
GPIO0 (below) NOTE: PWMB is not used in this example.
Monitor Buffers are used to record 100 elements of PWM data (into Monitor Buffer 0) and Motor
speed (into Monitor Buffer 1) – (below):
The trigger signal “:trig” is pulsed every time a PWM cycle begins (10KHz)
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 46
Format 1.16 required as the duty cycle command value for the ePWM block
The competed Debug model and results showing the PWM signal and the motor speed buffers are
presented below:
The following model is constructed. We have also included a “Monitor Buffer” to display the PWM
waveform in a “plot” block
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 50
Event 1
Triggers here &
timer is rest Event 2
Triggers here
PWM output (channel A) set to GPIO0
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 51
Timer count
Period
On time
time
Event 1
Triggers here &
timer is rest
Event 2
Triggers here
© 2016 solidThinking, Inc. Proprietary and Confidential. All rights reserved. An Altair Company. 52
End of Section