I'm stuck in my project, I try to read an absolute encoders angular position with my Arduino. I used and cabled a differential line receiver and driver between Arduino and the encoder, like explained in the 2 following links:
Figure 2.1 here
Here are the specifications of the encoder that I try to read.
Here is my Arduino code :
const int CLOCK_PIN = 5;
const int DATA_PIN = 6;
const int BIT_COUNT = 25;
void setup() {
//setup our pins
pinMode(DATA_PIN, INPUT);
pinMode(CLOCK_PIN, OUTPUT);
//give some default values
digitalWrite(CLOCK_PIN, HIGH);
Serial.begin(115200);
}
void loop() {
float reading = readPosition();
if (reading >= -0.5) {
//Serial.print("Reading: ");
//Serial.println(reading,2);
}
delay(200);
}
//read the current angular position
float readPosition() {
unsigned long sample1 = shiftIn(DATA_PIN, CLOCK_PIN, BIT_COUNT);
unsigned long sample2 = graytobin(sample1, BIT_COUNT);
delayMicroseconds(25); // Clock mus be high for 20 microseconds before a new sample can be taken
Serial.print("grey: ");
Serial.println(sample1, BIN);
Serial.print("binary: ");
Serial.println(sample2, BIN);
Serial.print("decimal: ");
Serial.println(sample2 & 0x0FFF);
Serial.print("\n");
return ((sample2 & 0x0FFF) * 360UL) / 4096.0;
}
// gray to binary
int graytobin(int grayVal, int nbits ) {
int binVal = 0;
bitWrite(binVal, nbits - 1, bitRead(grayVal, nbits - 1)); // MSB stays the same
for (int b = nbits - 1; b > 0; b-- ) {
// XOR bits
if (bitRead(binVal, b) == bitRead(grayVal, b - 1)) { // binary bit and gray bit-1 the same
bitWrite(binVal, b - 1, 0);
}
else {
bitWrite(binVal, b - 1, 1);
}
}
return binVal;
}
//read in a byte of data from the digital input of the board.
unsigned long shiftIn(const int data_pin, const int clock_pin, const int bit_count) {
unsigned long data = 0;
for (int i=0; i<BIT_COUNT; i++) {
data <<= 1;
digitalWrite(clock_pin,LOW);
delayMicroseconds(1);
digitalWrite(clock_pin,HIGH);
delayMicroseconds(1);
data |= digitalRead(data_pin);
}
return data;
}
I'm still able to receive information with my Arduino from the encoder but they are totally random. Here is an example of the output I get (I didn't move the encoder during this exercition):
grey: 1001111101110100011111001
binary: 11111111111111111011000010101110
decimal: 174
grey: 111110110010001111100111
binary: 11110101000101
decimal: 3397
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 1111111111111111111111111
binary: 11111111111111111010101010101010
decimal: 2730
grey: 1011001110011011100010001
binary: 10010111100001
decimal: 1505
grey: 1100111011011100100011111
binary: 11111111111111111101000111101010
decimal: 490
grey: 1101110110111001000111111
binary: 101110000101010
decimal: 3114
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 111111
binary: 101010
decimal: 42
grey: 111011011100100011100111
binary: 11111111111111111000111101000101
decimal: 3909
grey: 1100111001101110010001111
binary: 11111111111111111001011100001010
decimal: 1802
grey: 1100111011011100100011111
binary: 11111111111111111101000111101010
decimal: 490
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
grey: 0
binary: 0
decimal: 0
I don't know how to diagnose the problem, and how to find it.