Here's how I do it...
// Wait for incoming steering pulse
set_timer1(0); // Start the timeout timer
while (get_timer1() < 62500) { // 50 ms timeout
if (input(STEERING_PULSE)) {
set_timer1(0); // Start pulse timer
break;
}
}
if (get_timer1() >= 62500) {
missed_pulse = TRUE;
missed_pulses++;
good_radio = 0;
}
// Measure steering pulse duration
while (get_timer1() < 3125) { // 2.5 ms timeout
if (!input(STEERING_PULSE)) {
steering_pw = get_timer1();
set_timer1(0); // Start pulse timer
break;
}
}
if ((steering_pw < (ST_LEFT-50)) || (steering_pw > (ST_RIGHT+50))) { // If the steering PW is outside the valid range
steering_pw = 0;
missed_pulse = TRUE;
missed_pulses++;
good_radio = 0;
}
else { // Radio signal is good.
if (++good_radio == 10) // Radio signal must be good for 10 pulses to establish a "good radio".
radio_signal_lost = FALSE;
missed_pulse = FALSE;
missed_pulses = 0;
}
There's alot of extra stuff in there for detecting missing pulses and such, but what it amounts to is waiting for the pulse to go TRUE, clearing the timer, wait for the pulse to go FALSE, and then read your timer. The timer value is the measured duration of the pulse. Easy breezy.