22
1 Lab 4: Pulse Width Modulation (PWM) and Introduction to Simple Virtual Worlds Fall 2019

Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

1

Lab 4: Pulse Width Modulation (PWM) andIntroduction to Simple Virtual Worlds

1

Lab 4: Pulse Width Modulation and Introduction to Simple Virtual Worlds

(PWM)

Fall 2019

Page 2: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

2

Virtual Spring and Virtual Wall

• Virtual Spring– Puck attached to a reference point by a

virtual spring with constant – If the puck is moved to either side,

spring exerts a restoring force – We will use a motor and encoder to

create a virtual torsional spring• Virtual Wall

– On one side of a virtual wall ( ), wheel spins freely (motor applies no force)

– Once the wheel rotates into ( ), motor applies a force

Virtual Spring (top) and Virtual Wall

x < x0

x > x0

Fs = −kx

k

Page 3: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Equations

• Wheel Torque, Nmm• Spring Constant, Nmm/degree• Displacement, degrees

• Embedded system units are encoder counts and PWM duty cycle!– (Counts/Encoder Rev)(Wheel Rev/Degree)

= Counts/Degree

3

Tw = −KΘw

Θw =

Tw =K =

Page 4: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

DC Motor Torque Control4

S32K144

FlexTimer PWM

Filter

Analog voltage

PWM Servoamplifier DC Motor

Current controlled PWM for torque control

Page 5: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Switched Capacitor Filter• Sampled data

techniques accurately imitate continuous time functions

• Switched-capacitor filters model RC components

• Less expensive and less sensitive to component tolerances

5

Application Note 40

AN40-1

an40f

March 1990

Take the Mystery Out of the Switched-Capacitor Filter: The System Designer’s Filter CompendiumRichard Markell

L, LT, LTC, LTM, Linear Technology and the Linear logo are registered trademarks of Linear Technology Corporation. All other trademarks are the property of their respective owners.

INTRODUCTION

Overview

This Application Note presents guidelines for circuits utiliz-ing Linear Technology’s switched-capacitor filter family. Although the switched-capacitor filter has been designed into “telecom” circuits for over 20 years, the newer de-vices are faster, quieter and lower in distortion. These filters now achieve total harmonic distortion (THD) below –76dB (LTC®1064-2), wideband noise below 55µVRMS (LTC1064-3), high frequency of operation (LTC1064-2, LTC1064-3 and LTC1064-4 to 100kHz) and steep roll-offs from passband to stopband (LTC1064-1: –72dB at 1.5 × fCUTOFF). These specifications make the new generation of switched-capacitor filters from LTC candidates to replace all but the most esoteric of active RC filter designs.

Application Note 40 takes the mystery from the design of high performance active filters using switched-capacitor filter integrated circuits. To help the designer get the high-est performance available, this Note covers most of the problems prevalent in system level switched-capacitor filter design. The Note covers both tutorial filter material and direct operating criteria for LTC’s filter parts. Special attention is given to proper breadboarding techniques, proper power supply selection and design, filter response selection, aliasing, and optimization of dynamic range, noise and THD. These issues are presented after a short introduction to the switched-capacitor filter.

The Switched-Capacitor Filter

Why use switched-capacitor filters? One reason is that sampled data techniques economically and accurately imitate continuous time functions. Switched-capacitor filters can be made to model their active RC counterparts in the continuous time domain. The advantages of the

switched-capacitor approach lie in the fact that a MOS integrated capacitor with a few switches replaces the resistor in the active RC biquad filter allowing full filter implementation on a chip. The building block for most filter designs, the integrator, appears in Figure 1. When implemented with resistors, capacitors and op amps it is expensive and sensitive to component tolerances. The switched-capacitor integrator, as seen in Figure 2, eliminates the resistors and replaces them with switched capacitors. The dependency on component tolerances is virtually eliminated because the switched-capacitor filter integrator depends on capacitor value ratios, and not on

+

C

VOUT

VIN

AN40 F01

R

fO = 12πRC

+

C2fCLK

C1

VOUT

VIN

AN40 F02

fO = fCLKC12πC2

Figure 1. Active RC Inverting Integrator

Figure 2. Inverting Switched-Capacitor IntegratorRef: Linear Technology Application Note 40 (1990)Take the Mystery Out of the Switched-Capacitor Filter: The System Designer’s Filter CompendiumRichard Markell

Page 6: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

4th Order Switched Capacitor Butterworth Filter

• We need to generate two PWM signals

• One for the sc-filter:– PWM frequency: 1 MHz– PWM duty cycle: 50%

• One for the DC motor:– PWM frequency: 20 kHz– PWM duty cycle: variable

6

2.0 Designing With The MF4 (Continued)

DS005064-44

FIGURE 6. MF4-50 Amplitude Response with ±5VSupplies

DS005064-46

FIGURE 7. MF4-50 Amplitude Response with ±2.5VSupplies

DS005064-22

FIGURE 8. Design Example Magnitude Response Specification where the Response ofthe Filter Design must fall within the shaded area of the specification

www.national.com11

Ref: National Semiconductor MF4 4th

Order Switched Capacitor Butterworth Lowpass Filter

Page 7: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

7

Duty Cycle-to-Motor Torque

0.5 1.0

1581.25

0

-1581.25

Duty Cycle Limits: 0.24-0.76M

otor

Tor

que

(Nm

m)

Motor Limits

PWM Duty Cycle

Duty Cycle = torque/3162.5+0.5

Page 8: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

FlexTimer PWM Generation 8

• 4 FlexTimer Modules– Each module has 8 channels

that may be used for output– We will configure channel 0

of FTM0 to drive the DC motor using pin PTB12, and channel 0 of FTM3 to drive the switched capacitor filter using pin PTB8

– We will use Edge Aligned PWM (EPWM) signals.

Page 9: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

9

Programming the FTM for EPWM

• A counter counts up from 0 and is compared with the threshold value Cth

• High-true pulses: The PWM signal is high until the counter value matches Cth, and then switches to low

• When the counter value matches Cmax the signal goes high again, the counter overflows to 0, and resumes counting.

Cmax

Cth

Page 10: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

• Cmax determines the frequency of our PWM• Cth determines the duty cycle of our PWM

Ton=Tc*Cth : PWM On TimeT=Tc*(Cmax+1) : PWM switching periodDC = Cth/(Cmax+1)

• The FTM clock is set to 10 MHzTc=1/fc=10-7 sec. : One clock tick

• Cmax and Cth are number of clock ticks• Note: tradeoff between switching period and duty cycle

resolution. Suppose Cmax = 9:Cth = 1, 2, 3, … 10àDC = 0.1, 0.2, 0.3, ... à Resolution = 10%

10

Duty Cycle Resolution

Page 11: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Example

• Suppose we want PWM frequency =

20kHz and motor torque = 0 (50% duty

cycle)

T = 1/20kHz

1/Tc = 10MHz

T = Tc*(Cmax+1) à Cmax = (T/Tc-1) = 499DC = Cth/(Cmax+1)àCth = 0.5*500 = 250

• Resolution = 0.2%

11

Page 12: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

12

FTM Configuration Registers

• One set of configuration registers for each FTM.- FTM0 used for the DC motor- FTM3 used for the switched capacitor filter

• We will only need a few registers; leave the rest at their reset values.- Section 45.4.3.1

Page 13: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Status and Control (SC) Register 13

• The SC register is used to configure the FTM. • The CLKS bitfield is set for the appropriate FTM clock frequency:

external clock = SPLLDIV1_CLK_FREQ = 10 MHz (see eecs461.h).• We shall also need to set the PWMENn bit to enable channel n for PWM output.• Leave CPWMS at default value 0 to specify that the counter counts upward until it

rolls over to its initial value; this is used for Edge Aligned PWM.

Page 14: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Features Mode Selection (MODE) Register 14

• The MODE register contains several bitfields used to configure the FTM. • We shall only need to ensure that the Write Protection Disable is set so that

we can write to write-protected registers (WPDIS=1). (Note that 1 is the reset value of this bitfield.)

• Note: leave the bitfield FTMEN = 0.

Page 15: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Channel n Status and Control (CnSC) Register15

• One CnSC register for each of the 8 channels of an FTM.

• Set MSA, MSB, ELSA, ELSB bitfields for Edge-Aligned High-true pulses• See Table 45-5. Channel Modes Selection for details.

Page 16: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Counter and Initial Value Registers: CNT and CNTIN16

• The CNT register contains the current value of the counter used to determinethe duty cycle and switching period of the PWM signal. We will not read the counter; the comparisons between counter value and Cth and Cmax are done by the FTM.

• The CNTIN register contains the initial value INIT of the counter: set INIT = 0b0.

Page 17: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Modulo (MOD) Register 17

• The MOD register contains the value of Cmax in units of FTM clock cycles.• When the counter reaches MOD it rolls over to INIT = 0b0• Note: the switching period of the PWM signal is equal to MOD +1 FTM clock cycles.

Page 18: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Channel n Value (CnV) Register18

• One CnV register for each of the 8 channels of an FTM.

• When the FTM channel is used for output the VAL register contains the value that the counter must match to change the PWM signal from high to low.

• Set VAL equal to Cth, the threshold value that determines the PWM duty cycle.

Page 19: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Double Buffering

• Recall: we will change the PWM duty cycle by changing the value of Cth by setting the VAL bitfield in the SCn register.

• Whenever parameters that control system behavior are changed during the flow of execution, unpredictable results may occur unless precautions are taken.

• What could happen if Cth is changed during a counting cycle?

• S32K144 uses double buffering to assure PWM updates happen predictably

19

Page 20: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

Double Buffering• Double buffered means we don’t directly update the VAL bitfield of the CnV register that specifies the duty cycle of the PWM signal.

• Instead we update the write buffer for the CnV register.• The VAL bitfield is updated from the write buffer only at

the end of every full PWM switching period.– VAL updated when the FTM counter reaches the value set in MOD (Cmax) and rolls over to the value set in CNTIN (0b0).

20

Page 21: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

21

Lab 4 Software

• As usual, you are given pwm.h with function prototypes; you will write the functions in pwm.c, plus application code in lab4.c

• Four functions are required:– setPWM: Calculate Cmax and Cth and set the CnV

register to Cth– InitPWM: Initialize the FlexPWM module– InitPWMPCRs: Set up the PCR registers for the

motor and filter– oututTorque: Outputs torque to wheel in Nmm.

• Template code provided

Page 22: Lab 4: Pulse Width Modulation and Lab 4: Pulse Width ...Lab 4 Software •As usual, you are given pwm.hwith function prototypes; you will write the functions in pwm.c, plus application

22

Lab 4 Assignment

• Use everything you’ve learned so far:– Read a duty cycle value from a GPIO pin and

output a PWM signal to the oscilloscope only.– Drive the motor and haptic wheel with the PWM

signal. Output a constant 200 Nmm torque. Experiment with different frequencies and observe motor response. What do you expect to happen at 1kHz? 20KHz?

– Implement the virtual spring and virtual wall. Experiment with different values of the spring constant and observe the effect.