Posted on April 01, 2018 at 14:27I think the F051 has 4 capture channels on TIM1, TIM2 and TIM3 as well, which is already more than I need
While the timers' input circuit can be set so that capture occurs on both leading and trailing edge, if two edges happen close to each other and you won't succeed to read out the first from the capture register before the second arrives, you'll miss it. Note, that the interrupt latencies are relatively high, and significantly higher if multiple interrupts are active at the same times. In practice it means, you won't be able to measure PWM with small and large duty cycles.
This is why it's better to capture the two edges separately in two channels - the STM32 timers have a provision to connect one pin to two adjacent timer channels, so you don't even need to waste pins on that.
Note, that the two limit cases - 0% and 100% duty (more precisely, not only the DC case but also extremely short pulses with edges spaced closer to each other than is the period of timer input clock) won't fire an interrupt at all, and have to be handled as an exception in some other way.
How can I determine the duty cycle if I catch only one edge of the signal?
This works if you use the two-channel scheme outlined above. The other edge's time you read out simply from the other channel's capture register. Even if its readout is missed and you read out the next other-edge's occurence's time, it's easy to reveal by simple time comparison and you'd then simply wait until the next interrupt to determine the exact period relevant to that transition.
In the TIM chapter in RM you'll find also described a scheme with resetting the counter upon one edge, but I personally would be hesitant to use that (and it is unusable if both 2 couples of channels are to be used).
it might be enough to catch every 10th for example, that means that I could simply use an external multiplexer, right?
Yes, but if you multiplex all 10 channels into one, don't count on that you'll be able to determine the period/duty immediately after switching the multiplexer, i.e. it would be significantly more than every 10th period (I'd count more like 50th). Of course this approach can be tweaked with using several timers and lower multiplex ratio.
JW