Quote:
|
Originally Posted by Astronouth7303
None of that is going to work if you're going at any resonable speed. It's going to miss ticks. Use interupts.
|
If you use a quadrature encoder you don't need the state machine to decode its output. Connect channel A to an interrupt configured to fire on the rising edge. Connect channel B to a regular digital input (input 10 in the example). Your ISR becomes one line:
Code:
void Int_1_Handler()
{
Distance += (rc_dig_in10) ? 1 : -1;
}
The outputs follow the pattern AB = 00-10-11-01-00... If you look at the rising edges for channel A, going from left to right, channel B is 0. Looking at the rising edge of A from the other direction and channel B is 1. If the direction is reversed you can swap the inputs or change the signs in the ISR.