Changing the pwm frequency of mega 2560

Hello

I need help to change the pwm frequency of pin 11, but no solution worked for me. I am looking to make it 4 Khz or (whatever number).

#include <AVR_PWM.h>
#include <stdio.h>
#include <stdlib.h>
#include <CarPWMMotorControl.hpp>

int i=0;
int j=0;
int k=0;
int x=0;
int y=0;
int z=0;
int OK=0;
int OK1=0;
int OK2=0;
int OK3=0;
int OK4=0;

float h=0.0;
int duty_cycle,pwm1,read_ADC ;
double duty;
  static int vfbValue;
  static int vfbValue1;

int sinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,44,47,49,52,54,57,59,61,64,66,69,71,73,76,78,80,83,85,88,90,92,94,97,99,
101,103,106,108,110,113,115,117,119,121,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,169,
171,173,175,177,178,180,182,184,185,187,188,190,192,193,195,196,198,199,201,202,204,205,207,208,209,211,212,213,215,216,217,219,220,221,
222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,246,
247,247,247,248,248,248,248,249,249,249,249,249,255,255,255,255,249,249,249,249,249,248,248,248,248,247,247,247,246,246,245,245,
244,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,217,216,
215,213,212,211,209,208,207,205,204,202,201,199,198,196,195,193,192,190,188,187,185,184,182,180,178,177,175,173,171,169,168,166,164,162,160,
158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,121,119,117,115,113,110,108,106,103,101,99,97,94,92,90,88,85,83,80,78,76,
73,71,69,66,64,61,59,57,54,52,49,47,44,42,39,37,34,32,30,27,24,22,19,17,15,12,10,7,5,2,1};

int ledPin = 11; 
int potPin = A0;
int potIn; // variable to store the value coming from the potentiometer
int brightness; // variable to hold the pwm value

// int32_t frequency = 5000; //frequenza in Hz
void setup()
 {
// int32_t frequency = 5000; //frequenza in Hz
  //sets the frequency for the specified pin


 pinMode(ledPin, OUTPUT);
 pinMode(potPin, INPUT);
Serial.begin(9600);
pinMode(4, OUTPUT);    //Pwm T0B pin
pinMode(13,OUTPUT);   //Pwm T0A pin
pinMode(9,OUTPUT);      //Pwm T2B pin
pinMode(10,OUTPUT);   //Pwm T2A pin
pinMode(2,OUTPUT);    //Pwm T3B pin
pinMode(3,OUTPUT);   //Pwm T3C pin
pinMode(5,OUTPUT);  //Pwm T3A pin
pinMode(6,OUTPUT);   //Pwm T4A pin
pinMode(7,OUTPUT);  //Pwm T4B pin


cli();// stop interrupts

/*****************TIMER0************/
TCCR0A=0;//reset the value
TCCR0B=0;//reset the value
TCNT0=0;//reset the value
//0b allow me to write bits in binary
TCCR0A=0b10100001;//phase correct pwm mode
TCCR0B=0b00000001; //no prescaler

/*****************TIMER2************/
TCCR2A=0; //reset the value
TCCR2B=0; //reset the value
TCNT2=0; //reset the value
//0b allow me to write bits in binary
TCCR2A=0b10100001;//phase correct pwm mode
TCCR2B=0b00000001; //no prescaler

/*****************TIMER3************/
TCCR3A=0; //reset the value
TCCR3B=0; //reset the value
TCNT3=0; //reset the value
//0b allow me to write bits in binary
TCCR3A=0b10100001; //phase correct pwm mode
TCCR3B=0b00000001; //no prescaler


/*****************TIMER4************/
TCCR4B = TCCR4B & B11111000 | B00000010;

TCCR1B = TCCR1B & B11111000 | B00000010;  // for PWM frequency of 3921.16 Hz
OCR1A=509;     // compare match value
TCCR1B=0b00001001;    //WGM12 bit is 1 for CTC mode and no prescaler


/*****************TIMER1 INTERRUPT SETTING************/
TIMSK1 |=(1 << OCIE1A);   // enable interrupts
sei();    //enable interrupts
}


/**********************************isr***********************************************/  
ISR(TIMER1_COMPA_vect){// interrupt when timer 1 match with OCR1A value
  
if(i>313 && OK==0){ // final value from vector for pin 13
i=0;    // go to first value of vector
OK=1; //enable pin 4
}
if(i>313 && OK==1){ // final value from vector for pin 4
i=0; //go to firs value of vector
OK=0; //enable pin 13
}
x=sinPWM[i];         // x take the value from vector corresponding to position i(i is zero indexed)
i=i+1; // go to the next position
if(OK==0){
OCR0B=0; //make pin 4 0
OCR0A=x; //enable pin 13 to corresponding duty cycle
}
if(OK==1){
OCR0A=0; //make pin 13 0
OCR0B=x; //enable pin 4 to corresponding duty cycle
}

if ((i==209) || OK1==1){    //if i equal with 209 enable second signal
OK1=1;   //mantain if function
if(j>313 && OK2==0){// final value from vector for pin 10
j=0; // go to first value of vector
OK2=1; //enable pin 9
}
if(j>313 && OK2==1){// final value from vector for pin 9
j=0; //go to firs value of vector
OK2=0; //enable pin 10
}
y= sinPWM[j]; // y take the value from vector corresponding to position j(j is zero indexed)
j=j+1; // go to the next position
if(OK2==0){
OCR2B=0; //make pin 9 0
OCR2A=y; //enable pin 10 to corresponding duty cycle
}
if(OK2==1){
OCR2A=0; //make pin 10 0
OCR2B=y; //enable pin 9 to corresponding duty cycle
}
}

if ((j==209) || OK3==1){//if j equal with 209 enable third signal
OK3=1; //mantain if function
if(k>313 && OK4==0){// final value from vector for pin 5
k=0; // go to first value of vector
OK4=1; //enable pin 2
}
if(k>313 && OK4==1){// final value from vector for pin 2
k=0; //go to firs value of vector
OK4=0; //enable pin 5
}
z= sinPWM[k];// z take the value from vector corresponding to position k(k is zero indexed)
k=k+1; // go to the next position
if(OK4==0){
OCR3B=0; //make pin 2 0
OCR3A=z; //enable pin 5 to corresponding duty cycle
}
if(OK4==1){
OCR3A=0; //make pin 5 0
OCR3B=z; //enable pin 2 to corresponding duty cycle
}
}
// pinMode(11,155);
// TCCR1B = TCCR1B & B11111000 | B00000010;
// int32_t frequency = 5000; //frequenza in Hz
   // setPwmFrequency(11, 8);
// uint32_t  pwm_freq2 = 5000;



}


void loop()
 {
  potIn = analogRead(potPin);
  //mapping the Values between 0 to 255
  brightness = map(potIn, 0, 1023, 0, 255);
  analogWrite(ledPin, brightness);
  	delayMicroseconds(2);
  }
float map1(float x,float in_min,float in_max,float out_min,float out_max){
  return float(x-in_min) * (out_max - out_min) /(in_max-in_min)+out_min;
  }
  

1. What is the range of frequency in Hz?
2. What is the range of duty cycle in %?
3. What is the nature of output -- sine wave or square wave?

  1. Around 4Khz
  2. It's adjustable through potentiometer
  3. Square wave for this pin

Configure TC1 in Mode-14 operation to generate 4 kHz single slope Fast PWM signal at DPin-11 of MEGA.
Sketch: (untested but compiled on MEGA Board)

#define OC1A 11

void setup()
{
  Serial.begin(9600);
  pinMode(OC1A, OUTPUT); //Ch-A
  //-------------------------------
  TCCR1A = 0x00;   //reset
  TCCR1B = 0x00;   //TC1 reset and OFF
  //fOC1A = clckSys/(N*(1+ICR1)); Mode-14 FPWM; OCR1A controls duty cycle
  // 4000 Hz = 16000000/(8*(1+ICR1) N = 8 ==> ICR1 = 499
  TCCR1A |= (1 << WGM11); //Mode-14 Fast PWM
  TCCR1B |= (1 << WGM13) | (1 << WGM12);    //Mode-14 Fast PWM
  TCCR1A |= (1 << COM1A1) | (0 << COM1A0);  //Non-invert: HIGH-LOW
  ICR1 = 499;                               // TOP for 4000 Hz frequnecy
  OCR1A = 250;                              //= 50% duty cycle
  TCNT1 = 0;
  TCCR1B |= (1 << CS11) | (1 << CS10); //TC1 starts with N = 8;
}

void loop()
{
  //connect 5 k pot with A0-pin to regulate duty cycle
  OCR1A = map(analogRead(A0), 0, 1023, 0, 500);
  delay(1000); //test interval
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.