Mem,
Thanks for the response. I'm still a bit confused. I'm comparing two "ISR(TIMER1_CAPT_vect)" functions that you had posted previously one after the other. The first (11-06-08) :
ISR(TIMER1_CAPT_vect)
{
// we want to measure the time to the end of the pulse
if( (_SFR_BYTE(TCCR1B) & (1<<ICES1)) == pulseEnd ){
TCNT1 = 0; // reset the counter
if(ICR1 >= SYNC_GAP_LEN){ // is the space between pulses big enough to be the SYNC
processSync();
}
else if(Channel <= MAX_CHANNELS) { // else its channel pulse so save it
Pulses[Channel++] = ICR1 / TICKS_PER_uS; // store pulse length as microsoeconds
}
}
else { // the start of the pulse was detected
TCNT1 = 0; // so reset the counter to start timing
if(ICR1 >= SYNC_GAP_LEN){ // is the space between pulses big enough to be the SYNC
processSync();
}
if( State != READY_state && (ICR1 >= MIN_IN_PULSE_WIDTH) ){ // if the 'off' pulse is greater than minimum pulse, we need to invert the signal test
pulseEnd ^= 1; // invert the pulse mode
State = NOT_SYNCHED_state; //reset the state to start measuring from channel 1
}
}
TCCR1B ^= _BV(ICES1); // toggle bit value to trigger on the other edge
}
This code was modified next to (21-07-08):
ISR(TIMER1_CAPT_vect)
{
// we want to measure the time to the end of the pulse
if( (_SFR_BYTE(TCCR1B) & (1<<ICES1)) == pulseEnd ){
TCNT1 = 0; // reset the counter
if(ICR1 >= SYNC_GAP_LEN){ // is the space between pulses big enough to be the SYNC
processSync();
}
else if(Channel < MAX_CHANNELS) { // check if its a valid channel pulse and save it
if( (ICR1 >= MIN_IN_PULSE_WIDTH) && (ICR1 <= MAX_IN_PULSE_WIDTH) ){ // check for valid channel data
Pulses[++Channel] = ICR1 / TICKS_PER_uS; // store pulse length as microsoeconds
}
else if(State == READY_state){
State = FAILSAFE_state; // use fail safe values if input data invalid
Channel = 0; // reset the channel count
}
}
}
}
Both versions began with :
Code:
if( (_SFR_BYTE(TCCR1B) & (1<<ICES1)) == pulseEnd ){
TCNT1 = 0; // reset the counter
However only the earlier version had the line :
TCCR1B ^= _BV(ICES1); // toggle bit value to trigger on the other edge.
Looks like this line is needed to reset TCNT1 to 0 every time a falling edge and leading edge is detected so that it can set TCNT1 to 0 at the start of a pulse and get ICR1 at the end of a pulse.
From my understanding, the ICES1 in TCCR1B never changes once initialized in the begin(). Therefore (in default case) the beginning if-statement that tests for pulseEnd will only enter if a falling edge is detected. And at that point, set TCNT1 = 0. But i'm still confused as to where is TCNT1 set to 0 when a rising edge is detected? It looks like there is only 1 line in the entire code where TCNT1 is reset to 0(or set to anything).
Thanks again mem.