Normal PWM setup

Hello all,

Could anyone guide me as to where i can find instructions on how to setup normal PWM, i did learn the analogueWrite() function but what i need is a way to make a PWM where in i have the control on the frequency and the duty cycle, the datasheet didnt seem to have instructions on how to set this up.

Thanks.

It's not in the Atmel datasheets. You can find in the playground ways to change the freq. analogWrite sets the duty.

Why do you want to change the freq?

Mark

I am building a boost converter and want it to operate at 25khz

Again why? or why 25 MHz?

While it could be fun to build the boost converter it is cheaper just to buy one.

Mark

i made calculations for 25khz, it doesnt matter... i can use any frequency,,,... is it really that hard to change the frequency..??,

I found what is called the PWM library in the playground. Could someone please plase check my code and see if it does what the comments say, i dont have an oscilloscope at the moment and theres no way ii know that i can use to see if the code really does output a PWM.

/*

 Aurthor: Open Source :)
 Using the PWM library to generate a 25Khz PWM on pin 9
 
 */

#include <PWM.h>

int pwmPin = 9;
int32_t frequency = 25000; // desired frequency in Hertz

void setup()
{
  //initialize all timers except for 0, to save time keeping functions
  initTimerSafe();//why??? and what is time keeping??

  //sets the frequency for the specified pin i.e 25Khz
  bool success = SetPinFrequencySafe(led, frequency);

  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
  }

}

void loop()
{
  //setting the duty to 50% with 8 bit pwm. 128 is 1/2 of 256
  pwmWrite(led, 128);//loop PWM forever??
}

What you have to remember that only at specific frequencies can you get the full range of duty cycles from the PWM. So while you can set the frequency to what you like the range of duty cycle control is limited.

it part of my school project so, i really dont have a choice.

So they are setting school projects on impossible things now. What school is it Hogwarts?

does the code i wrote change the frequency though?

ozwurld:
i dont have an oscilloscope at the moment and theres no way ii know that i can use to see if the code really does output a PWM.

It seems pointless to go to all the trouble of trying to design your own replacement for a cheap and readily available component without access to the basic tools needed to judge how well it is working. If you're serious about doing the project, get the tools needed to do it properly.