¿ªÔÆÌåÓý

Re: sbitx encoder contact bounce


Jack, W8TEE
 

Hi Evan:

What is the interpretation of the call to enc_read(&enc_b)? Most encoder ISR's return either 0 or 1. If that's the case, the ISR could be simplified to:

void tuning_isr(void) {
?? int tuning += enc_read(&enc_b);
}

However, if the encoders always return 0 or 1, the complex if statement blocks in enc_read() check for values 2 and 3, too. What's the interpretation of those values?


Also, the fragment:

newState = enc_state(e); // Get current state??
? ??
? if (newState != e->prev_state)
? ? ?delay (1);
??
? if (enc_state(e) != newState || newState == e->prev_state)
? ? return 0;?

Can newState ever be different than e->prev_state? If not, is the check even necessary?

Jack, W8TEE



On Monday, February 26, 2024 at 11:30:33 AM EST, Evan Hand <elhandjr@...> wrote:


Farhan,

In reviewing the tuning encoder, I found the function tuning_isr(void):

static int tuning_ticks = 0;
void tuning_isr(void){
int tuning = enc_read(&enc_b);
if (tuning < 0)
tuning_ticks++;
if (tuning > 0)
tuning_ticks--;
}

When the interrupt is received, the following is called :

int enc_read(struct encoder *e) {
? int result = 0;?
? int newState;
??
? newState = enc_state(e); // Get current state??
? ??
? if (newState != e->prev_state)
? ? ?delay (1);
??
? if (enc_state(e) != newState || newState == e->prev_state)
? ? return 0;?
?
? //these transitions point to the encoder being rotated anti-clockwise
? if ((e->prev_state == 0 && newState == 2) ||?
? ? (e->prev_state == 2 && newState == 3) ||?
? ? (e->prev_state == 3 && newState == 1) ||?
? ? (e->prev_state == 1 && newState == 0)){
? ? ? e->history--;
? ? ? //result = -1;
? ? }
? //these transitions point to the enccoder being rotated clockwise
? if ((e->prev_state == 0 && newState == 1) ||?
? ? (e->prev_state == 1 && newState == 3) ||?
? ? (e->prev_state == 3 && newState == 2) ||?
? ? (e->prev_state == 2 && newState == 0)){
? ? ? e->history++;
? ? }
? e->prev_state = newState; // Record state for next pulse interpretation
? if (e->history > e->speed){
? ? result = 1;
? ? e->history = 0;
? }
? if (e->history < -e->speed){
? ? result = -1;
? ? e->history = 0;
? }
? return result;
}

The only delay function is the?

?if (newState != e->prev_state)
? ? ?delay (1);

That is using delay and not the same as what Jack has suggested.? Still, the delay is active for only one millisecond.? If this would need to be increased, it would be better to use the construct that Jack has suggested.? Other delays in the code do use the millis() function.?

Please let me know if I have missed something so I can understand the code better.

73
Evan
AC9TU

--
Jack, W8TEE

Join [email protected] to automatically receive all group messages.