Pages: [1]   Go Down
Author Topic: [Solved] Reading an Absolute Encoder (Bourns EMS22A)  (Read 2607 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,
I'm having problems reading an Bourns EMS22A Absolute Encoder. It outputs a stream of bitwise data which is controlled by a clock line. you can find the Data Sheet here.
I connected the Pin 6 & 1(chip select & digital input) to ground since I only have 1 encoder and don't need to daisy chain anything.
Here my test code to test the encoder, but somehow it only outputs zeros and I'm not sure why:
Code:
const int PIN_CLOCK = 6;
const int PIN_OUTPUT = 7;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_CLOCK, OUTPUT);
  pinMode(PIN_OUTPUT, INPUT);
  digitalWrite(PIN_CLOCK, HIGH);
}

int last_output = -1;

void loop() {
  delay(1000);
  for (int i=0; i<16; i++) {
    digitalWrite(PIN_CLOCK, LOW);
    digitalWrite(PIN_CLOCK, HIGH);
    Serial.print(digitalRead(PIN_OUTPUT));
  }
  digitalWrite(PIN_CLOCK, LOW);
  digitalWrite(PIN_CLOCK, HIGH);
  Serial.println("");
}
Maybe some one can figure out what I'm doing wrong.
« Last Edit: June 18, 2012, 10:09:44 am by sepal » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It SAYS you can connect CS to GND but all of the timing diagrams show the sample being taken when you drop CS.  smiley-sad
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 29
Posts: 2239
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

On the second page of the datasheet the timing diagram shows that the CSn pin must be high for a minimum of 500ns between readings.

Your datasheet link didn't work, so I'm reading it from a random "datasheet archive" site.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I figured, that the cs pin would only  if you daisy chain several encoders together. I tried it anywhy but it didn't changened anything:
Code:
const int PIN_CS = 5;
const int PIN_CLOCK = 6;
const int PIN_OUTPUT = 7;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_CS, OUTPUT);
  pinMode(PIN_CLOCK, OUTPUT);
  pinMode(PIN_OUTPUT, INPUT);
  
  digitalWrite(PIN_CLOCK, HIGH);
  digitalWrite(PIN_CS, LOW);
}

void loop() {
  delay(1000);
  digitalWrite(PIN_CS, HIGH);
  delayMicroseconds(1);
  digitalWrite(PIN_CS, LOW);
  delayMicroseconds(1);
  
  for (int i=0; i<16; i++) {
    digitalWrite(PIN_CLOCK, LOW);
    digitalWrite(PIN_CLOCK, HIGH);
    Serial.print(digitalRead(PIN_OUTPUT));
  }
  digitalWrite(PIN_CLOCK, LOW);
  digitalWrite(PIN_CLOCK, HIGH);
  Serial.println("");
}
BTW: I updated the links in my post above.
« Last Edit: June 16, 2012, 02:48:59 am by sepal » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Seems I just confused with the wiring, since it works now smiley-razz
Thanks for the help anyway.

btw: The ChipSelect pin is realy necessary. Here is my example code in case somebody else wants to tinker with this type of encoders:

Code:
const int PIN_CS = 5;
const int PIN_CLOCK = 6;
const int PIN_DATA = 7;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_CS, OUTPUT);
  pinMode(PIN_CLOCK, OUTPUT);
  pinMode(PIN_DATA, INPUT);

  digitalWrite(PIN_CLOCK, HIGH);
  digitalWrite(PIN_CS, LOW);
}


//byte stream[16];
void loop() {

  digitalWrite(PIN_CS, HIGH);
  digitalWrite(PIN_CS, LOW);
  int pos = 0;
  for (int i=0; i<10; i++) {
    digitalWrite(PIN_CLOCK, LOW);
    digitalWrite(PIN_CLOCK, HIGH);
   
    byte b = digitalRead(PIN_DATA) == HIGH ? 1 : 0;
    pos += b * pow(2, 10-(i+1));
  }
  for (int i=0; i<6; i++) {
    digitalWrite(PIN_CLOCK, LOW);
    digitalWrite(PIN_CLOCK, HIGH);
  }
  digitalWrite(PIN_CLOCK, LOW);
  digitalWrite(PIN_CLOCK, HIGH);
  Serial.println(pos);
}
Logged

Pages: [1]   Go Up
Jump to: