Issue with Interrupt using PWM
I'm using a PIC24FJ128GC010 with XC16 (v1.51) and MPLABX 5.4.
I've configured the Output Compare lines to run in PWM mode to drive a stepper motor controller. I have the PWM output configured so that it is outputting a square wave at the frequency that I require to drive my stepper motor.
PWM is configured as edge aligned and appears to be working fine. I can vary the PWM period and speed up or slow down the output / motor.
OC1CON1bits.OCTSEL = 0; // TMR2 as clock source
OC1CON2bits.SYNCSEL = 0x1F; // self
The issue I'm having is with the OC/PWM interrupt. I'm counting interrupts to track the number of steps taken so I can determine when to stop. However, the interrupts do not seem to be generated at the same freq as the PWM output? For instance, if I set the PWM output freq to be 200 Hz (5ms per pulse), I get one full rotation per second of my motor which is correct since it is a 200 step per rotation motor. In the interrupt I am counting pulses to determine when it makes 3 full rotations = 600 steps. However, the motor make many more than 3 rotations, but when my motor stops, the interrupt reports that is only counted 600 pulses? The 600 pulses would be correct, however the motor made many more than 3 rotations so it should have been more than 600 steps/pulses.
Am I wrong in thinking the PWM interrupt should be generated on each pulse output?
Of course my first thoughts are that I have something configured incorrectly, but the motor speed control appears to be working just fine, it's just the counting of steps/pulses in the interrupt that appears to be not working.