Hello all.
I have a logic problem with my implementation of a base-6 counter. The problem I have is that due to constraints in the system I am programming, I can only change a single digit at a time (much like a grey code).
My implementation therefore counts first up, incrementing the next significant digit, then back down, incrementing the next significant digit, like this:
000, 001, 002, 003, 004, 005,
015, 014, 013, 012, 011, 010,
020...
My problem is that it works fine for two digits, but when I get to 050 - I am stuck with how to implement the logic to then increment the higher significant digit (to 150 and onwards) - it's driving me totally nuts.
The current implementation has two 32 byte arrays, with each byte representing a digit of the counter. The first array is for the main counter digits; the second is for the direction table for each digit, so as to indicate if each byte is being incremented or decremented.
Any help or pointers would be greatly appreciated.Code:Code snippet: unsigned char dPOSITION[32]; // contains each digits value unsigned char iPOSITION[32]; // contais flag for direction (0=+/1=-) int j=0; for(i=0;i<MAX_COUNT;i++) { j=0 { if ( (dPOSITION[j]==5) && (iPOSITION[j]==0) ) { iPOSITION[j]=1; j++; } if ( (dPOSITION[j]==0) && (iPOSITION[j]==1) ) { iPOSITION[j]=0; j++; } if (iPOSITION[j]==0) dPOSITION[j]++; if (iPOSITION[j]==1) dPOSITION[j]--; } }
FF.