Use interrupt with 100kHz PWM Arduino UNO

Hi,

I am using an online code for generating 100 kHz PWM, board UNO, for my application (laser gain pumping signal).

void setup ()
{
  // pick a timer
  pinMode (3, OUTPUT) ;  // not sure if needed
  TCCR2A = 0;
  TCCR2B = 0;
  TCCR2A |= (1 << WGM21);
  OCR2A = 79;  // There are 80 counts, 0 to 79
  TCCR2B |= (1 << CS20);
  TCCR2A |= (1 << COM2B0);
}

void loop() {}

I want to use an external trigger to enable and disable the PWM output (same as arduino basic blink() example). I guess I am tring to use the board timers in a wrong way. If one know how to switch the PWM signal to other output in order to enable an interrupt control, It will be very helpful.

Thanks in advanced,

Matan

I am concerned.

You are making some reference to "interrupts".

Why would interrupts have anything whatever to do with your project?

Perhaps you had better explain what your project actually is?

You can turn the output on and off with software is several ways:

  1. Set the CS bits to 0b000 to turn off the timer
  2. Set the COM2B bits to 0b00, 0b10 or 0b11 to to get a non-toggling output.
  3. Set OCR2B above 79 so the OCR2B never matches

But there isn't a hardware way to turn the output on or off with an input pin. I this the closest you will get is a CHANGE interrupt on Pin 2 or Pin 3 using the pin value and software to turn the output on or off.

Thanks for the reply,

For lasing there is need for pwm signal that enable the gain pumping. When the pwm signal is off there is no pumping and the lasing stops. This is the motivation. to control the laser by enable or disable the gain pumping. Because we try to trigger few other component in the optical setup (like camera and function generator) we use triggers. I try to use interrupt example blink() as reference for changing ON/OFF output.

Hope it make it clear,

Matan

Thanks,

We try it and it works.

Matan

Thanks again,

Can I generate the 100kHz PWM both in pin 3 and pin 11 simultaneously? the phase between outputs is irrelevant.

Matan

after this, try adding
TCCR2A |= (1 << COM2A0);
to Toggle OC2A (pin 11) on Compare Match

You need also to set the pin as an OUTPUT. This gives you, incidentally, yet another alternative method of suspending the output of the timer via the data direction register.

It appears that you have configured a duty cycle of 50% and a period of 10uS. Is that what you need? It is a very simple case of PWM.

Thanks a lot.
Is supposed to be any different between the voltage amplitude and current of PWM generate using timer1 and timer2?

I am using an original UNO board and get difference (beside the max frequency)

Thanks again,
Matan

Not quite clear. :grin:

millis() does of course use an interrupt routine itself. I was merely concerned that you might imagine some need to use an ISR in your code.

No. Maybe you forgot to set pinMode(pin, OUTPUT);?

Also if the duty cycle changes then, depending on how you measure the voltage, you may see a difference. A digital volt meter will tend to show an average. An oscilloscope will show the true voltage.

Thanks for the reply,

We are measuring with oscilloscope.

I will debug it today

Matan