How to handle encoder count overflow

Thread Starter

Embededd

Joined Jun 4, 2025
157
You don't need a very long 533-bit shift register.
I have dropped the monostable solution and went with the long shift register in software.
I see your two points, one saying we don’t need such a long 533-bit shift register, and another saying you already went with the long register in software.

if we really go with a long 533-bit shift register, how exactly would that be implemented? A normal integer variable only gives me 16 bits
 

drjohsmith

Joined Dec 13, 2021
1,609
I see your two points, one saying we don’t need such a long 533-bit shift register, and another saying you already went with the long register in software.

if we really go with a long 533-bit shift register, how exactly would that be implemented? A normal integer variable only gives me 16 bits
the possible magic words are
linked list
software fifo
stack
 

panic mode

Joined Oct 10, 2011
5,002
if we really go with a long 533-bit shift register, how exactly would that be implemented? A normal integer variable only gives me 16 bits
declare an array of integers that can hold 533 bits. then write some code that uses it...

533/16 =33.3125 so you need at least 34 elements. for start try something smaller such as 4 bytes and see what it takes to shift all data. do it on paper first to come up with a plan that works, then implement it in code using integers and scale it up

suppose you have data like this:
0 : 00000010
1: 00011001
2 : 00000010
3: 00010100

you KNOW that single shift would need to produce:
0 : 00000001
1: 00001100
2 : 10000001
3: 00001010

so what operations would you perform to do that? in which order?

how about setting up loop that goes from last element (3) to first element (0), then on each element performs two simple operations - first a right shift, then set MSB with LSB value of preceding element?
 

panic mode

Joined Oct 10, 2011
5,002
there are many ways to keep track of something like this, but TS is very inexperienced. he is of course free to decide how he wants to handle this though so far he needed a lot of examples and hand-holding. when it comes to simplicity, memory use, ability to see and optionally display buffered data, it will be hard to beat bit-vector.

so here is one take on the whole thing
shift_data function is to be called every time encoder has moved the distance that corresponds to length of one part.
reject_sensed is a value from reject sensor. result of the function is used to trigger ejection
used resources (memory and code footprint) are minimal, no pointers at all, just one declaration for buffer and one short function that handles everything.

now, i would really appreciate if someone would show other implementations such as linked list.

Code:
int buffer[34] = {0}; // declare buffer array

bool shift_data(bool reject_sensed) {
  bool wrap;
  bool result;
  for(int n=33; n>0;n--){
    buffer[n]=buffer[n]>>1;
    if(n>0)
      wrap=(buffer[n-1] && 0x8000) != 0;
   else
      wrap = 0;
    if (wrap) {
        buffer[n]=buffer[n] || 0x8000;
    }
  }
    if (reject_sensed) {
      buffer[0]=buffer[0] || 0x8000;
    }
  return (buffer[34] && 0x0100) != 0; // bit 533
}
 
Last edited:
Top