# Generate short pulse from AVR MCU PWM

I am trying to generate short pulses as low as 31ns (on time) with a frequency of 10 Hz to 10 kHz.

Microcontroller: Atmel Xmega Clock: 32 MHz PWM timer: 16-bit

I am using PWM channels to generate the required pulse. I'm able to get 31ns pulse using compare channel OCnX. But because of 16-bit timer I couldn't get the required frequency levels in few Hz.

Is there a way to solve these type of problems.

• Look at using a second timer and the event system to trigger the high speed timer. This assumes you don't need 31ns resolution in the interval between pulses, if so I believe you can cascade two 16 bit timers to form a 32 bit timer on the XMEGA. – Dean Franks Oct 26 '17 at 8:28
• As per above scenario my pulse is ON when timer overflows and cleared when compare match occurs. So when two timers are Cascaded how can we output ON when timer 2 over flows and clear when compare match on timer1. Because on overflow of first timer, you PWM channel automatically toggles the pin to high. – Vinod kumar Oct 26 '17 at 8:32
• If you need short pulse, but not 31ns resolution, and your event channel #0 is not used by anything else, you can do this with single timer only too. Use slowly enough clocked TC to get your lowest rate necessary and use EVOUT signal for output (see CLKEVOUT register), not PWM. You can trigger the even by TC's overflow for example and signal lasts only one peripheral clock's tick regardless of clocking of the TC. – Martin Oct 26 '17 at 8:53
• You can use the event system, or set a terminal count interrupt on the least significant counter and enable the compare output on the least significant counter when the most significant counter is at its terminal count. An outline of the event solution is in the XMEGA counter app note: atmel.com/images/doc8045.pdf – Dean Franks Oct 26 '17 at 8:54
• If not with Xmega is there any way to do it – Vinod kumar Oct 27 '17 at 13:14

most ATMEGA micro controller data sheets have different types of PWM and their accompanying modes with the maximum frequency obtainable (the minimum frequency is usually 1 Hz). to get the lower frequency , you would have to use the switch the necessary bits that attenuate the clock frequency (usually, 1,2,4,8,16...1024).. attenuating the clock frequency used in the timer can get you as low as 0.5 Hz with a large duty cycle. ***

(e.g using a 1024 attenuation, your clock frequency becomes ~ 32 Hz, using the fast PWM with duty cycle of 20 would give you PWM of (32/(23+1)) == 1.33 HZ.)

PS: this function is present in smaller older AVR MCU's so i assume that it should be present in better ones.

• You didn't understand my question. We should generate pulse of 31ns i.e (1/32MHz) freq: 10hz – Vinod kumar Oct 26 '17 at 12:27

I'm able to get 31ns pulse using compare channel OCnX. But because of 16-bit timer I couldn't get the required frequency levels in few Hz.

From what I understand, you are able to get 31 nS on-time but not 99999969 nS off time (at 10Hz PWM frequency). If I'm not mistaken, you are using timer overflow method to generate an off state. Modify this a bit and you can get extended off time periods. How?
Use a counter variable!

Let's say the maximum time you can measure with your timer is t_max = t1 nS, (t1 < 99999969). Do the math and count how many times you will have to overflow the timer to get the required off-time. Let this number be 5.
Whenever the timer overflows, increment a counter variable. Keep resetting the timer and the incrementing the counter after every timer overflow event till counter < = 5 (here) i.e. till you reach the desired time value. Keep the PWM pin state LOW until you reach the desired off-time for the PWM pulse
So, in your case, if you are unable to get a delay of 99999969 nS in one overflow event, repeat the process n times times till you attain the desired off-time. Hold the PWM pin LOW until you reach a certain value of counter variable which gives you the required off-time.

Eg. let,
t_max = 15nS
t_off = 100nS
i = counter variable
Here, you can measure 15nS of time in one timer overflow event. So you will have to overflow the timer 6 times to get a delay of 90nS and in the next timer interval, you can get a 100nS time measurement. So you have keep overflowing the timer for 6 times i.e. while (i <= 6), overflow timer;