In my application, I have a timer generating periodic interrupts. In its ISR a certain number of PWM cycles needs to be generated on a GPIO pin. I first use another timer to time the duration, but it is not very deterministic since the PWM termination is influenced by other events, and it can end up in logic high sometimes. I can also do some integer counting in main() function, but it is less efficient. Is there a simple and efficient way of doing this?