Sinusoidal PWM(SPWM) using AVR interrupt.

Hi all,

I’m basically working on programming for generating Sinusoidal PWM(SPWM) and simple PWM output using Arduino Uno ATMEGA 328p. To be honest, i’m new in Arduino and have little knowledge in programming. My program is mostly taken from another and not totally done by me from scratch.

My SPWM code is basically based on this link:

My objective is to produce PWM output like figure below:

For the past few week, i have been working on this and able to produce the required PWM. Figure below shows the generated PWM using Proteus:

My problem is, when running the program after a few minutes, the square waves generated start to shifted from the filtered SPWM. FYI, the square wave is running on 50 Hz freq and the filtered SPWM is producing 100 Hz. Figure below shows the square waves are shifted when running after a few minutes:

Based on my opinion, this problem occur due to timer1 and timer2 are not oscillating in synchronize and delayed with few nano seconds. Below is my code:

* DDS Sine Generator using Arduino Uno ATMEGA328P

#include "avr/pgmspace.h"

// table of 256 sine values / half sine period / stored in flash memory
PROGMEM  prog_uchar sine256[]  = {

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

double dfreq;
// const double refclk=320000;    // =16MHz / 50Hz
const double refclk=312500;      // measured

// variables used inside interrupt service declared as voilatile
volatile byte icnt;              // var inside interrupt
volatile byte icnt1;             // var inside interrupt
volatile byte c4ms;              // counter increment
volatile unsigned long phaccu;   // phase accumulator
volatile unsigned long tword_m;  // dds tuning word m
boolean toggle1 = 0;

void setup()
  pinMode(9, OUTPUT);      // sets the digital pin as output
  pinMode(10, OUTPUT);      // sets the digital pin as output
  pinMode(11, OUTPUT);     // pin11= PWM  output / frequency output

  cli();                   //stop interrupts
  //set timer1 interrupt at 100Hz
  TCCR1A = 0;              // set entire TCCR1A register to 0
  TCCR1B = 0;              // same for TCCR1B
  TCNT1  = 0;              //initialize counter value to 0
  // set compare match register for 100hz increments
  OCR1A = 155.25;            // = (16*10^6) / (100*1024) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12 and CS10 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);  
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  sei();//allow interrupts

  // disable interrupts to avoid timing distortion
  cbi (TIMSK0,TOIE0);              // disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              // enable Timer2 Interrupt

  dfreq=1000.0;                    // initial output frequency = 1000.o Hz
  tword_m=pow(2,32)*dfreq/refclk;  // calulate DDS new tuning word 
void loop()
  while(1) {
     if (c4ms > 256) {                 // timer / wait fou a full second

      cbi (TIMSK2,TOIE2);              // disable Timer2 Interrupt
      tword_m=pow(2,32)*dfreq/refclk;  // calulate DDS new tuning word
      sbi (TIMSK2,TOIE2);              // enable Timer2 Interrupt 
// timer1 setup
void Setup_timer1(){

// timer2 setup
// set prscaler to 1, PWM mode to phase correct PWM,  16000000/510 = 31372.55 Hz clock
void Setup_timer2() {

// Timer2 Clock Prescaler to : 1
  sbi (TCCR2B, CS20);
  cbi (TCCR2B, CS21);
  cbi (TCCR2B, CS22);

  // Timer2 PWM Mode set to Phase Correct PWM
  cbi (TCCR2A, COM2A0);  // clear Compare Match
  sbi (TCCR2A, COM2A1);

  sbi (TCCR2A, WGM20);  // Mode 1  / Phase Correct PWM
  cbi (TCCR2A, WGM21);
  cbi (TCCR2B, WGM22);

// Timer1 Interrupt Service at 100 Hz
//generates pulse wave of frequency 100Hz/2 = 50Hz (takes two cycles for full wave- toggle high then toggle low)
ISR(TIMER1_COMPA_vect){   //timer1 interrupt 100Hz toggles pin 9,10

  if (toggle1){
    toggle1= 1;

// Timer2 Interrupt Service at 320 KHz = 3.125uSec
// this is the timebase REFCLOCK for the DDS generator
// FOUT = (M (REFCLK)) / (2 exp 32)
ISR(TIMER2_OVF_vect) {

  phaccu=phaccu+tword_m; // soft DDS, phase accu with 32 bits
  icnt=phaccu >> 24;     // use upper 8 bits for phase accu as frequency information
                         // read value fron ROM sine table and send to PWM DAC
  OCR2A=pgm_read_byte_near(sine256 + icnt);    

  if(icnt1++ == 1) {  // increment variable c4ms


I’m hoping that someone could help me to solve this problem or at least give me some idea on how to solve this. TQ ;D

First thing that strikes me:

  OCR1A = 155.25;            // = (16*10^6) / (100*1024) - 1 (must be <65536)

You don’t have decimal places in the timer fields (they are integers). So it won’t be 155.25. It will be 155.


That is a slow way of doing a bit shift.

Well, i guess that is the problem. since the program only read 155, it cannot oscillate 100 Hz accurately, but instead 100.16 Hz.

i’m thinking on something that can phase lock the square wave and the filtered SPWM together but i dont know where to start.