Javatpoint Logo
Javatpoint Logo

Pulse Width Modulation in AVR Microcontroller

Pulse Width Modulation (PWM) is a square wave with varying low and high amplitude signal. A general PWM signal is given in a figure below:

Pulse Width Modulation in AVR1

There are various terms related with the Pulse Width Modulation (PWM):

  • Off-Time: Duration of time period when the signal is low.
  • On-Time: Duration of time period when the signal is high.
  • Duty cycle: It is the percentage of the time period when the signal remains ON during the period of the pulse width modulation signal.
  • Period: It is the sum of off-time and on-time of pulse width modulation signal.

Duty Cycle:

Calculation of duty cycle is done by calculating the ON-time from total period of time. It is a ratio between ON-time and total time period of the signal using period calculation, duty cycle is calculated as shown in the equation below:

Pulse Width Modulation in AVR2

Period:

As represented in the above figure, Toff represents the off-time and Ton represents the on-time of a signal. Period is a sum of both on and off times and period is calculated as shown in the equation below:

Pulse Width Modulation in AVR3

PWM: Voltage Regulation

PWM signals when operates at different duty cycle it gives a varying voltage at the output. Voltage regulation method is used in various areas of application like:

  • Audio
  • LED dimmers
  • Analog signal generation
  • Switching regulators
  • and many more..

Voltage regulation operation is performed by averaging the PWM signal. Output voltage is calculated as shown in the equation below:

Pulse Width Modulation in AVR4

The output voltage can be directly varied by varying ON time of the pulse width modulated signal.


AVR Timers as PWM:

Mostly in AVR Microcontroller the on-chip PWM channel is available which makes the PWM usage much simpler and highly accurate. AVR timers and counters can be used in PWM mode of operation without disturbing the basic timer function.

As in case of microcontroller (AT90S8515), Timer1 can be used in PWM mode by setting PWM10 and PWM11 bits in TCCR1A register.

The following modes are available in PWM of AVR microcontroller:

PWM10PWM11Description
00PWM operation of Timer/Counter1 is disabled
10Timer/Counter1 in 8-bit PWM Mode
01Timer/Counter1 in 9-bit PWM Mode
11Timer/Counter1 in 10-bit PWM Mode

The pre-scalar mode for Counter1/Timer can be selected by the use of clock select bits in TCCR1B register.

Width of a pulse is loaded in a timer output compare registers OCR1A (OCR1AL & OCR1AH) and OCR1B (OCR1BL & OCR1BH). The counter1/Timer acts as an up/down counter, counting from $0000 to TOP as shown in a table below:

PWM ResolutionTimer Top ValueFrequency
10-bit PWM$03FF (1023)Ftck1/2046
9-bit PWM$01FF (511)Ftck1/1022
8-bit PWM$00FF (255)Ftck1/510

Here it counts and turn down again to zero before the cycle is repeated. When a counter value matches the content of 10 least significant bits of OCR1B or OCR1A, the OC1B/PD5 (OC1A) pins are cleared or set according to the settings of COM1A0/COM1A1 or COM1B1/COM1B0 bits in Timer/Counter1 control register (TCCR1A) as shown in a table below:

COM1X0COM1X1Effect on OCX1
00Not Connected
10Not Connected
01Cleared on compare match, up-counting. Set on compare match down-counting (non-inverted PWM)
11Cleared on compare match, down-counting. Set on compare match up-counting (inverted PWM)

Note: X can be A or B register.


AVR: Assembly Code for running PWM:


AVR: PWM Setup in C:





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA