Pulse Width Modulation (PWM) PDF

Title Pulse Width Modulation (PWM)
Author P. Ng
Course Micro-Controller Based Systems
Institution University of the West of England
Pages 8
File Size 306.4 KB
File Type PDF
Total Downloads 76
Total Views 159

Summary

Pulse Width Modulation (PWM)...


Description

5

Pulse Width Modulation (PWM)

Pulse width modulation (PWM) is the method of choice to control the amount of energy that is supplied to an actuator, light source (LED) or any other device when employing digital control systems. PWM requires fast switches. Typically these switches are FETs with a low RDS(ON ) .8 The principle of operation is shown in figure 39. In order to vary the amount of power/energy that is supplied to a load, the pulse width is modulated. PWM signals are described by PWM

Figure 39: Principle of PWM: The PWM period (TP W M ) is fixed and the waveform represents different duty cycles. The duty cycle is defined as the proportion of time the PWM output is switched on with respect to the PWM period. period and the duty cycle. The duty cycle is the proportion of time the output is switched on with respect to the PWM period. The principle of PWM is based on the idea that the average value s¯ of a periodic signal s(t) can be represented by s¯ =

1 T

Z

T

s(t)dt,

(1)

0

with T denoting the period. For rectangular signals with a pulse height of sˆ, the integration is particularly simple. Observing figure 40, we can write: 1 s¯ = T

Z

T 0

1 s(t)dt = T

"Z

TDC

sˆdt +

0

Z

T

TDC

#

0dt =

1 [ˆ s × TDC + 0] . T

(2)

With TDC = T × DC and 0 < DC < 1, it is easily observable that the average signal is just a linear function of the duty cycle DC, namely s¯ =

1 sˆ × T × DC = sˆ × DC. T

(3)

This looks much more pleasant, the average value of the signal is the PWM pulse height multiplied by the duty cycle. As an example, this is illustrated in 8R

DS(ON )

denotes the resistance between drain and source of a FET when switched on.

38

Figure 40: Illustration of a PWM signal.

figure 40. Imagine an 8 Ampere PWM signal with a duty cycle of 25%. The integration defined in equation 2 is graphically solved. The Area A of the pulse is equivalent to the area under the 2 Ampere average.

Figure 41: PWM: Illustration of the integration of a PWM signal. The DC current Iavg is equivalent to the 8A (IP W M )pulse with a duty cycle of 25%.

5.1

Choice of PWM period

The choice of the PWM period/frequency is not arbitrary. If the frequency is too low, the load will follow the pulse. If the frequency is too high, we may generate large amount of electromagnetic interference to other systems and would also expect high transmission losses. The choice of PWM frequency influences the amount of ripple in the resulting signal. This is illustrated in the following thought experiment: the circuit in figure 42 depicts a PWM source (V1) driving an RC load and what we are trying to control is the voltage on the capacitor C1. The PWM source is configured with a 25% duty cycle and is generating 5V voltage pulses. Three different PWM periods/frequencies are chosen and the illustrations show the different effect on the ripple. A PWM frequency of 4ms causes acceptable levels of ripple, depicted in figure 43. Increasing the

39

Figure 42: PWM circuit. V1 is the PWM source and R1 and C1 form the load. R1=20kΩ, C1=1µF. The time constant of the load is τ = RC = 20kΩ × 1µF = 20ms.

PWM period to 40ms leads to large ripples in the signal as shown in figure 44. A shorter PWM period of 0.4ms, as shown in figure 45 causes a very smooth, ‘ripple-free’ output signal.

Figure 43: 4ms PWM signal.

The important message to remember is that you have to understand your load before you can make a choice about the PWM period. For example, if one is required to drive an electric motor, an important factor is the mechanical time constant (determined by the inertia etc.) and the electrical time constant. The PWM frequency needs to be high enough so that the motor does not speed up with every PWM pulse and slow down during the inactive phase. On the other hand, if the PWM frequency is too high, the switching transistors may reach their limit and our motor driver is a potential source of interference to other systems. Many small DC motor applications are driven with PWM frequencies between 5kHz and 50kHz but every problem/system requires a thorough analysis. 40

Figure 44: 40ms PWM signal.

Figure 45: 400µs PWM signal

41

5.2

Generation of a PWM signal

The principle of generation of a PWM signal is depicted in figure 46. The main elements of a PWM generator are a timer/counter and a comparator whose outputs drive the PWM signal. At the beginning of the cycle, the PWM signal is switched on. The comparator compares the timer/counter value with the PWM duty cycle value. When the counter reaches the PWM duty cycle value, the PWM output is switched off. The counter continues to increase until it reaches the PWM period value and then resets, completing one PWM period.

Figure 46: Generation of a PWM signal with a saw-tooth signal and a comparator. The PWM counter counts up and resets when the PWM period has been reached; this is repeated continually. In the beginning of the duty cycle the PWM output is switched on. When the PWM counter reaches the value of PWM duty cycle register, the output is switched off. The duty cycle, therefore, can be controlled by changing the value in the duty cycle register. By changing the value in the PWM duty cycle register, the pulse width can be modulated. The period is normally not changed during operation. What is shown here is also referred to as edge aligned PWM generation, an alternative is centre aligned PWM and you should refer to the data sheet to explore both in detail.

5.3

PWM generation with the dsPIC30F4011

The dsPIC30F4011 has three PWM generators which can drive 6 different pins, operating as 3 pairs (3 × PWMxL, PWMxH, the low-side and high-side drivers). In the maximum configuration, the PWM generator is particularly suitable to drive 3phase brushless DC motors, where each pair is responsible for one phase. In the first instance, we are only going to use channel one of the PWM generator to drive one port (PWM1L) and the following description will focus on this mode of operation. Clearly, more advanced modes of operation are possible and you should read more about this once you are capable of a basic, workable configuration. As usual, all information about this is available in the data sheet,

42

with additional information in the Family reference. Note that the PWM generator is quite similar to a timer. Since we have already covered Timer1 last week, we can progress with fewer explanations. The main PWM configuration steps for PWM1 can be summarised as follows: •

clear PTEN // switch off PWM time base during configuration



set PTCKPS // choose time base prescaler



set PTPER



// set the PWM period

set PMOD1 // set PWM unit 1 into the independent mode (use PWMCON1bits.PMOD1 =1)( datasheet shows different naming compared with family reference!)



set PEN1L // enable PWM 1 low-side driver



set PEN1H // enable PWM 1 high-side driver



set PTEN // finally, switch on the PWM generator

Figure 47: The PWM control registers on the dsPIC30F4011. Once the PWM generator is running, you can change the pulse width by simply writing to the duty cycle register PDC1. Before you start your exercises, take into consideration that the PWM period register PTPER is only 15 bits wide. However, the duty cycle register PDC1 is 16 bits wide. To illustrate what this means for your PWM resolution, have a look at figure 48. The PWM period is set to 11 cycles (PTPER=10) and the prescaler is set to 1:1. Two different duty cycle values, namely PDC=14 and PDC=15, are depicted. You should note that the PWM generator effectively doubles your PWM resolution. Even though your PWM counter only counts up to ten (11 cycles), you can set duty cycle register to twice this range. In this example, a duty cycle of 100% would mean a PDC value of 22 [2 × (P T P ER + 1)], a duty cycle of 50% is achieved by setting PDC to 11. Generally, you want to achieve a high resolution so that your control output is quasi-analogue. This is achieved by counting to a high value. Be aware that the choice of resolution is not independent of your PWM period. Counting to a high value takes a longer time and therefore your PWM period gets longer. In other words, PWM period and resolution cannot be independently chosen. So, depending on your load and resolution requirements, you have to find a good compromise. Again, read in the data sheet for detailed information on the period and duty cycle generation. For an overview, all PWM registers are presented in figure 47 43

Figure 48: Resolution of the PWM duty cycle. The LSB of the duty cycle register determines the change at the positive or the negative edge. This effectively doubles the PWM resolution. For a prescaler value of 1:1 the maximum T P ER +1)) resolution in bits is ln(2×(Pln(2)

44

5.4

Exercises: PWM generation

As usual, you will have to recycle most of your work from the previous weeks. You need your I/O setup functions and your Timer1 functions. Start with a clean project and copy your last weeks files into this folder. 1. As usual, create a *.c and *.h file for your PWM functions. 2. Write a PWM setup function (e.g. setupPWM() ) that sets up channel one of the PWM generator for edge aligned PWM signals, driving the PWM1L pin which is connected to LED4. The PWM frequency should be set up to 250Hz. This is fast enough for the ‘slow’ human eye. Make notes about how you calculated the values in PTPER and PTCKPS. What is the resolution of your PWM system? 3. Extend your program from last week. You should keep your I/O and Timer1 setup Function and add the PWM setup functions. After this, your main should again enter a while(1) loop. Your main() should look similar to the example shown below. int main(void) { //initialise all necessary modules gpIOSetup(); //configures your I/O pwmSetup(); //configures the PWM generator timer1Setup(); //this starts the timer! //infinite loop while(1); //remain here forever, let Timer1 ISR do the work return 0; //we should never really return } 4. Given your PWM configuration, which value do you have to write to the duty cycle register to get a duty cycle of (a) 0% (b) 25%, (c) 50% and (d) 100%. 5. Now write a program that drives LED4 with four different pulse widths (10%, 50%, 75% and 100%) for 5 seconds each. Repeat this sequence continuously. Use the Timer1 interrupt routine (with a 10ms period) to achieve the timing. Verify this with the scope. Take scope screenshots and add them to your log-book and report. 6. Modulate LED4 with a 1Hz sine wave. The sine-function should be called in your 10ms Timer1 ISR you created last week. Remember that the output of sine function is a floating point value in the range [-1.0 .. 1.0] and you need to adapt this to the range of your PWM duty cycle register. 7. Extras: Use S3 to switch between 4 different duty cycles (10%, 50%, 75% and 100%). With each press of the button, the duty cycle should change so that you can step through 4 different brightness values, starting with 10% and looping from 100% back to 10% (i.e. 10%, 50%, 75%, 100% 10% 50% etc). Write this ‘state-machine’ either in the Timer1 ISR or, better, in a function that is called cyclically by the Timer1 ISR. 45...


Similar Free PDFs