fbpx

Pulse Width Modulation Using AVR

Description

Pulse-width modulation (PWM) or pulse-duration modulation (PDM), is a modulation technique that conforms the width of the pulse, the pulse duration, based on modulator signal information.

  • Although this modulation technique can be used to encode information for transmission, its main use is to allow the control of the power supplied to electrical devices, especially to inertial loads such as motors.
  • The term duty cycle describes the proportion of ‘on’ time to the regular interval or ‘period’ of time.
  • A low duty cycle corresponds to low power because the power is off for most of the time.
  • The duty cycle is expressed in percent, 100% being fully on.
  • By changing the width of the pulse applied to the DC motor we can increase or decrease the amount of power to the motor and increase or decrease the motor speed.

Generating PWM  in AVR

  • AVR contains separate hardware. By using this, the CPU instructs the hardware to produce PWM of a particular duty cycle.
  • ATmega16 has four in-built PWM channels.
  • There are two modes of operation of AVR timer which are able to generate the PWM signal.
    1. Fast PWM.
    2. Phase Corrected PWM.

Fast PWM

  • The Fast PWM mode is based on the single-slope operation.
  • In single-slope operation
    Register TCNTn counts from bottom value to maximum value and its value resets to zero and again counting starts from the bottom.
  • The register OCRn compares the value with the TCNTn register constantly.
  • When these two registers value match with each other then PWM output pin OCn value goes low when timer configured in non-inverting mode.
  • OCn value goes high when the TCNTn register reaches at the bottom value.
  • In inverting mode OCn pin behaves opposite to non-inverting mode.
  • The frequency of fast PWM mode signal is twice than the Phase Correct PWM mode signal because of single-slope operation.
  • Output  frequency:-
    fast PWM signal = Crystal frequency/(PrescalerX256)

Programming Steps

STEPS:
1. Select Fast PWM mode by setting WGM2 bits.
2. Program COM2 and select inverting or non-inverting mode.
3. Set the OC2 pin as an output pin.
4. Set OCIE2 bit of TIMSK register.
5. Enable global interrupt.

Phase correct PWM mode

  • The Phase correct PWM mode can be selected by setting bits WGM0=01.
  • It is based on a dual-slope operation.
  • In this method, TCNTn counts from bottom value to maximum value and maximum value to bottom value.
  • The OCRn register compares the value with the TCNTn register during up-counting and down-counting.
  • Output  frequency= Crystal frequency/(PrescalerX510)
    = 12000000/(1×510)
    = 23529.41 = 23.53KHz

Programming Steps

STEPS:
1. Select Fast PWM mode by setting WGM2 bits.
2. Program COM0 and select inverting or non-inverting mode.
3. Set the OC0 pin as an output pin.
4. Set OCIE0 bit of TIMSK register.
5. Enable global interrupt.

PWM Wave Form

Leave a Reply

Your email address will not be published. Required fields are marked *