Greetings to all : )
I need to generate 3 phase SPWM using arduino where I need 6 output waveforms from Arduino, 3 waveforms of 3 phase 120 degree shift and and other 3 complement of those. also i need switching frequency of 25kHz Im using the code given below. also I made some changes in sine lookup table.
#include "avr/pgmspace.h"
#include "avr/io.h"
const byte sine256[] PROGMEM={128, 131, 134, 137, 140, 143, 146, 149, 152, 156, 159, 162, 165, 168, 171, 174, 176, 179, 182, 185,
188, 191, 193, 196, 199, 201, 204, 206, 209, 211, 213, 216, 218, 220, 222, 224, 226, 228, 230, 232,
234, 235, 237, 239, 240, 242, 243, 244, 246, 247, 248, 249, 250, 251, 251, 252, 253, 253, 254, 254,
254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 253, 252, 252, 251, 250, 249, 248, 247, 246,
245, 244, 242, 241, 239, 238, 236, 235, 233, 231, 229, 227, 225, 223, 221, 219, 217, 215, 212, 210,
207, 205, 202, 200, 197, 195, 192, 189, 186, 184, 181, 178, 175, 172, 169, 166, 163, 160, 157, 154,
151, 148, 145, 142, 138, 135, 132, 129, 126, 123, 120, 117, 113, 110, 107, 104, 101, 98, 95, 92,
89, 86, 83, 80, 77, 74, 71, 69, 66, 63, 60, 58, 55, 53, 50, 48, 45, 43, 40, 38,
36, 34, 32, 30, 28, 26, 24, 22, 20, 19, 17, 16, 14, 13, 11, 10, 9, 8, 7, 6,
5, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2,
3, 4, 4, 5, 6, 7, 8, 9, 11, 12, 13, 15, 16, 18, 20, 21, 23, 25, 27, 29,
31, 33, 35, 37, 39, 42, 44, 46, 49, 51, 54, 56, 59, 62, 64, 67, 70, 73, 76, 79,
81, 84, 87, 90, 93, 96, 99, 103, 106, 109, 112, 115, 118, 121, 124
};
#define cbi(sfr,bit) (_SFR_BYTE(sfr) &=~_BV(bit))
#define sbi(sfr,bit) (_SFR_BYTE(sfr) != _BV(bit))
int PWM_OUT_1=11; // PWM output on pin 11
int PWM_OUT_2=10; // PWM output on pin 10
int PWM_OUT_3=9; // PWM output on pin 9
int LED_PIN=13; //LED STATUS ON PIN 13
int TEST_PIN=7; // Scope trigger on pin 7
int POTEN_IN = A0; //Potentiometer on pin 0
int OFFSET_1=85; // Offset for second phase
int OFFSET_2=170; //Offset for third phase
double dfreq;
const double refclk = 31376.6; // measured
const uint64_t twoTo32 = pow(2,32); // compute value at startup and use as constant
// Variables used inside interrupt services are declared as volatile
volatile uint8_t icnt; //var inside interrupt
volatile uint8_t icnt1; //var inside interrupt
volatile uint8_t c4ms; //counter increamented every 4ms
volatile uint32_t phase_accum; //phase accumulator
volatile uint32_t tword_m; // dds tunning word m
//***********************************************************************************
void setup() {
// put your setup code here, to run once:
pinMode (LED_PIN, OUTPUT); // sets the digital pin as output
Serial.begin(115200); // connect to the serial port
Serial.println("DDS Test");
pinMode (TEST_PIN, OUTPUT); // sets the digital pin as output
pinMode (PWM_OUT_1, OUTPUT); // PWM output frequency output
pinMode (PWM_OUT_2, OUTPUT); // PWM output frequency output
pinMode (PWM_OUT_3, OUTPUT); // PWM output frequency output
// Setup the timers
setup_timerl();
setup_timer2();
// disable interrupts to avoid timing distortion
cbi (TIMSK0, TOIE0); // disable Timező !!! delay() is now not available
sbi (TIMSK2, TOIE2); // enable Timer2 Interrupt
dfreq = 1000.0; // initial output frequency 1000.0 Hz
tword_m = twoTo32 * dfreq/refclk; //calculate DDSnew tuning word
}
void loop()
{
// put your main code here, to run repeatedly:
if (c4ms > 250) // timer / wait for a full second
{
c4ms > 0;
dfreq = analogRead (POTEN_IN); // read Poti on analog pin 0 to adjust output frequency from 0..1023 Hz
cbi (TIMSK2, TOIE2); // disble Timer2 Interrupt
tword_m = twoTo32 * dfreq/refclk; // calulate DDS new tuning word
sbi (TIMSK2, TOIE2); // enable Timer2 Interrupt
Serial.print (dfreq);
Serial.print(" ");
Serial.println(tword_m);
}
}
//*************
// timerl setup
// set prscaler to 1, PWM mode to phase correct PWM, 16000000/512 = 31.25kHz clock
void setup_timerl (void){
// Timerl Clock Prescaler to: 1
sbi (TCCR1B, CS10);
cbi (TCCR1B, CS11);
cbi (TCCR1B, CS12);
// Timer 0 PWM mode set to Phase Correct PWM
cbi (TCCR1A, COM1A0); // clear Compare Match
sbi (TCCR1A, COM1A1);
cbi (TCCR1A, COM1A0); // clear Compare Match
sbi (TCCR1A, COM1B1);
sbi (TCCR1A, WGM10); // Mode 1 Phase Correct PWM
cbi (TCCR1A, WGM11);
cbi (TCCR1B, WGM12);
cbi (TCCR1B, WGM13);
}
//timer2 setup
//set prscaler to 1, PWM mode to phase correct PWM, 16000000/512=31.25kHz 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 PM
cbi (TCCR2A, COM2A0); // clear Compare Match
sbi (TCCR2A, COM2A1);
sbi (TCCR2A, WGM20); // Mode 1 Phase Correct PWM
cbi (TCCR2A, WGM21);
cbi (TCCR2B, WGM22);
}
// Timer2 Interrupt Service at 31.25kHz=32us
// this is timebase REFCLOCK for the DDS generator
// FOUT (M (REFCLK))/ (2 exp 32)
// runtime: 8 microseconds (inclusive push and pop)
ISR (TIMER2_OVF_vect)
{
sbi (PORTD, TEST_PIN); // Test / set PORTD, TEST_PIN high to observe timing with a oscope
phase_accum += tword_m; // soft DDS, phase accu with 32 bits
icnt = phase_accum >> 24; // use upper 8 bits for phase accu an frequency information OCR2A pgm read byte near (sine256+ icnt); // read value fron ROM sine table and send to PWM DAC OCRIA pgm read_byte_near(sine256+ (uintet) (icnt OFFSET_1)): OCRIB pgm read byte near (sine256+ (uintet) (icnt + OFFSET_2)); if (icnti+125) // increment variable cama every 4 milliseconda
OCR2A=pgm_read_byte_near(sine256+icnt);//read value from ROM sine table and send to PWM DAC
OCR1A=pgm_read_byte_near(sine256+(uint8_t)(icnt+OFFSET_1));
OCR1B=pgm_read_byte_near(sine256+(uint8_t)(icnt+OFFSET_2));
if(icnt1++ ==125)// increament variable c4ms every 4 milliseconds
{
c4ms++;
icnt1=0;
}
cbi (PORTD, TEST_PIN); // reset PORTD, TEST_PIN
}
I am not getting desired output.
Output waveforms im expecting is attach below where it is controllable frequency using varying potentiometer
my proteus simulation is also given below.
I referred one Youtube Video for this code and simulation. im attaching link below.
please guide me.
I will be very Thankful for your help : )