How to Use Timer/Counter/Prescaler ???

Hello excuse my bad english i am from Austria and trying my best.

I need a PWM signal with 160 kHz on my Mega2560 and i dont know how to set the right values in the Registers.
I found some Timer/Counter/Prescaler Calculator:
http://evolutec.publicmsg.de/index.php?menu=software&content=prescalertools
http://www.et06.dk/atmega_timers/
but they didn't helped me. Please could anyone help me i don't get it work.
This is my code:

#include <avr/io.h>
#include <avr/interrupt.h>

#define LEDPIN 2
void setup()
{
  pinMode(LEDPIN, OUTPUT);

  // initialize Timer1
  cli();          // disable global interrupts
  TCCR1A = 0;     // set entire TCCR1A register to 0
  TCCR1B = 0;     // same for TCCR1B
  TCCR1B |= (1 << CS10);
  TCCR1B |= (0 << CS11);
  TCCR1B |= (0 << CS12);
  TCNT1 = 65436;
  TIMSK1 |= (1 << OCIE1A);
  sei();

}

void loop()
{

}

ISR(TIMER1_COMPA_vect)
{
  digitalWrite(LEDPIN, !digitalRead(LEDPIN));
}

For that fast a PWM you'll have to set prescale to divide-by-1, then limit the period
to 100 cycles. (16MHz / 160kHz = 100).

So you'll only have 100 steps, not 256 (using fast PWM). Or only 50 steps for
phase-correct.

Choose a mode where TOP is defined by a register (aka "frequency-correct") and set
that register to 100-1 - in fact a mode where TOP is defined by ICRx is the best as it
should allow use of both pins connected to the timer in question.

The exact modes depend on which timer, they are each slightly different - the data sheet
has all the details.

You can find information about how the timers work in the datasheet for the ATmega2560. There's a link to an English version on the Arduino Mega 2560 product page, here: http://arduino.cc/en/Main/ArduinoBoardMega2560. It may be available in other languages at the Atmel website.

If you compare the timer settings from this code to the information about Timer1 in the datasheet, you'll see that two important items aren't established the way you want them:

  • The timer is set to mode 0, "Normal" mode, which counts to 0xFFFF, rolls over to zero, and counts all the way to 0xFFFF again. That will roll over at a frequency of about Hz. There's no way to control the frequency in the Normal mode.
  • Nothing in the code sets anything that can affect the rollover frequency of the timer.

You will want to look at the datasheet, read about modes of operation for this timer, select a mode that lets you set the frequency. You'll want to change your code to set the timer mode, and add code to set whichever register you want to use for the timer rollover value.