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.
You can turn the output on and off with software is several ways:
Set the CS bits to 0b000 to turn off the timer
Set the COM2B bits to 0b00, 0b10 or 0b11 to to get a non-toggling output.
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.
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.
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.
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.