Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to use fast PWM (Pulse Width Modulation) Mode of AVR microcontroller Timer- (Part 18/46)

By Ashutosh Bhatt July 7, 2010

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.

 

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.
 
COM 0 Bit Values and Description in AVR
Fig. 2: COM 0 Bit Values and Description in AVR
 
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.

 

Output Waveform Comparison of Fast PWM and Phase Correct PWM waves in AVR

Fig. 3: Output Waveform Comparison of Fast PWM and Phase Correct PWM waves in AVR

Project Source Code

###


// Program  to use fast PWM (Pulse Width Modulation) Mode of AVR microcontroller Timer
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
 
#define FREQ 12000000
#define duty_cycle 50 // duty cycle require
#define prescaler 8
 
#define OCR_value ((duty_cycle*256)/100)  //OCR value calculation
 
void t0_pwm_init(void);
void t2_fastpwm_init(void);
 
int main()
{
t0_pwm_init();  
t2_fastpwm_init();
sei();
while(1);
}
 
void t0_pwm_init() // initialization for Phase Correct PWM signal using timer 0
{
// WGM0[1:0]= 01, for Phase Correct PWM mode
// COM0[1:0]= 10, to select non inveting mode
// CS0[2:0] =010. for prescaler=8
 
TCCR0=(1<<WGM00)|(2<<COM00)|(2<<CS00);
DDRB|=(1<<PB3); // selcet OC0 as output pin
TIMSK|=(1<<OCIE0); //enable Output compare interrupt
}
 
void t2_fastpwm_init() // initialization for Phase Correct PWM signal using timer 2
{
// WGM2[1:0]= 11, for Fast PWM mode
// COM2[1:0]= 10, to select non inveting mode
// CS2[2:0] =010. for prescaler=8
 
TCCR2=(1<<WGM20)|(1<<WGM21)|(2<<COM20)|(2<<CS20);
DDRD|=(1<<PD7); // selcet OC2 as output pin
TIMSK|=(1<<OCIE2); //enable Output compare interrupt
}
 
ISR(TIMER0_COMP_vect) // interrupt subroutine
{
OCR0=OCR_value; // put OCR value
}
 
ISR(TIMER2_COMP_vect) // interrupt subroutine
{
OCR2=OCR_value; // put OCR value
}
 

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-use-fast-PWM-Pulse-Width-Modulation-Mode-of-AVR-microcontroller-Time

Project Components

  • ATmega16

Project Video


Filed Under: AVR
Tagged With: atmega16, avr, microcontroller, pwm, timer
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe