user4024: I have extracted the following code from the multiwii project. It generates a ~490-500hz pwm signal on pin 6 which is used to drive an esc.
I want to change this frequency down to 50Hz to drive a servo instead. But ive had next to no experience at this level of avr programming, and dont want to loose anymore days to it, if its a dead end. (ie not possible) (i have tried reading various websites on the topic)
void setup() { 
    pinMode(6, OUTPUT);
    TCCR4E |= (1<<ENHC4); // enhanced pwm mode
    TCCR4B &= ~(1<<CS41); TCCR4B |= (1<<CS42)|(1<<CS40); // prescaler to 16
    TCCR4D |= (1<<WGM40); TC4H = 0x3; OCR4C = 0xFF; // phase and frequency correct mode & top to 1023 but with enhanced pwm mode we have 2047

    TCCR4C |= (1<<COM4D1)|(1<<PWM4D); // connect pin 6 to timer 4 channel D   


}

void loop() {
    //write out
    int value = 1500;
    TC4H = value>>8; 
    OCR4D = (value&0xFF);
}

here is the new code so far working off the solution provided below:
void setup() {   
    pinMode(6, OUTPUT);
    TCCR4B &= ~(_BV(CS43) | _BV(CS42) | _BV(CS41) | _BV(CS40));
    TCCR4B |= _BV(CS43) | _BV(CS41);
    TCCR4D &= ~(_BV(WGM41) | _BV(WGM40));
    TC4H = 624 >> 8; // B10 0x2
    OCR4C = 624 & 0xff;//B1110000 0x70
    TCCR4C |= (1<<COM4D1)|(1<<PWM4D);
}
void loop() {
    //write out
    //30 - 60 gives 1000 to 2000 us, but that only allows 30 increments between ??
    int value = 30;
    TC4H = value >> 8;
    OCR4D = value & 0xff;
}

the wave form i want:


user368: Yeah... sometimes it's easier to scrap what's already there and start fresh.
We need to:

Pick a suitable prescaler to get close to the desired cycle length
Fine-tune the value in TOP in order to get an exact value

Assuming this is a 16MHz Leonardo, and we have Fast 10-bit PWM (PWM4x = 1, WGM4x = 0b00) available:
16MHz / 50Hz / (1 << 10) = 312.5
Therefore we need a prescaler of 512 (CS4x = 0b1010).
16MHz / 50Hz / 512 = 625
Therefore we need a TOP of 624 (OCR4C = 624).
TCCR4B &= ~(_BV(CS43) | _BV(CS42) | _BV(CS41) | _BV(CS40));
TCCR4B |= _BV(CS43) | _BV(CS41);
TCCR4D &= ~(_BV(WGM41) | _BV(WGM40));
TC4H = 624 >> 8;
OCR4C = 624 & 0xff;

And then you set COM4xn and PWM4x to appropriate values for whichever pin(s) you want to output PWM on, and set OCR4x to a value between 0 and 624 (using TC4H appropriately; see the datasheet for details) to control the duty cycle.

user10794: To get finer resolution use Timer 1 - as Ignacio Vazquez-Abrams suggested.
void setup ()
  {
  pinMode (10, OUTPUT);   // OC1B on the Leonardo / Micro
  TCCR1A = 0;   // stop Timer 1
  TCCR1B = 0;   //     ditto

  // set up Timer 1
  TCCR1A = bit (COM1B1) | bit (WGM11) | bit (WGM10);  // toggle OC1B on Compare Match
  TCCR1B = bit (WGM13) | bit (CS11);   // PWM phase correct, prescaler of 8
  OCR1A = 20000;  // frequency: 50 Hz
  OCR1B = 1000;   // duty cycle - 1 ms
  }  // end of setup

void loop ()
  {
  // whatever 
  }  // end of loop

As you can see from the scope output, the pulse width is 1 ms, which extremely conveniently corresponds to the duty cycle of 1000. Thus, therefore, the duty cycle figure is exactly related to microseconds.
Also you can see that the frequency is 50 Hz.

This uses a prescaler of 8 to give high resolution. It takes two pulses to make one "cycle" (one on, one off) and thus it effectively divides the system clock by 16. Since the system clock is 16 MHz, that is why one "unit" is 1 µs.
The OCR1A figure is arrived at easily:
 1000000 / 50 = 20000  (1 MHz / 50 Hz)

Now you can just tweak your desired pulse width knowing that each unit is 1 µs. So for 2 ms OCR1B would need to be 2000.

4024: <p>I have extracted the following code from the multiwii project. It generates a ~490-500hz pwm signal on pin 6 which is used to drive an esc.</p>

<p>I want to change this frequency down to 50Hz to drive a servo instead. But ive had next to no experience at this level of avr programming, and dont want to loose anymore days to it, if its a dead end. (ie not possible) (i have tried reading various websites on the topic)</p>

<pre><code>void setup() { 
    pinMode(6, OUTPUT);
    TCCR4E |= (1&lt;&lt;ENHC4); // enhanced pwm mode
    TCCR4B &amp;= ~(1&lt;&lt;CS41); TCCR4B |= (1&lt;&lt;CS42)|(1&lt;&lt;CS40); // prescaler to 16
    TCCR4D |= (1&lt;&lt;WGM40); TC4H = 0x3; OCR4C = 0xFF; // phase and frequency correct mode &amp; top to 1023 but with enhanced pwm mode we have 2047

    TCCR4C |= (1&lt;&lt;COM4D1)|(1&lt;&lt;PWM4D); // connect pin 6 to timer 4 channel D   


}

void loop() {
    //write out
    int value = 1500;
    TC4H = value&gt;&gt;8; 
    OCR4D = (value&amp;0xFF);
}
</code></pre>

<p>here is the new code so far working off the solution provided below:</p>

<pre><code>void setup() {   
    pinMode(6, OUTPUT);
    TCCR4B &amp;= ~(_BV(CS43) | _BV(CS42) | _BV(CS41) | _BV(CS40));
    TCCR4B |= _BV(CS43) | _BV(CS41);
    TCCR4D &amp;= ~(_BV(WGM41) | _BV(WGM40));
    TC4H = 624 &gt;&gt; 8; // B10 0x2
    OCR4C = 624 &amp; 0xff;//B1110000 0x70
    TCCR4C |= (1&lt;&lt;COM4D1)|(1&lt;&lt;PWM4D);
}
void loop() {
    //write out
    //30 - 60 gives 1000 to 2000 us, but that only allows 30 increments between ??
    int value = 30;
    TC4H = value &gt;&gt; 8;
    OCR4D = value &amp; 0xff;
}
</code></pre>

<p>the wave form i want:</p>

<p><img src="https://i.stack.imgur.com/f1BEv.png" alt="the wave form i want"></p>
 368: <p>Yeah... sometimes it's easier to scrap what's already there and start fresh.</p>

<p>We need to:</p>

<ul>
<li>Pick a suitable prescaler to get close to the desired cycle length</li>
<li>Fine-tune the value in TOP in order to get an exact value</li>
</ul>

<p>Assuming this is a 16MHz Leonardo, and we have Fast 10-bit PWM (PWM4x = 1, WGM4x = 0b00) available:</p>

<p>16MHz / 50Hz / (1 &lt;&lt; 10) = 312.5</p>

<p>Therefore we need a prescaler of 512 (CS4x = 0b1010).</p>

<p>16MHz / 50Hz / 512 = 625</p>

<p>Therefore we need a TOP of 624 (OCR4C = 624).</p>

<pre><code>TCCR4B &amp;= ~(_BV(CS43) | _BV(CS42) | _BV(CS41) | _BV(CS40));
TCCR4B |= _BV(CS43) | _BV(CS41);
TCCR4D &amp;= ~(_BV(WGM41) | _BV(WGM40));
TC4H = 624 &gt;&gt; 8;
OCR4C = 624 &amp; 0xff;
</code></pre>

<p>And then you set COM4xn and PWM4x to appropriate values for whichever pin(s) you want to output PWM on, and set OCR4x to a value between 0 and 624 (using TC4H appropriately; see the datasheet for details) to control the duty cycle.</p>
 10794: <p>To get finer resolution use Timer 1 - as Ignacio Vazquez-Abrams suggested.</p>

<pre><code>void setup ()
  {
  pinMode (10, OUTPUT);   // OC1B on the Leonardo / Micro
  TCCR1A = 0;   // stop Timer 1
  TCCR1B = 0;   //     ditto

  // set up Timer 1
  TCCR1A = bit (COM1B1) | bit (WGM11) | bit (WGM10);  // toggle OC1B on Compare Match
  TCCR1B = bit (WGM13) | bit (CS11);   // PWM phase correct, prescaler of 8
  OCR1A = 20000;  // frequency: 50 Hz
  OCR1B = 1000;   // duty cycle - 1 ms
  }  // end of setup

void loop ()
  {
  // whatever 
  }  // end of loop
</code></pre>

<p>As you can see from the scope output, the pulse width is 1 ms, which extremely conveniently corresponds to the duty cycle of 1000. Thus, therefore, the duty cycle figure is exactly related to microseconds.</p>

<p>Also you can see that the frequency is 50 Hz.</p>

<p><img src="https://i.stack.imgur.com/kFOiZ.png" alt="50 Hz Timer 1 example"></p>

<p>This uses a prescaler of 8 to give high resolution. It takes two pulses to make one "cycle" (one on, one off) and thus it effectively divides the system clock by 16. Since the system clock is 16 MHz, that is why one "unit" is 1 µs.</p>

<p>The OCR1A figure is arrived at easily:</p>

<pre><code> 1000000 / 50 = 20000  (1 MHz / 50 Hz)
</code></pre>

<p>Now you can just tweak your desired pulse width knowing that each unit is 1 µs. So for 2 ms OCR1B would need to be 2000.</p>
