Show Posts
Pages: [1]
1  Using Arduino / Sensors / Arduino UNO and CUI encoder (SPI ) on: April 06, 2013, 07:47:44 am
Good Day ALL

   I am trying to interface an absolute encoder with the Arduino. I have never worked with SPI, I have read information and understand the concept. I am just unclear when it comes to setting the code up. I found an example that someone had posted for the encoders they were working with , so I took the code and tried modifying it to work with the CUI encoder but am a bit lost. Please lend me a hand if you can. Note the code may not make much sense as it has some pieces of the old code basically I added the pins for MOSI , MISO, and CS.  


Code:
const int PIN_CS = 4;
const int PIN_CLOCK = 13;
const int PIN_MOSI =11;
const int PIN_MISO = 12;

void setup() {
  Serial.begin(9600);
  pinMode(PIN_CS, OUTPUT);
  pinMode(PIN_CLOCK, OUTPUT);
  pinMode(PIN_MOSI, OUTPUT);
  pinMode(PIN_MISO, 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_MOSI) == 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);
}  

 I appreciate any help/examples that you guys can offer.

  JH

 encoder
Code:
http://www.cui.com/Product/Components/Encoders/Absolute_Encoders/Modular/AMT203-V
Pages: [1]