user30965: I have to switch 2 MOSFET IRF640 at a duty ratio of .6 and the frequency of  switching is  50khz how can I generate it with a single aurdino

user30005: Lots of ways.

Use a timer interrupt.
Use a pwm module.
Use the output compare module.
Use the SPI module.
Use a timer.
Use a pwm generator.

.......
Literally endless ways of doing it.

user7508: This could be achieved either with software (with a timer interrupt or
by polling micros()) or with hardware. The software methods, however,
are prone to a high level of jitter. If you want to have a steady
signal, you should use a hardware solution.
The most straightforward hardware option is to use a timer in PWM mode.
I would avoid fiddling with timer 0, as it is used by the Arduino
timekeeping functions. You are left with either timer 1 or timer 2. The
following program configures timer 2 to output the desired signal:

period = 20 µs = 5 × 64 CPU cycles
signal HIGH for 12 µs = 3 × 64 CPU cycles.

The output goes to pin 3, which is one of the two PWM pins controlled by
timer 2.
void setup()
{
    DDRD |= _BV(PD3);     // OC2B = PD3 = digital 3 as output
    OCR2A  = 5 - 1;       // period = 5 * 64 CPU cycles
    OCR2B  = 3 - 1;       // HIGH for 3 * 64 CPU cycles
    TCCR2A = _BV(COM2B1)  // non-inverting PWM on OC2B
           | _BV(WGM20)   // fast PWM, TOP = OCR2A
           | _BV(WGM21);  // ...ditto
    TCCR2B = _BV(WGM22)   // ...ditto
           | _BV(CS22);   // clock at F_CPU / 64
}

void loop(){}

For some in depth information on how timers work, see
Nick Gammon's tutorial on timers.

30965: <p>I have to switch 2 MOSFET IRF640 at a duty ratio of .6 and the frequency of  switching is  50khz how can I generate it with a single aurdino</p>
 30005: <p>Lots of ways.</p>

<ol>
<li><p>Use a timer interrupt.</p></li>
<li><p>Use a pwm module.</p></li>
<li><p>Use the output compare module.</p></li>
<li><p>Use the SPI module.</p></li>
<li><p>Use a timer.</p></li>
<li><p>Use a pwm generator.</p></li>
</ol>

<p>.......</p>

<p>Literally endless ways of doing it.</p>
 7508: <p>This could be achieved either with software (with a timer interrupt or
by polling <code>micros()</code>) or with hardware. The software methods, however,
are prone to a high level of jitter. If you want to have a steady
signal, you should use a hardware solution.</p>

<p>The most straightforward hardware option is to use a timer in PWM mode.
I would avoid fiddling with timer 0, as it is used by the Arduino
timekeeping functions. You are left with either timer 1 or timer 2. The
following program configures timer 2 to output the desired signal:</p>

<ul>
<li>period = 20 µs = 5 × 64 CPU cycles</li>
<li>signal HIGH for 12 µs = 3 × 64 CPU cycles.</li>
</ul>

<p>The output goes to pin 3, which is one of the two PWM pins controlled by
timer 2.</p>

<pre><code>void setup()
{
    DDRD |= _BV(PD3);     // OC2B = PD3 = digital 3 as output
    OCR2A  = 5 - 1;       // period = 5 * 64 CPU cycles
    OCR2B  = 3 - 1;       // HIGH for 3 * 64 CPU cycles
    TCCR2A = _BV(COM2B1)  // non-inverting PWM on OC2B
           | _BV(WGM20)   // fast PWM, TOP = OCR2A
           | _BV(WGM21);  // ...ditto
    TCCR2B = _BV(WGM22)   // ...ditto
           | _BV(CS22);   // clock at F_CPU / 64
}

void loop(){}
</code></pre>

<p>For some in depth information on how timers work, see
<a href="http://www.gammon.com.au/timers" rel="noreferrer">Nick Gammon's tutorial on timers</a>.</p>
