cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate N pulses at F frequency 50% duty cycle using timer?

parth kothiya
Senior

I want to generate N numbers of pulses at F frequency and 50% duty cycle for that application what is my timers mode setting is best? (PWM , OC)

after N pulse timer stop its out put

how can do it?

8 REPLIES 8
TDK
Guru

Include your chip number.

Some timers have a repetition counter which could be used for this.

But this is always possible using a chained timer configuration, with the master timer in one-shot mode with a duration of exactly the length of time you want and the slave generating the PWM signals.

If you feel a post has answered your question, please click "Accept as Solution".

STM32F030C8T6TR MCU​

Factor the Prescaler and Period values

Frequency= APBCLK / ((Prescaler + 1) * (Period + 1))

Duty = (Period + 1) / 2 // 50/50 duty

Consider TIM1 or TIM8, these have repetition counts (8-bit)

And then fire in One-Shot mode

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

As Tesla DeLorean writes. Consider using Advanced Timer TIM1 in one-shot mode if 255 counts is enought for you. If not you can link two timers together

Timer A (slave) generate PWM with 50% duty cycle, and is gated by output from Timer B.

Timer B run as counter and counts update events from timer A (count your pulses).

Then you can set pulse frequency by setting Timer A period and pulse number by timer B Compare channel value. This configuration should overcome 255 pulse limit in simple solution with Advanced timer (TIM1).

my requirement is 3(gpio pins) channel pulse out put with different frequency & Pulses.

3timers use each timers any one channel as pulse output.

how can I do this without using master / slave timer if possible?

single basic timer pulse count in interrupt like this ?

can i do this using single basic timer pulse counting in interrupt if possible then how?

because i need 3-chanel with different frequency & pulse

3-timer use each timers any one channel as output.

How many pulses do you need generate in one sequence ? And what is your pulse width ? That information can limit some soulitons.

With STM32F030 you have four timers with repetition counter. TIM1, TIM15, TIM16, TIM17. Use them in one-shot mode if 255 pulses are enough for your application.

If you need more pulses, you can do it that way. Take one of the mentioned timers. Set repetition counter to 255. Enable interrupts from "update" event. Interrupt fires every time timer generate 255 pulses. Then you can count number of generated pulses. When will remain less then 2*255 pulses. Write new value to repetition counter, to match desired number of pulses. For example if you want to generate 1000 pulses your sequence will looks that:

  1. timer generate first 255 pulses
  2. IRQ rutine fires
  3. timer generate second 255 pulses
  4. IRQ rutine fires and you will write 235 into repetition counter
  5. timer generate third 255 pulses and after that loads (235) new value into repetition counter
  6. IRQ rutine fires and you se one-shot mode
  7. timer generate 235 pulses and then disable itself (because one-shot mode)