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

closed as too broad by dhimaspw, Code Gorilla, jfpoilpret, Majenko, Nick Gammon Mar 1 '17 at 20:17

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

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.

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.

  • 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.