cancel
Showing results for 
Search instead for 
Did you mean: 

Count PWM pulses

Posted on February 08, 2016 at 10:30

Hello there,

I am using STM32F4. I was wondering either it is possible to count the PWM rising or falling edges that the mcu generates. I know that I could connect the PWM output to anothers timer input trigger pin and do it this way. But I am using 8 PWM output channel so I would additionaly need 8 input capture channels (dunno how many timers is needed for that yet, hope less than 8). I could also generate an interrupt for each pwm channel to increment a variable in the interrupt routine but that would be really heave for the MCU if I had 8 pwm channels running 10kHz each.

Is it possible maybe, to somehow hook up timers internally to get this functionality I am looking for? Or using DMA maybe? I cannot think of anything better than external pin connection. I would apreciate all help!
12 REPLIES 12
Posted on February 08, 2016 at 13:26

I have come to a conclusion that the solution for my problem, would be connecting another timer to my PWM timer as slave, in the way that if PWM timer 1 overflows, the slave timer increments by one. This can be done using the external pin (slave timer as input capture). But can this be done without involving external pins and somehow connect the second timer counter with PWM timer 1 overflow (without manually writing that to interrupt routine)?

Posted on February 08, 2016 at 14:16

This is the purpose of timer chaining.

> in the way that if PWM timer 1 overflows, the slave timer increments by one

> the slave timer increments by one

In the master timer (''PWM timer 1''), set TRGO to Update (TIMx_CR2.MMS = 0b010).

Select slave timer so that it can be triggered from master timer - have a look at the tables after description of TIMx_SMCR, or into the table in http://www.efton.sk/STM32/STM32F4xx%20misc.pdf . In the slave's TIMx_SMCR.TS, select ITRy accordingly. In the same TIMx_SMCR, set .SMS field to External clock mode 1, i.e. 0b111. Set slave's ARR to max (0xFFFF or 0xFFFFFFFF) and enable by TIMx_CR1.CEN=1. Enjoy.

JW

Posted on February 08, 2016 at 15:03

One could also presumably just account for elapsed time vs period/settings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 08, 2016 at 15:11

Thank you Jan, that is what I meant.

Clive, could you please explain more of what you meant exacly?

Also, there is one more question about the chaining- If I understand correctly, the fequency for a timer if same for all 4 channels, and the channels cannot have separate different frequencies. But can one timer, be a slave for 4 different timers (on 4 channels) or just one? If i have 4 timers that each generates 1 pwm signal. Do I need 4 additional slave timers or just 1 slave timer with 4 channels?
Posted on February 08, 2016 at 15:28

>> One could also presumably just account for elapsed time vs period/settings.

> Clive, could you please explain more of what you meant exacly?

If your PWM period is N cycles and the elapsed time is M cycles, there were M/N PWM cycles, exactly. ;)

> But can one timer, be a slave for 4 different timers (on 4 channels) or just one?

Just one at a time. To quote Clive, there is only one counting element per timer. Channels refer to the capture/compare units only.

JW

Posted on February 08, 2016 at 16:00

Thank you for answer. I think I over estimated the timers functionality when I was making the board and now I see some of the things has to be done manually. The problem is that at the moment I have 8 pwm channels (4 channels for TIM1 and 4 channels for TIM8). I though I would be able to set the PWM frequency (not pulse width) for each channel separatelly and also be able to tell how many PWM cycles were done each time I check for it in time. Now I see I can set the frequency separatelly only for TIM1 and TIM8. Also there is no easy way of changing the frequency the way I change PWM for a channel.

If I decided now to use 8 separate timers, 1 pwm channel each i would then be able to at least change the frequency for each channel. But then, I wont have enough timers to make them slaves, I would need another 8 timers. If i for example made 8 variables, so I could increment one of them each time the corresponding pwm timer overflows, I would get a massive amount of interrupts and I am not sure the system wouldnt be starved then.
Posted on February 08, 2016 at 16:30

As Clive said above, you don't need to count the PWM pulses, as you *know* what is their period, it's enough to measure time.

JW

Posted on February 08, 2016 at 16:38

I understand, but You can agree that the situation gets a bit more complicated when I need to take track of 8 pwm channels and each one of them can run on different frequency.

Posted on February 08, 2016 at 17:29

I'm not sure it's that complicated, the machine is synchronous, and time is linear. You have a computer to do the arithmetic, you're not having to do it in your head, although you clearly need to understand the interrelationships.

To paraphrase a movie quote, Time, it's what stops everything from happening at once.

The timers on the STM32 are rather lacking. There is the repetition count on the advanced timers, which can decimate the update rate, but that really isn't going to help much for multiple channels/timers.

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