Arduino Projects    |   Raspberry Pi  |     Electronic Circuits   |    AVR    |    PIC    |    8051    |    Electronic Projects

How to use fast PWM (Pulse Width Modulation) Mode of AVR microcontroller Timer


Developed By: 
Akshay Daga
This article is in continuation of PWM generation using AVR timer. In the previous article, PWM generation using Phase correct PWM mode is described. However, there are some applications like DAC, power regulation and rectification etc. which require high frequency PWM wave. The PWM generation using Fast PWM mode is suitable for such applications. This article focuses on Fast PWM mode of AVR Timer.
Fast PWM (Pulse Width Modulation) Mode of AVR Timer


The Fast PWM mode is based on single-slope operation. In single slope operation, the register TCNTn counts from bottom value to maximum value and its value resets to zero. The counting starts again from bottom. The register OCRn compares the value with the TCNTn register constantly. If the timer is configured in non-inverting mode, PWM output pin (OCn) goes low when the value of the above two registers matches. The OCn pin becomes high when the TCNTn register reaches at bottom value. In inverting mode OCn pin behaves opposite to non-inverting mode. For timer 0 fast PWM mode, following table shows the functionality of COM 0[1:0] bits.
Frequency of fast PWM mode signal is twice than Phase Correct PWM mode signal because of its single slope operation.       
Output frequency of fast PWM signal = Crystal frequency÷(Prescaler ×256)
Objective: Compare output waveform of Phase correct PWM mode and Fast PWM generated signal.
For this objective Timer 0 is configured in Phase correct PWM and Timer 2 is configured in Fast PWM mode. The Duty cycle of signals is set to 50%.
Circuit description:
The connection of ATmega16 is shown in circuit diagram. Since, Timer0 and Timer2 are used to generate PWM wave then output will be taken on OC0(PB3) and OC2(PD7) pins respectively. 
Programming steps:
The programming steps to configure Phase correct PWM mode is similar as used in previous article. The following steps are written to configure the Timer2 for Fast PWM mode:
1. Select Fast PWM mode by programming WGM2 [1:0] bit.
2. Program COM2[1:0] and select inverting or non-inverting mode.
3. Set OC2 pin as output pin.
4. Set OCIE2 bit of TIMSK register.
5. Enable global interrupt by “sei()” command.
Output waves:
Phase correct PWM wave frequency by formula:
Output frequency= Crystal frequency÷(Prescaler ×510)
             = 12000000 ÷ (8×510)
                         = 2941.17 Hz = 2.941KHz
Practically, the output wave form has frequency of 2.942 KHz with 50% duty cycle.
Fast PWM frequency by formula:
Output frequency= Crystal frequency ÷ (Prescaler ×256)
             = 12000000 ÷ (8×256)
                         = 5859.375 Hz = 5.859 KHz
Practically, the output wave has frequency of 5.86 KHz with 50% duty cycle.

This experiment proves that Fast PWM wave has twice the frequency of Phase Correct PWM wave.



Circuit Diagram



This Code is only visible to Registered users. Please Login/Register



ATmega16 AVR Microcontroller
ATmega16 is an 8-bit high performance microcontroller of Atmel’s Mega AVR family with low power consumption. Atmega16 is based on enha...
You are here