Close

Timers and edges

A project log for 2-Phase PWM with ATMEGA328P

Make 2-phase PWM to drive a transformer power supply circuit

bobubobu 09/11/2021 at 19:090 Comments

ATMEGA328P is the part that is found on inexpensive and generic Arduino-compatible boards.  ATMEGA328P is an older part but it is well supported in Arduino world.  It's what I've got.  If this was a commercial project, it would be better to use ATMEGA328PB or newer part in the ATmega family.  With '328P, there are 3 timers, 0, 1 and 2.  Each has unique features.  Timer1 is 16 bit and has most PWM features, so let's start with that one.  Timer0 is 8 bit and is used by Arduino code for delay() and millis().  Those functions are important, so don't use timer0.  Timer2 is also 8 bit and is also used by some functions.  Timer1 is used by servo libraries.  This project won't use servo PWM, so that should work.

Looking at the PWM timing diagram, there are 4 edges to generate.  2 rising and 2 falling edges.  This is a challenge because timer1 has 2 output compare modules.  Most of the PWM modes change state when the timer reaches an output compare match.  The PWM output then resets at the TOP count.  So how can we generate 2 PWM pulses with 180 degree phase shift?

This project will focus on the phase correct method.  Maybe the other methods can be tried later.

Discussions