1

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

4

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.

| improve this answer | |
1

Lots of ways.

  1. Use a timer interrupt.

  2. Use a pwm module.

  3. Use the output compare module.

  4. Use the SPI module.

  5. Use a timer.

  6. Use a pwm generator.

.......

Literally endless ways of doing it.

| improve this answer | |
  • 1
    Endless ways of naming the same things: 2, 3 and 6 are the same, 5 is the same as either 1 or (2, 3, 6). – Edgar Bonet Feb 15 '17 at 14:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.