One issue I see is
This won't actually swap from rising to falling edge (or vice versa) -CCP1CONbits.CCP1M =~CCP1CONbits.CCP1M;
//change rising - falling front
CCP1M = 0b0100 for every falling edge and 0b0101 (not the inverse of the other)
You could do this with if
CCP1CONbits.CCP1M1 = ~CCP1CONbits.CCP1M1; // Toggle single bit
Also - measuring for 650 'ticks' of TMR0 (which if the speed shown is correct) is a short interval - why not just use a small delay (3ms?) I would measure for longer - the display update will be unpleasant.
As demonstrated in my code - I would disable the peripheral interrupts whilst displaying the values.
In the interrupt enable you have
Code: Select all
CCPR1H=0;
CCPR1L=0;
In the C block
Code: Select all
TMR1H = 236; // Preset for Timer1 MSB register
TMR1L = 120; // preset for timer1 LSB register
In my code - I clear TMR1H and L on each interrupt - this seemed simpler than subtracting one value from another to give the pulse time - so if you want to time a 'single' pulse - set TMR1H and L to 0 on a rising pulse then the count on a falling pulse is the pulse length - no need to subtract the value?
I would be tempted to use a (global) bool variable 'rising' (initially true) - this is true when we are watching for a rising pulse.
So (in pseudocode - a description of what you want the MCU to do..) in CAP_RE:
if rising: //FC
set CCP1CONbits.CCP1M1 for falling //C
TMR1H = 0; //C
TMR1L = 0; //C
else
CCP1CONbits.CCP1M1 for rising //C
PulseLength = (CCPR1H << 8) + CCPR1L; // C
then
rising = ~rising //FC - toggle direction