Skip to content

Instantly share code, notes, and snippets.

@pstiasny
Created December 7, 2025 14:19
Show Gist options
  • Select an option

  • Save pstiasny/c7a34487f10e7f81f96c4d1e09ae4b95 to your computer and use it in GitHub Desktop.

Select an option

Save pstiasny/c7a34487f10e7f81f96c4d1e09ae4b95 to your computer and use it in GitHub Desktop.
volatile uint8_t encoder_prev = 0;
volatile int8_t encoder_delta = 0;
static uint8_t read_encoder(void) {
return (ENC_A_GetValue() << 1) | ENC_B_GetValue();
}
void encoder_interrupt(void) {
uint8_t index = (encoder_prev << 2) | read_encoder();
switch (index) {
// no change
case 0b0000:
case 0b0101:
case 0b1111:
case 0b1010:
// illegal change - 2 bits change
case 0b0011:
case 0b0110:
case 0b1100:
case 0b1001:
break;
case 0b0001:
case 0b0111:
case 0b1000:
case 0b1110:
encoder_delta--;
break;
case 0b0010:
case 0b0100:
case 0b1011:
case 0b1101:
encoder_delta++;
break;
}
encoder_prev = index & 0b11;
}
int main(void)
{
// ..
encoder_prev = read_encoder();
ENC_A_SetInterruptHandler(encoder_interrupt);
ENC_B_SetInterruptHandler(encoder_interrupt);
// ..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment