Go Down

Topic: SOLVED How to generate SINE PWM SOLVED (Read 66275 times) previous topic - next topic

dlloyd

#75
Mar 22, 2014, 06:36 pm Last Edit: Mar 22, 2014, 06:51 pm by dlloyd Reason: 1
Not 32 bit (4,294,967,296) ... try 10 bit (1024).  Really, 1050 should be the max value that's written to pwm_write_duty.

Go back to basics ... just generate 1 waveform ... use delayMicroseconds() for better frequency control.

Or, as a test, you could set your PWM frequency to 15000 and the resolution to 12 bit, then use the sine waveform (table) of values from here: http://arduino.cc/en/Tutorial/DueSimpleWaveformGenerator#.Uy3IjmRzboY

so3ody

Hi so I added the new sine wave table from the example. But I still have the same problem as I mentioed before

when I change the PWM frequency it desent change the frequency of my filterd sine wave..they dosent affect each other


Code: [Select]

#include "pwm01.h"



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};

static int k = sizeof(sine)/sizeof(int)/3; //length of the sine table =51
static int j= k*2; //for the third wave length of the sine table
//static int test= sizeof(sine)/sizeof(int) ;



void setup()
{
   // uint32_t  pwm_duty =0.5* 65535;//2^16=65536
    uint32_t  pwm_freq2 = 30000; // Set a unique frequency to the three output signals

    // Set PWM Resolution
    pwm_set_resolution(16);  //

    // Setup PWM Once (Up to two unique frequencies allowed
    //-----------------------------------------------------   

    pwm_setup( 7, pwm_freq2, 2);  // Pin 7 freq set to "pwm_freq2" on clock B
    pwm_setup( 8, pwm_freq2, 2);  // Pin 8 freq set to "pwm_freq2" on clock B
    pwm_setup( 9, 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 );  // 75% duty cycle on Pin 7


   // delay(30000);  // 30sec Delay; PWM signal will still stream
       
    // Force PWM Stop On All Pins
    //-----------------------------   

/* pwm_stop( 6 );
    pwm_stop( 7 );
    pwm_stop( 8 );
    pwm_stop( 9 );*/
}

void loop()

  while(1){
   for(int i = 0; i<50;i++){
    pwm_write_duty( 7, sine[i]*16 );  // 16 is a factor to improve the resoultion of the sine wave
    pwm_write_duty( 8, sine[(i+k)%51]*16);
   
    delay(5); // The sine wave frequency is determinded in this way Sine index = 51*10 milli seconds delay= 510, Frequncy=1/510
   
     }
     }
 
}




dlloyd

#77
Mar 22, 2014, 08:18 pm Last Edit: Mar 22, 2014, 10:22 pm by dlloyd Reason: 1
Quote
Hi so I added the new sine wave table from the example. But I still have the same problem as I mentioed before when I change the PWM frequency it desent change the frequency of my filterd sine wave..they dosent affect each other


You're right!  It never will. The PWM frequency is only the "carrier" frequency for the "modulated" sine wave. One way you can change the sine wave frequency is to update the duty cycle at a faster or slower rate.

If you're controlling a motor, another way to control speed is to scale the values (x 0.01 to 1.00) for 1 to 100% coarse speed control (this changes the sine amplitude).  You could also vary the supply voltage with another PWM output for fine resolution speed control (by changing power supply amplitude). Note that some VFDs have a sine output frequency range of around 0.1 to 600 Hz.

In your code below, there is some minor changes marked with ########.
Try to determine why those changes were made.

If you set delayMicroseconds to 8000, you will get about 1 Hz sine wave on all 3 outputs.
If you set delayMicroseconds to 8, you will get about 670 Hz sine wave on all 3 outputs.

You're still at the tip of this iceberg and I'm slipping off (I need to return to my iceberg) ... good luck!

Code: [Select]

#include "pwm01.h"

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};

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
//static int test= sizeof(sine)/sizeof(int) ;



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( 7, pwm_freq2, 2);  // Pin 7 freq set to "pwm_freq2" on clock B
   pwm_setup( 8, pwm_freq2, 2);  // Pin 8 freq set to "pwm_freq2" on clock B
   pwm_setup( 9, 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 );  // 75% duty cycle on Pin 7


  // delay(30000);  // 30sec Delay; PWM signal will still stream
       
   // Force PWM Stop On All Pins
   //-----------------------------    

/* pwm_stop( 6 );
   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] );  //#########################
   pwm_write_duty( 8, sine[(i+k)%120]); //#########################
   pwm_write_duty( 9, sine[(i+j)%120]); //#########################
   
   delayMicroseconds(8000); // The sine wave frequency is determinded in this way Sine index = 51*10 milli seconds delay= 510, Frequncy=1/510 #########################
 
    }
    }
 
}

so3ody

Thanks alot man for your help :)

I know that im still on the beginning but am I on the right way devolping a Varible frequency drive ?

so3ody

#79
Mar 31, 2014, 10:26 pm Last Edit: Mar 31, 2014, 10:31 pm by so3ody Reason: 1
Hi there

My code genrates 3 sine waves throught varying the duty cycle of 3 pwm signals. I have a fixed pwm frequency = 15 khz
and I change the sine wave frequency in the void  loop throught delay micro seconds

Can I chnage it in a better way ?  Im sorry if my question sounds stupid  but im new in programming uc


Code: [Select]

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));

}
}

}



This is my header file to calculate the delay value for the sine wave frequency
Code: [Select]
/*

#ifndef CalculateDelayValueForSinusFrequency_H
#define CalculateDelayValueForSinusFrequency_H

#include "Arduino.h"

long double CalculateDelayValueForSinusFrequency ( int sinewave_frequency);

#endif



the pwm01.h header 
Code: [Select]
/* 

Library:     pwm01.h (version 01)
Date:        2/11/2013
Written By:  randomvibe 

Purpose:     
   Setup one or two unique PWM frequenices directly in sketch program,
   set PWM duty cycle, and stop PWM function.

User Functions:     
   pwm_set_resolution(int res) ~  setup PWM resolution; up to 16 bit
   pwm_setup( uint32_t pwm_pin, uint32_t pwm_freq, int iclock) ~ Setup PWM on clock-A (iclock=1) or clock-B (iclock-2)
   pwm_write_duty( uint32_t pwm_pin, uint32_t pwm_duty) ~ Write PWM duty cycle
   pwm_stop( uint32_t pwm_pin) ~ Force PWM stop

Notes:
   - Applies to Arduino-Due board, PWM pins 6, 7, 8 & 9.
   - Libary Does not operate on the TIO pins.
   - Unique frequencies set via PWM Clock-A ("CLKA") and Clock-B ("CLKB")
     Therefore, up to two unique frequencies allowed.
   - Set max duty cycle counts (pwm_max_duty_Ncount) equal to 255
     per Arduino approach.  This value is best SUITED for low frequency
     applications (2hz to 40,000hz) such as PWM motor drivers,
     38khz infrared transmitters, etc.
   - Future library versions will address high frequency applications.
   - Arduino's "wiring_analog.c" function was very helpful in this effort.
     
*/

#include "Arduino.h"


static int       pwm_resolution_nbit = 8;
static uint32_t  pwm_clockA_freq = 0;
static uint32_t  pwm_clockB_freq = 0;
static uint32_t  pwm_max_duty_Ncount = 255;


void pwm_set_resolution(int res)
{
pwm_resolution_nbit = res;
}


void pwm_set_clockA_freq(uint32_t  val)
{
pwm_clockA_freq = val;
}


void pwm_set_clockB_freq(uint32_t  val)
{
pwm_clockB_freq = val;
}


static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to)
{
    if (from == to)
        return value;
    if (from > to)
        return value >> (from-to);
    else
        return value << (to-from);
}



// MAIN PWM INITIALIZATION
//--------------------------------
void  pwm_setup( uint32_t  pwm_pin,  uint32_t  pwm_freq,  int iclock  )
{
    uint32_t  pwm_duty = 0;   
    uint32_t  chan = g_APinDescription[pwm_pin].ulPWMChannel;
     
    // SET CLOCK FREQUENCY
    //------------------------
    if (iclock==1)  pwm_set_clockA_freq( pwm_max_duty_Ncount*pwm_freq );
    if (iclock==2)  pwm_set_clockB_freq( pwm_max_duty_Ncount*pwm_freq );


    if (pwm_pin>=6 && pwm_pin<=9)
    {   
        // SET PWM RESOLUTION
        //------------------------
        pwm_duty = mapResolution( pwm_duty, pwm_resolution_nbit, PWM_RESOLUTION);
   
        // PWM STARTUP AND SETUP CLOCK
        //-------------------------------
        pmc_enable_periph_clk( PWM_INTERFACE_ID );
        PWMC_ConfigureClocks( pwm_clockA_freq, pwm_clockB_freq, VARIANT_MCK );   
     
        // SETUP PWM FOR pwm_pin
        //------------------------
        PIO_Configure( g_APinDescription[pwm_pin].pPort,  g_APinDescription[pwm_pin].ulPinType,  g_APinDescription[pwm_pin].ulPin,  g_APinDescription[pwm_pin].ulPinConfiguration);
        if (iclock==1)  PWMC_ConfigureChannel(PWM_INTERFACE, chan, PWM_CMR_CPRE_CLKA, 0, 0);
        if (iclock==2)  PWMC_ConfigureChannel(PWM_INTERFACE, chan, PWM_CMR_CPRE_CLKB, 0, 0);
        PWMC_SetPeriod(PWM_INTERFACE, chan, pwm_max_duty_Ncount);
        PWMC_SetDutyCycle(PWM_INTERFACE, chan, pwm_duty);
        PWMC_EnableChannel(PWM_INTERFACE, chan);
    }

}



// WRITE DUTY CYCLE
//--------------------------------
void  pwm_write_duty( uint32_t  pwm_pin,  uint32_t  pwm_duty )
{
    if (pwm_pin>=6 && pwm_pin<=9)
    {   
        pwm_duty = mapResolution( pwm_duty, pwm_resolution_nbit, PWM_RESOLUTION);
       
        uint32_t  chan = g_APinDescription[pwm_pin].ulPWMChannel;
       
        PWMC_SetDutyCycle(PWM_INTERFACE, chan, pwm_duty);
    }
};



// FORCE PWM STOP
//--------------------------------
void  pwm_stop( uint32_t  pwm_pin )
{
    pinMode(pwm_pin, OUTPUT);      // sets the digital pin as output
    digitalWrite(pwm_pin, LOW);    // sets the LED off
};


Coding Badly


I assume the post above belongs on this thread.  If not, please let me know.  Good luck to all!

so3ody


adelo14

i have a projet to to generate 3 PWM 120 degrees out of phase with arduino mega2560 because i want to control six pluse igbt to control 3 phase motor .
i read  the code but  i have problems with :
Code: [Select]

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;}

p_SinusAmplitudenTeilerFaktor = 1; ???????????????

// Set PWM Resolution ?????????????????
pwm_set_resolution(12);


Grumpy_Mike

#83
May 12, 2014, 09:07 am Last Edit: May 12, 2014, 09:11 am by Grumpy_Mike Reason: 1
Why does the title of this thread say solved when it clearly is not.
What is the problem you are having with that bit of code?

The proper way to change the frequency is to have a non interger look up table pointer and increment it in a non interger way. Then just use the interger portion to look up the sample.
Also why not have a loop in the setup function to generate the values in your sin look up table?

adelo14


Why does the title of this thread say solved when it clearly is not.
What is the problem you are having with that bit of code?

The proper way to change the frequency is to have a non interger look up table pointer and increment it in a non interger way. Then just use the interger portion to look up the sample.
Also why not have a loop in the setup function to generate the values in your sin look up table?


thanks  Grumpy_Mike can you give me some help here : http://forum.arduino.cc/index.php?topic=236778.msg1718939#msg1718939

Mausinho

Hello,

Anyone knows if at the end someone was "converted" that code for arduino uno or due?? I tried to do, using libraries but doesn´t work

jeanmcqui

i was trying to make a single phase inverter, and got a code from a friend

#include "avr/pgmspace.h"

#define PEAK1 195
#define PEAK2 100
#define PEAK3 67

#define HALF_DELAY1 8
#define HALF_MICRO_DELAY1 772
#define HALF_DELAY2 10
#define HALF_MICRO_DELAY2 0
#define HALF_DELAY3 11
#define HALF_MICRO_DELAY3 111

#define LED1 1
#define LED2 8
#define LED3 13

#define PWM_OUT1_P 3
#define PWM_OUT1_N 9

#define SWITCH1 7
#define SWITCH2 4
#define SWITCH3 2

//uint8_t icnt = 0;
boolean buttonOne = false;
boolean buttonTwo = false;
boolean buttonThree = false;

uint8_t running = 1;

uint8_t peakToUse = PEAK1;
int halfDelayToUse = HALF_DELAY1;
int halfMicroDelayToUse = HALF_MICRO_DELAY1;

void setup() {
  //Serial.begin(9600);
  pinMode(PWM_OUT1_P, OUTPUT);
  pinMode(PWM_OUT1_N, OUTPUT);
  pinMode(SWITCH1, INPUT);
  pinMode(SWITCH2, INPUT);
  pinMode(SWITCH3, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
 
  //31250 Hz
  /*
  setPwmFrequency(3, 1);
  setPwmFrequency(9, 1);
  setPwmFrequency(10, 1);
  setPwmFrequency(11, 1);
  //62500 Hz
  setPwmFrequency(5, 1);
  setPwmFrequency(6, 1);
  */
}

void loop() {
  setRunningState();
  setButtonStates();

  analogWrite(PWM_OUT1_P, peakToUse);
  analogWrite(PWM_OUT1_N, 0);
  delay(halfDelayToUse);
  delayMicroseconds(halfMicroDelayToUse);
  analogWrite(PWM_OUT1_P, 0);
  analogWrite(PWM_OUT1_N, peakToUse);
  delay(halfDelayToUse);
  delayMicroseconds(halfMicroDelayToUse); 
}

void setRunningState() {
  if ((buttonOne == false) && (digitalRead(SWITCH1) == HIGH)) {
    running = 1;
    peakToUse = PEAK1;
    halfDelayToUse = HALF_DELAY1;
    halfMicroDelayToUse = HALF_MICRO_DELAY1;
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
  } else if ((buttonTwo == false) && (digitalRead(SWITCH2) == HIGH)) {
    running = 2;
    peakToUse = PEAK2;
    halfDelayToUse = HALF_DELAY2;
    halfMicroDelayToUse = HALF_MICRO_DELAY2;
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
  } else if ((buttonThree == false) && (digitalRead(SWITCH3) == HIGH)) {
    running = 3;
    peakToUse = PEAK3;
    halfDelayToUse = HALF_DELAY3;
    halfMicroDelayToUse = HALF_MICRO_DELAY3;
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
  }
}

void setButtonStates() {
  if (digitalRead(SWITCH1) == HIGH) {
    buttonOne = true;
  } else {
    buttonOne = false;
  }
  if (digitalRead(SWITCH2) == HIGH) {
    buttonTwo = true;
  } else {
    buttonTwo = false;
  }
  if (digitalRead(SWITCH3) == HIGH) {
    buttonThree = true;
  } else {
    buttonThree = false;
  }
}

void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x7; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}




but unfortunately, the wave formed in the oscilloscope, did not meet the expected output of a modified sine wave. Its frequency was 500 hz, when what we expected were: 57hz, 50hz, and 45hz (btw, we were trying to change the speed of a motor, such is why we had 3 options.)

can anyone tell me how to have a fixed freq for an output in every option? thank you so much

Go Up