PWM (PULSE WIDTH MODULATION)

INTRODUCTION

Suppose we want to vary the speed of D.C. motor connected to AVR microcontroller. Without gaining the knowledge of PWM it cannot possible. In this section, we learn the different modes of PWM and control the speed of D.C. motor connected to the microcontroller.

What is duty cycle?

duty cycle is the fraction of one period in which a signal or system is active. Duty cycle is commonly expressed as a percentage or a ratio. A period is a time it takes for a signal to complete an on-and-off cycle. As a formula, a duty cycle (%) may be expressed as:

Equally, a duty cycle (ratio) may be expressed as:

Where D is the duty cycle, PW {\displaystyle PW} is the pulse width (pulse active time), and T is the total period of the signal. Thus, a 60% duty cycle means the signal is on 60% of the time but off 40% of the time. The “on time” for a 60% duty cycle could be a fraction of a second, a day, or even a week, depending on the length of the period.

Suppose a D.C. motor is connected to the microcontroller and we want to vary its speed. We generate square wave to control the speed of the motor. This square wave is fed to D.C. motor. In WAVE GENERATION AND INPUT CAPTURE section, we learn how to generate the square wave. Speed of motor can be varied in two ways-

  1. By changing the voltage.
  2. By changing the current.

The speed of the motor can be varied if we change the voltage given to the motor. But in the previous section, we learned that the square wave coming out of micro controller can have only two states either ‘0’ means 0 volt or ‘1’ means 5 volts. So we cannot control the speed of the motor by changing voltage. Now the second method is to change the current given to the motor. But the current coming out of each pin of AVR micro controller is constant i.e.40mA. This means that the maximum current coming out of each pin of AVR micro controller is 40mA. So how can we vary the speed of D.C. motor?

 The answer lies in the following equations-

If we vary the power delivered to the motor then, the speed of the motor will vary. This power delivered depends on the voltage applied. And we also know that voltage depends on time or duty cycle. So, when we change the duty cycle then, we can vary the speed of the motor.

So, by changing the width of the pulse applied to the D.C. motor we can increase or decrease the amount of power provided to the motor, thereby increasing or decreasing the motor speed. We know that voltage has fixed magnitude, but it has a variable duty cycle. That means wider the pulse( high duty cycle), more is the power and higher the speed.

D.C. MOTOR CONTROL USING PWM-

Now we know that by varying the width of pulses we can control the speed of D.C. motor. We can make the pulses by simply using “_delay_ms(); ” function. Consider the following steps to make pulses-

  1. Consider a PINB0, where we have to make the pulse of different width. First, make this pin as an output.
  2. Then write ‘1’ in PORT register and give delay.
  3. Write ‘0’ in PORT register and again give delay.

Suppose we want to make a pulse of 60% duty cycle and time period of 100msec. Then, we write ‘1’ in its PORT register and give a delay of 60msec then, write ‘0’ in its PORT register and give a delay of 40msec. In this way, we are able to control the speed of D.C. motor by making the pulses of varying duty cycle.

MAIN POINTS TO BE REMEMBER DURING PROGRAMMING-

  1. Make pulse of specific duty cycle then, fed it to the positive input of D.C. motor.
  2. If we have to move the motor with different speeds than this method doesn’t work. As in this method, we can run the motor with the specific speed at an instant of time. To again change its speed we have to change the program code and introduce new values in delay function. So, this is the main drawback of this method.
  3. As we know that there are two terminals to D.C. motor one is positive and other is negative. We fed the pulse of the specific duty cycle to an only positive terminal of the motor the negative terminal remain unaffected.

 

Program code to make pulse of 60% duty cycle with time period of 100msec-

Program code to control the speed of D.C. motor using PWM –

CIRCUIT DIAGRAM-

PWM MODES-

We have seen the example to control the speed of the motor using PWM. But most widely we use build PWM feature of AVR. As it gives us the option of programming the period and duty cycle more conveniently.

In other PWM modes, we don’t have to make the pulses continuously in while(1) loop. Just we have to set some value of register and PWM wave is continuously flowing at some pin.

  1. FAST PWM.
  2.  PHASE CORRECT PWM.

Now we will study these modes using TIMER0, TIMER1, and TIMER2.

PWM MODES USING TIMER0:  We have studied TIMER0 in TIMERS IN AVR section now we will discuss the PWM modes using timer0. We don’t discuss TIMER2 here as we know that the difference between TIMER0 and TIMER2 is only of names of registers. Programming of TIMER0 and TIMER2 are quite similar. 

1.FAST PWM- To select FAST PWM mode in timer0 set TCCR0 register bits WGM01 and WGM00 high. The fast PWM mode is also called single-slope operation. The counter counts from 0x00 to 0xFF then restarts from 0x00. In CTC mode, we set some value in OCR0 register between 0x00 and 0xFF. Then when the value of counter register becomes equals to OCR0 (compare match occurs ) then, counter cleared. This same thing happens in FAST PWM mode but when compare match occur, the value of counter register doesn’t clear, but the value of OC0 pin changes. There are two different ways in which the value of OC0 pins changes which produce two modes in FAST PWM. These two modes of FAST PWM are-

  1. NON-INVERTING COMPARE OUTPUT MODE-The OC0 pin is cleared when compare match occurs between TCNT0 and OCR0. That means when TCNT0 is lesser than OCR0 register than OC0 pin set high. If TCNT0 becomes greater than OCR0 register than OC0 pin cleared.
  2. INVERTING COMPARE OUTPUT MODE- The OC0 pin is set high when compare match occurs between TCNT0 and OCR0 register. That means when TCNT0 is lesser than OCR0 register than OC0 is cleared. If TCNT0 becomes greater than OCR0 register than OC0 pin set high.

The PWM frequency for the output can be calculated by the following equation:

The N variable represents the prescale factor (1, 8, 64, 256, or 1024).

REGISTERS USED AND THEIR DESCRIPTION-

TCCR0:

EXAMPLE-PROGRAM TO GENERATE PULSE MODULATED WAVE USING FAST PWM MODE. USE TIMER0 AND XTAL=1MHz.

CIRCUIT DIAGRAM-

2.PHASE CORRECT PWM-The phase correct PWM mode (WGM01: WGM00=01) provides a high-resolution phase correct PWM waveform generation option. The phase correct pwm mode based on dual-slope operation. In phase correct PWM mode, the counter counts from 0x00 to 0xFF and then again from 0xFF to 0x00. In non-inverting compare output mode, when counter counts from 0x00 to 0xFF (up-counting) then, clear OC0 on compare match and when counter counts from 0xFF to 0x00 (down-counting) then, set OC0 on compare match.

In inverting compare output mode, when counter counts from 0x00 to 0xFF (up-counting) then, set OC0 on compare match and when counter counts from 0xFF to 0x00 (down-counting) then, clear OC0 on compare match.

Download (DOCX, 14KB)

Download (DOCX, 124KB)

PWM frequency for the output when using phase correct PWM can be calculated by the following equation:

The N variable represents the prescale factor (1, 8, 64, 256, or 1024)

REGISTERS USED AND THEIR DESCRIPTION-

TCCR0:

EXAMPLE-PROGRAM TO GENERATE PULSE MODULATED WAVE USING PHASE CORRECT  PWM MODE. USE TIMER0 AND XTAL=1MHz.

CIRCUIT DIAGRAM-

PWM MODES USING TIMER 1: We have studied about TIMER1 in TIMERS IN AVR section so, we not discuss here the register used in TIMER1.

FAST PWM MODE- In FAST PWM mode, the counter counts from BOTTOM (lowest value in TCNT1 ) to TOP(maximum value of TCNT1) then, cleared. There are five FAST PWM modes in timer1: mode 5, 6, 7, 14 and 15.

  • In mode 5 , 6 and 7 the TOP value is fixed at 0xFF , 0x1FF and 0x3FF respectively.
  • While in modes 14 and 15, ICR1 and OCR1A register represent the TOP value respectively.

REGISTER USED AND THEIR DESCRIPTION-

TCCR1A:

 

TCCR1B:

EXAMPLE-PROGRAM TO GENERATE PULSE WIDTH MODULATED WAVE USING FAST PWM MODE IN TIMER1.

 

PHASE CORRECT PWM: In PHASE CORRECT PWM, timer counts from BOTTOM until is reaches the TOP value, then is down-counts from TOP until it reaches BOTTOM. The TOV1 flag is set when timer returns back to zero.

There are five PHASE CORRECT PWM modes: mode 1, 2, 3, 10 and 11.

  • In mode 1 , 2 and 3 the top value are 0x00FF , 0x01FF and 0x03FF respectively.
  • In mode 10 and 11 the top value is stored in register ICR1 and OCR1A respectively.

REGISTER USED AND THEIR DESCRIPTION-

TCCR1A:

TCCR1B:

EXAMPLE-PROGRAM TO GENERATE PULSE WIDTH MODULATED WAVE USING PHASE CORRECT PWM MODE IN TIMER1.