user30412: i don't know how to manipulate arduino uno or mega registers and timers to be close to 75kHz output frequency. Could you help please ?

user7508: First you have to compute, from your desired frequency, the number of
CPU cycles per period of the signal:
F_CPU / 75 kHz = 16,000 kHz / 75 kHz ≈ 213.33
Since the timers can only count up to an integer, the closest you can
get is 213 CPU cycles. The frequency you get will then be
F_CPU / 213 = 16,000 kHz / 213 ≈ 75.12 kHz
Now you can configure a timer. I find that the easiest is to use a so
called “fast PWM” mode. Choose one of those modes where the TOP value
can be set with a register, and set it to one unit less than the desired
period: the timer will then repeatedly count from 0 to 212.
Here is an example using channel A of Timer 1 on an Arduino Uno.
The output is on digital pin 9 = OC1A = PB1:
// Configure Timer 1 for PWM @ 75.12 kHz.
DDRB  |= PB1;         // set OC1A = PB1 as output
TCCR1A = 0;           // clear the settings of the Arduino core
TCCR1B = 0;           // ...ditto
TCNT1  = 0;           // clear the timer
TCCR1A = _BV(COM1A1)  // non-inverting PWM on OC1A
       | _BV(WGM11);  // mode 14: fast PWM, TOP = ICR1
TCCR1B = _BV(WGM12)   // ...ditto
       | _BV(WGM13)   // ...ditto
       | _BV(CS10);   // clock at F_CPU
ICR1   = 213 - 1;     // period = 213 CPU cycles = 13.3125 us
OCR1A  = 213/2 - 1;   // duty cycle ~ 50%

You can easily adapt this to any timer on the Uno or Mega. Just avoid
Timer 0 if you are going to use the Arduino timekeeping functions.

30412: <p>i don't know how to manipulate arduino uno or mega registers and timers to be close to 75kHz output frequency. Could you help please ?</p>
 7508: <p>First you have to compute, from your desired frequency, the number of
CPU cycles per period of the signal:</p>
<p>F_CPU / 75 kHz = 16,000 kHz / 75 kHz ≈ 213.33</p>
<p>Since the timers can only count up to an integer, the closest you can
get is 213 CPU cycles. The frequency you get will then be</p>
<p>F_CPU / 213 = 16,000 kHz / 213 ≈ 75.12 kHz</p>
<p>Now you can configure a timer. I find that the easiest is to use a so
called “fast PWM” mode. Choose one of those modes where the TOP value
can be set with a register, and set it to one unit less than the desired
period: the timer will then repeatedly count from 0 to 212.</p>
<p>Here is an example using channel A of Timer 1 on an Arduino Uno.
The output is on digital pin 9 = OC1A = PB1:</p>
<pre class="lang-cpp prettyprint-override"><code>// Configure Timer 1 for PWM @ 75.12 kHz.
DDRB  |= PB1;         // set OC1A = PB1 as output
TCCR1A = 0;           // clear the settings of the Arduino core
TCCR1B = 0;           // ...ditto
TCNT1  = 0;           // clear the timer
TCCR1A = _BV(COM1A1)  // non-inverting PWM on OC1A
       | _BV(WGM11);  // mode 14: fast PWM, TOP = ICR1
TCCR1B = _BV(WGM12)   // ...ditto
       | _BV(WGM13)   // ...ditto
       | _BV(CS10);   // clock at F_CPU
ICR1   = 213 - 1;     // period = 213 CPU cycles = 13.3125 us
OCR1A  = 213/2 - 1;   // duty cycle ~ 50%
</code></pre>
<p>You can easily adapt this to any timer on the Uno or Mega. Just avoid
Timer 0 if you are going to use the Arduino timekeeping functions.</p>
