PWM Generation with Master/Slave Timer Configuration in STM32

pwmstm32

I am going to generate a PWM with master/slave timer configuration. I prefer that my master timer generates PWM with an interrupt. I need to connect this interrupt to another timer as its input clock to count the number of the pulses.

Based on this diagram, what should I do?

enter image description here

  1. I enable my timer to generate PWM.
  2. Enable timer interrupt for each pulse.
  3. How to connect this interrupt to another timer?
  4. How to configure it as master and the second timer as a salve?
  5. Is it necessary to define these timers as master and slave timer, or is connecting the interrupt as the second timer enough?

Best Answer

There is no concept of connecting interrupts each other. You need to connect timers in master & slave configuration.

See MMS (Master Mode Selection) bits of TIMx_CR2 register. These must be set to 0b010 (Update), so that the master timer creates a pulse on TRGO (Trigger output) whenever it overflows, in other words, after each PWM pulse it generates. I assume you have made the other settings correctly for the master timer to produce a PWM. Do not enable interrupts for master timer.

In the slave timer, see SMS (Slave Mode Selection) and TS (Trigger Selection) bits in TIMx_SMCR register. SMS must be 0b111 (External clock mode 1). And you must also set TS bits according to the master timer you use, which must be one of the ITRx options.

Slave timer needs a prescaler of 1 (TIMx_PSC = 0). Set the TIMx_ARR register according to the desired pulse count and enable update interrupt of the slave timer. Slave timer interrupt should be triggered when the desired pulse count is reached. And in the slave timer interrupt service routine, you can stop the master timer.

I haven't recently tested these steps and I'm not sure if I missed something or not, but I believe they can put you in the right direction.