Quote:
|
Originally Posted by stephenthe1
if the pin state hasn't changed, how does it know to not make another tick count? is there a flag, kind of like interrupts that must be cleared before you can register another tick? if so, that's pretty neat.
|
This is what state variables are for. As an example:
Code:
// assumptions:
// left encoder phase-A signal goes to rc_dig_in01
// left encoder phase-B signal goes to rc_dig_in06
// right encoder phase-A signal goes to rc_dig_in02
// right encoder phase-B signal goes to rc_dig_in08
static long left_encoder_count = 0;
static long right_encoder_count = 0;
static unsigned char prior_left_phase_a_state = 0;
static unsigned char prior_right_phase_a_state = 0;
// look for a rising edge on left phase A
if(rc_dig_in01 != prior_left_phase_a_state && rc_dig_in01 != 0)
{
// get direction information from left phase B
if(rc_dig_in06 != 0)
{
left_encoder_count++;
}
else
{
left_encoder_count--;
}
}
// look for a rising edge on right phase A
if(rc_dig_in02 != prior_right_phase_a_state && rc_dig_in02 != 0)
{
// get direction information from right phase B
if(rc_dig_in08 != 0)
{
right_encoder_count++;
}
else
{
right_encoder_count--;
}
}
// update state variables
prior_left_phase_a_state = rc_dig_in01;
prior_right_phase_a_state = rc_dig_in02;
Placed in the fast loop, this code will emulate the interrupt-driven code.
-Kevin