PWM @ 200kHz in Mega2560 ????

Guys, I need to get PWM at 200kHz from a mega 2560 and a mega 328.

i also need to keep the mills() function properly working. Thus OC0 I think should be kept unchanged.

Then OC1 can be reconfigured (scaled) for the 200kHz. I searched in web and found a code, that i implemented like this:

const int PWM_pin = 8;

void setup() {
   TCCR2A = 0x23;
   TCCR2B = 0x09;
   OCR2A = 159;
   pinMode(PWM_pin, OUTPUT);
   Serial.begin(9600);
  }

void loop() {
  //while (Serial.available() > 0) {
    int duty = (255*(Serial.parseInt()))/100;  
  // iterate over the pins:
  //analogWrite(PWM_pin, duty);
  analogWrite(PWM_pin, 127);

  //}
}

Yes, I know I should change it for OC1A right?? Now can you exactly guide me in this regard?? Also any more higher frequency can I make????

Mishu~

Guys, I got this wonderful library for PWM from google codes. (forgot the link, but it's available after a few search).

Now, I have installed it accordingly, the way Arduino suggests... then it shows me the error of this:

PWM_Test.ino: In function 'void setup()':
PWM_Test:25: error: 'InitTimersSafe' was not declared in this scope
PWM_Test:28: error: 'SetPinFrequencySafe' was not declared in this scope
PWM_Test.ino: In function 'void loop()':
PWM_Test:45: error: 'pwmWrite' was not declared in this scope

Now, here is my code:

/*

 Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker 
 because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when 
 the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library 
 allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When 
 SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the 
 frequency was actually changed.
 
 This example runs on mega and uno.
 */

#include <PWM.h>

//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 11;                // the pin that the LED is attached to

int32_t frequency = 200000; //frequency (in Hz)

int duty = 0;

void setup()
{
  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 

  //sets the frequency for the specified pin
  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()
{
  
  while (Serial.available() > 0) {
    
    duty = (255*Serial.parseInt())/100;
  
    //use this functions instead of analogWrite on 'initialized' pins
    pwmWrite(led, duty);

  }      
}

The library is at PWM frequency library - Libraries - Arduino Forum and I think it's a installation issue... any guidelines??

I'm using mega2560 board.

So you're saying that you have the PWM library folder placed in your Arduino library folder?

Because those error messages indicate otherwise.

The following "compiles", successfully, even though there is not a library called "dreck", the IDE doesn't result a "no such library found" response:

#include <dreck.h>
byte foam;

void setup ()
{  }

void loop ()
{  }

PWM_Test.ino: In function 'void setup()':
PWM_Test:25: error: 'InitTimersSafe' was not declared in this scope
PWM_Test:28: error: 'SetPinFrequencySafe' was not declared in this scope
PWM_Test.ino: In function 'void loop()':
PWM_Test:45: error: 'pwmWrite' was not declared in this scope

this is the error during compile

You do not, evidently, have the PWM folder placed in your Arduino library folder.
I told you that basically last time, without being blunt.

If it's not where it should be then you will get exactly those error messages with each call of a PWM.h-related function (every single time.)

In my computer the PWM folder is placed thus --
d:\Arduino104\arduino-1.0.4\libraries

(You may have to re-start the IDE after placing the library in the libraries folder.)

Why do you want 200kHz pwm? it's not trivial to do switching at such a high frequency. The only application i can think of is a class D (audio) amplifier but i think you would be better off with sigma/delta than pwm

guys... it's working... i had to copy files manually as the supply did not dropped perfectly..

And now all i need to know is how exactly can i use ONLY one timer (1-n) except 0. Plus a way to ON or OFF the PWM on demand from the code... need research though...

I need PWM as i need to make a buck converter for a arduino based battery charger...

But such a high frequency? There is very little to be gained once the switching is out of the audio spectrum. Except for smaller inductors. Most converters work between 30kHz and 50kHz

yepp... inductor issue...

many buck converter uses 250khz...

and i need a 500khz CW transmitter... thus i was also thinking of carrier @ 500khz

500kHz converters using GaN MOSFETs mean inductors and capacitors can be
a lot smaller for high power levels (I doubt you are using GaN's though)! EPC
do some development boards for these new GaN devices.

If you are wanting to generate a carrier wave you probably need to replace the
ceramic resonator with a quartz crystal so you get a stable frequency.

MarkT:
500kHz converters using GaN MOSFETs mean inductors and capacitors can be
a lot smaller for high power levels (I doubt you are using GaN's though)! EPC
do some development boards for these new GaN devices.

If you are wanting to generate a carrier wave you probably need to replace the
ceramic resonator with a quartz crystal so you get a stable frequency.

Good piece of advice...

Yepp, number one is I'm making a buck and a buck-boost converter out of arduino [for feedback based pwm] so that variable in can be made for variable out, depending on logic. [no room for a bunch of programming resistors and selecting one from there like traditional buck converter ICs]. And yes, 200khz is my point of interest due to smaller inductor for both small and high current... [yes, i know both way there are limitations]...

and of course, since i'm gonna use arduino with 16mhz XTAL, i hope that will be smooth... so i can generate CW @ 500khz... [it's a distress beacon]...

and if this seems "ok" to you guys, next then i am looking for a further assistance... how exactly can i make this square pulsating wave in to an AC... [not + and GND but i need - and + (-2.5 - 0 - +2.5)] Any ideas???

So how are you going to detect saturation in the inductor? Once saturated an inductor
becomes just a short length of copper wire and large currents will flow.

MarkT:
So how are you going to detect saturation in the inductor? Once saturated an inductor
becomes just a short length of copper wire and large currents will flow.

interesting point... ideas????

I'm using the http://ieeexplore.ieee.org/xpl/login.jsp?tp=&arnumber=6749881&url=http%3A%2F%2Fieeexplore.ieee.org%2Fxpls%2Fabs_all.jsp%3Farnumber%3D6749881 for this