I'm still learning C, and am having some problems with the code to detect a pulse over a specific width.
My method is:
The pulse is negative going, normal state is high: _____ ________ \_____/
Pulse duration time is measured by a register 'Break_Time' which is incremented in the ISR each time TIMER1 overflows.
I'm using interrupt INT0 to sense the start and finishing edges of the pulse.
1)Sense pulse using INT02)On interrupt:
3) If INTEDG0 was set for falling edge start timer to measure pulse duration 4) if INTEDG0 was set for rising edge then stop timer & test 'Break_Time' to see if the pulse length was long enough. 5) If pulse is too short then the 'gWatchDog' register is loaded with a value that forces a reset in the main program loop.The code I am using is below. Apologies for the poor formatting, I'm still trying to get that right as well. Debugging seems to show that the reset { gWatchDog = 125; } line is executing after the end of the pulse is detected.
I've tried a few different approached to no avail. Any suggestions on what I'm doing wrong here or the tidiest way to do this task?
Thanks D.
//______________________________________________________ // // I N T 0 I S R //______________________________________________________
if ( INTCONbits.INT0IF == 1 ) // is interrupt INT0? { INTCONbits.INT0IF = 0; // yes - so clear INT0 flag
if ( INTCON2bits.INTEDG0 == 0 ) // Falling Edge / Start of Break Detected { gBreakLength = 0; PIE1bits.TMR1IE = 1; // Enable TMR1 interrupt T1CONbits.TMR1ON = 1; // Start it counting PIR1bits.TMR1IF = 0; INTCON2bits.INTEDG0 = 1; // Set INT0 for rising edge, look for end of break gWatchDog = 0; // kick dog INTCONbits.INT0IF = 0; // clear INT0 flag } // end if
if ( INTCON2bits.INTEDG0 == 1 ) // Rising Edge / End of Break Detected {
if ( gBreakLength >= 10 ) // if pulse>10 open comms otherwise reset { INT0_DISABLED(); // disable interrupt INT0 gCommsBufferIndex = 0; // initialise buffer index UART_Init(); // Set up UART INTCONbits.INT0IE = 0; // disable INT0 (now in serial mode) } //end if
//### PROBLEM HERE ###
else // Force reset. Break