dlloyd:
There is a kind of "ground hog day" thread here. Similar problem (control of frequency), not sure if it was really solved. You might find some information useful.
i found it but it work with arduino due , i used arduino mega2560 that's oki .
int sine[] = { 0x7ff, 0x86a, 0x8d5, 0x93f, 0x9a9, 0xa11, 0xa78, 0xadd, 0xb40, 0xba1,
0xbff, 0xc5a, 0xcb2, 0xd08, 0xd59, 0xda7, 0xdf1, 0xe36, 0xe77, 0xeb4,
0xeec, 0xf1f, 0xf4d, 0xf77, 0xf9a, 0xfb9, 0xfd2, 0xfe5, 0xff3, 0xffc,
0xfff, 0xffc, 0xff3, 0xfe5, 0xfd2, 0xfb9, 0xf9a, 0xf77, 0xf4d, 0xf1f,
0xeec, 0xeb4, 0xe77, 0xe36, 0xdf1, 0xda7, 0xd59, 0xd08, 0xcb2, 0xc5a,
0xbff, 0xba1, 0xb40, 0xadd, 0xa78, 0xa11, 0x9a9, 0x93f, 0x8d5, 0x86a,
0x7ff, 0x794, 0x729, 0x6bf, 0x655, 0x5ed, 0x586, 0x521, 0x4be, 0x45d,
0x3ff, 0x3a4, 0x34c, 0x2f6, 0x2a5, 0x257, 0x20d, 0x1c8, 0x187, 0x14a,
0x112, 0xdf, 0xb1, 0x87, 0x64, 0x45, 0x2c, 0x19, 0xb, 0x2,
0x0, 0x2, 0xb, 0x19, 0x2c, 0x45, 0x64, 0x87, 0xb1, 0xdf,
0x112, 0x14a, 0x187, 0x1c8, 0x20d, 0x257, 0x2a5, 0x2f6, 0x34c, 0x3a4,
0x3ff, 0x45d, 0x4be, 0x521, 0x586, 0x5ed, 0x655, 0x6bf, 0x729, 0x794};
#include "pwm01.h"
#include <math.h>
#include <Arduino.h>
#include <stdio.h>
#include "CalculateDelayValueForSinusFrequency.h"
// Calculate the delay value for the sine wave frequency
long double CalculateDelayValueForSinusFrequency ( int p_sinewave_frequency_Hz)
{
long double DelayForSinFreq_us;
DelayForSinFreq_us=1000000/ (p_sinewave_frequency_Hz*120); // f=1/T
return DelayForSinFreq_us;
}
/*** Programm Variablen ***/
int PhaseU= 7;
int PhaseV= 8;
int PhaseW= 9;
int SineFrequency =50 ; // desierd sine wave frequency in Hz
int p_SinusAmplitudenTeilerFaktor = 1; // default = 1 means 3.3volt =1 = 3.3 volt for the sine wave signal
static int k = sizeof(sine)/sizeof(int)/3; //length of the sine table =120
static int j= k*2; //for the third wave length of the sine table
void setup()
{.
// uint32_t pwm_duty =0.5* 65535;//2^16=65536
uint32_t pwm_freq2 = 15000; // Set a unique frequency to the three output signals #########################
// Set PWM Resolution
pwm_set_resolution(12); // #########################
// Setup PWM Once (Up to two unique frequencies allowed
//-----------------------------------------------------
pwm_setup( PhaseU, pwm_freq2, 2); // Pin 7 freq set to "pwm_freq2" on clock B
pwm_setup( PhaseV, pwm_freq2, 2); // Pin 8 freq set to "pwm_freq2" on clock B
pwm_setup( PhaseW, pwm_freq2, 2); // Pin 9 freq set to "pwm_freq2" on clock B
// Write PWM Duty Cycle Anytime After PWM Setup
//-----------------------------------------------------
// pwm_write_duty( 7, pwm_duty ); //
// delay(30000); // 30sec Delay; PWM signal will still stream
// Force PWM Stop On All Pins
//-----------------------------
// pwm_stop( 7 );
// pwm_stop( 8 );
//pwm_stop( 9 );
}
void loop()
{
while(1){
for(int i = 0; i<120;i++){
pwm_write_duty( 7, sine[i]/p_SinusAmplitudenTeilerFaktor);
pwm_write_duty( 8, sine[(i+k)%120/p_SinusAmplitudenTeilerFaktor]);
pwm_write_duty( 9, sine[(i+j)%120/p_SinusAmplitudenTeilerFaktor]);
delayMicroseconds(CalculateDelayValueForSinusFrequency(SineFrequency));
}
}
}
