You should be fine. If a timer is being used for PWM it should also be useable to generate interrupts. Also, you should be able to use one timer for more than one PWM output. I mean, if they design the chip with 4 PWM outputs and 3 timers, there has to be a way to actually use all 4 PWM outputs.
The timer being used to generate the PWM signal has an interrupt service routine (ISR). It is either triggered every time the output pin changes (Interrupt on match), or the timer is reloaded (Interrupt-on-overflow).
That interrupt service routine can increment a variable to count one more pulse every time it is called.
AFAICT, all of the PIC18F8722 timers support, at least, one of those two types of ISR.
There is no need for another hardware counter.
The pulse count will be stored in a 'global' variable and so can be read by code outside the ISR. If you are using C to program the PIC18F8722, then the pulse count variable will be defined as 'volatile'.
Ideally, the count will fit in one byte, you will only need to count up to 255. However, if the pulse count variable needs to be bigger than a byte, you'll need to be slightly careful how the code outside the ISR uses it.
A variable bigger than one byte will be read in several instructions, so it is possible for the interrupt to happen in the middle of the main code accessing its value. This can result in the main code occasionally getting confused about the actual value of the pulse count.
(Please add a bit more information to your question, so that we can understand if you need help with solving this kind of problem. If it is not an issue, it seems too much complexity to add to an answer.)
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 ofTIMx_CR2
register. These must be set to0b010
(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) andTS
(Trigger Selection) bits inTIMx_SMCR
register.SMS
must be0b111
(External clock mode 1). And you must also setTS
bits according to the master timer you use, which must be one of theITRx
options.Slave timer needs a prescaler of 1 (
TIMx_PSC = 0
). Set theTIMx_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.