Hello,
It appears you are attempting to generate a waveform with an on period of 40 us and an off period of 15 us, to give a total waveform period of 55 us. Is this correct?
There are two potential methods of doing this using the TPM -
- Edge aligned PWM mode for the TPM channel (interrupt not required), or
- Output compare interrupt for each edge of the waveform.
You seem to be attempting to combine both PWM operation and interrupts. Because of the relatively short period the PWM method is probably more appropriate, with minimal software overheads.
For example:
T2MOD = 55; /* TPM overflow period 55 us */
T2CH0V = 40; /* PWM on Ch 0 with period 40 us */
T2CH0SC = 0x28; /* Edge aligned PWM mode for Ch 0 */
The waveform should now be generated without the need for interrupts. The possible disadvantage of this method is that the other TPM channel are stuck with the same very short timer overflow period, which may limit their usefulness.
To use the output compare interrupt method, the T2MOD value should remain at its default value. The "toggle on output compare" setting must be avoided to prevent the possibility of incorrect phasing. Instead, within the ISR code, the setting for T2CH0SC should be either 0x58 (clear output on compare) or 0x5C (set output on compare) depending on the next required output state. You will also need to calculate the next output compare value, which will depend on the edge currently being processed, and this value used to update T2CH0V.
The output compare interrupt method will be disrupted if interrupt processing takes too much time relative to the required waveform period, or if the occurrence of other interrupts prevents update of the next output compare value in a timely manner.
Regards,
Mac