http://www.pldesignline.com/196604078
http://www.et.byu.edu/groups/it548/C...s/encoders.pdf
http://en.wikipedia.org/wiki/Gray_code
From the above link:
Code:
// parameters: value, base, digits
// Convert a value to a graycode with the given base and digits. Iterating
// through a sequence of values would result in a sequence of graycodes in
// which only one digit changes at a time.
int baseN[digits]; // Stores the ordinary base-N number, one digit per entry
int gray[digits]; // Stores the base-N graycode number
// Put the normal baseN number into the baseN array. For base 10, 109
// would be stored as [9,0,1]
int power = 1;
for(i = digits - 1; i >= 0; i--) {
baseN[i] = (value / power) % base;
power *= base;
}
// Convert the normal baseN number into the graycode equivalent. Note that
// the loop starts at the most significant digit and goes down.
int shift = 0;
for(i = digits - 1; i >= 0; i--) {
// The gray digit gets shifted down equal to the sum of the higher
// digits.
gray[i] = (baseN[i] + base - shift) % base; // + base to prevent neg
shift += gray[i];
}
// EXAMPLES
// input: value = 1899, base = 10, digits = 4
// output: baseN[] = [9,9,8,1], gray[] = [0,1,7,1]
// input: value = 1900, base = 10, digits = 4
// output: baseN[] = [0,0,9,1], gray[] = [0,1,8,1]
looks like C, apparantly Gray code and Petherick code, run down similar roads. Sorry best I can do . . .
Bookmarks