4. Pulse Width Modulation

download 4. Pulse Width Modulation

of 23

Transcript of 4. Pulse Width Modulation

  • 8/11/2019 4. Pulse Width Modulation

    1/23

    Pulse Width Modulation

    Robotics and Machine Intelligence

    NIT Trichy

  • 8/11/2019 4. Pulse Width Modulation

    2/23

    What is PWM ?

  • 8/11/2019 4. Pulse Width Modulation

    3/23

    How is PWM generated ?

    PWM is generated from asquare-wave(0V-5V) of aparticular frequency andvarying duty - cycle.The avg.value of the PWM output isequal to Vmax * Duty Cycle

  • 8/11/2019 4. Pulse Width Modulation

    4/23

    Examples of PWM

  • 8/11/2019 4. Pulse Width Modulation

    5/23

    Some more examples...

  • 8/11/2019 4. Pulse Width Modulation

    6/23

    What is the need of PWM ?

    Say we have a 300RPM motor and voltage rating is12 Volt so if you want to run motor to 150RPM thenwhat will you do?? Reduce voltage to 6 volt butthats not good idea as RPM is not linear function ofvoltage and as voltage decrease from specifiedrating Torque will also get reduced. So here comesthe PWM concept : what you can do is you canswitch on and switch off the motor repetitively such

    that effectively you get 50% ON time if you do itreasonably fast then motor seems runningcontinuous.

  • 8/11/2019 4. Pulse Width Modulation

    7/23

    Use of PWM to run DC motor

    If a person wants to drive a DC motor with variable speed but heonly has constant voltage supply. As an alternative for PWMhe can add a variable resistance in series with the motor.

    The drawbacks of this

    connection are:

    1. The resistance value cannotbe varied dynamically(automation is difficult).

    2. There will be unnecessarypower loss across theresistor.

  • 8/11/2019 4. Pulse Width Modulation

    8/23

    Use of PWM to run DC motor

    By adjusting the duty cycle of the signal (modulating the width ofthe pulses) i.e., the time fraction for which it is "on", theaverage power can be varied, and hence the motor speed. Inother words by varying the duty cycle we are getting differentvalues of average voltage (Vavg) across the motor resulting indifferent speeds.

  • 8/11/2019 4. Pulse Width Modulation

    9/23

    PWM Overview

    Advantages of PWM:

    1. Here the switch is either on or off unlike normal regulation(using variable resistance), so less power is wasted as heat andsmaller heat-sinks can be used.

    2. Since no resistor is used, there is no power loss.

    3. Can be easily automated by programmable control.

    Disadvantages of PWM:

    1. We require extra circuitry to implement PWM (in AVR we havein built PWM-circuitry on chip).

    2. Some authorities claim the pulsed power puts more stress onthe DC motor bearings and windings, shortening its life.

  • 8/11/2019 4. Pulse Width Modulation

    10/23

    How is PWM Implemented ?

    Clock period of an MCU is , generally 1Mhz inAtmega8.

  • 8/11/2019 4. Pulse Width Modulation

    11/23

    Dual Slope Phase Correct PWM

    T= Tclk*TOP *2

    TOP =255

    Duty cycle = (OCR/TOP)

    If, Timer_value OCRthenPWM Output=Low

    If, Timer_value < OCRthenPWM Output=High

  • 8/11/2019 4. Pulse Width Modulation

    12/23

    PWM in ATMega8/8L

  • 8/11/2019 4. Pulse Width Modulation

    13/23

    Basic PWM Code

    General Code to generate a square wave that has 50% duty cycle at pin 15(OC1A) :

    int main(){Step 1 Set PB1 (OC1A) as output pin;Step 2 Enable PWM;Step 3 Select Phase correct PWM mode and TOP value;Step 4 Set OCR value TOP/2;Step 5 Set prescalervalue and clock source ;Step 6 Start PWM;while(1){//do any job here

    }return 0;}

    Now if you connect Oscilloscope at pin 15 you can see a square wave with 50% duty cycle

  • 8/11/2019 4. Pulse Width Modulation

    14/23

    Register Settings

    Timer/Counter 1 Control Register A TCCR1A

    Bit 7:6 COM1A1:0: Compare Output Mode for channel A

    Bit 5:4 COM1B1:0: Compare Output Mode for channel B

    The COM1A1:0 & COM1B1:0 control the Output Compare Pins (OC1A and OC1B (respectively)

    behavior. If one or both of the COM1A1:0 bits are written to one, the OC1A output overrides

    the normal port functionality of the I/O pin it is connected. And for COM1B1:0. However, note

    that the Data Direction Register (DDR) bit corresponding to the OC1A or OC1B pin must bewritten1in order to enable the output driver.

  • 8/11/2019 4. Pulse Width Modulation

    15/23

    Register Settings

    Compare Output Mode, Phase Correct and Phase and Frequency Correct PWM

    Bit 3 FOC1A: Force Output Compare for channel A Bit 2 FOC1B: Force Output Compare for channel BThese bits are not used in phase correct PWM set write there bit 0

  • 8/11/2019 4. Pulse Width Modulation

    16/23

    Register Settings

    Timer/Counter 1 Control Register B TCCR1B

    Bit 2:0 CS12:0: Clock SelectThe three clock select bits select the clock source to be used by the Timer/Counter,

  • 8/11/2019 4. Pulse Width Modulation

    17/23

    WGM bit settings for PWM

    Bit WGM13, WGM12 in TCCR1B and WGM11, WGM10 in TCCR1A

    WGM stand for Wave generation mode. There are several PWM and time mode here we want to dosetting for phase correct PWM for TOP=255 and for that you need to write

  • 8/11/2019 4. Pulse Width Modulation

    18/23

    Register Settings

    Timer/Counter 1 TCNT1H and TCNT1L:

    Output Com pare Register 1 A OCR1AH and OCR1AL:

    Similarly w e have Output Comp are Register 1 B OCR1BH and OCR1BL registers.

  • 8/11/2019 4. Pulse Width Modulation

    19/23

    Standard PWM Coding in AVR

    #include

    int main()

    {

    Step 1 Set PB1 (OC1A) as output pin;

    Step 2 Enable PWM;

    Step 3 Select Phase correct PWM mode and TOP value;

    Step 4 Set OCR value TOP/2;//Du ty-Cyc le = 50%

    Step 5 Set prescaler value and clock source ;

    Step 6 Start PWM;

    while(1){//do any job here}

    return 0;

    }

  • 8/11/2019 4. Pulse Width Modulation

    20/23

    Step Details :

    Step 1 Set PB1 (OC1A) as output pin; For detail check i/o tutorial Code: DDRB|=(1

  • 8/11/2019 4. Pulse Width Modulation

    21/23

    Step Details :

    Step 4 Set OCR value TOP/2;

    Code:

    OCR1A=128;

    Step 5 Set prescalervalue and clock source ;

    Here we will set prescaler 1024 for that we need to write CS12=1, CS11=0, CS10=1

    *For detail see Clock Select Bit

    Code:

    TCCR1B=0x05;

    Step 6 Starts PWM;

    Its done, PWM has started.

  • 8/11/2019 4. Pulse Width Modulation

    22/23

    AVR-C Code for PWM

    #include

    int main(){

    DDRB|=(1

  • 8/11/2019 4. Pulse Width Modulation

    23/23

    Happy PWM Coding