I´ve copied this code from the forum:
You’re welcome. (See Footnote.)
Can someone change the code that I have a 2MHz output on an other pin.
How about Pin 3?
const int freqOutputPin = 3;
const int ocr2aval = 3;
// These are not needed to generate the code. They are simply to remind the user
// what the frequency is. Note that changing the value of "prescale" here does
// not affect the counter operation. It's just for the printout vlaue
const int prescale = 1;
const float period = 2.0 * prescale * (ocr2aval+1) / (F_CPU/1.0e6);
const float freq = 1.0e6 / period;
// Toggle OC2B on "compare match".
// Normal count mode with OCRA as "top"
TCCR2A = ((1 << WGM21) | (1 << COM2B0));
// Prescale divider value = 1
// If you want to change the prescale value, look at the ATmega
// data sheet for the appropriate values of CS22:0
TCCR2B = (1 << CS20);
// Not really needed here, since TIMSK is initialized
// to zero, but for general use, it's probably not
// a bad idea to make sure timer 2 interrupts are
TIMSK2 = 0;
// This is the "top" value at which the counter will reset
OCR2A = ocr2aval;
Serial.print("Period = ");
Serial.print("Frequency = ");
// Timer 2 Output is directly connected to the designated OCR pin, so
// program intervention is not required to do anything with it.
// Do just about anything you want to here, but if you mess
// with Timer 2, you may affect the output on its OCR pin.
Tested on ATmega 328 Arduino boards.
We contribute to the forum in hopes that people can learn from the code. Because of the power and versatility of the timer functions on ATmega chips (and, therefore, the complexity), it’s hard for most of us to wade through the data sheet. That’s why I sometimes post complete programs. (And I always test any complete programs that I post.)
If people (even beginners) are really interested, they can go back through the data sheet and, maybe, see why these registers are set up like this. Maybe the few comments that I put here will help. There were more comments in http://arduino.cc/forum/index.php/topic,62964.0.html. Sometimes my propensity for wordiness confuses people, or maybe it just wears them down.