Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JamesNewton/0ef27804defa2ee33246a77f3280b7f6 to your computer and use it in GitHub Desktop.
Save JamesNewton/0ef27804defa2ee33246a77f3280b7f6 to your computer and use it in GitHub Desktop.
Arduino code to read the angle register from the MassMind.org ENC1 via 3 wire read only mode.
/*
Source from
http://forum.arduino.cc/index.php?topic=78881.msg1375885#msg1375885
* Ahmad Byagowi (ahmadexp@gmail.com)
* Date : Aug 31, 2013
* All code (c)2013 Ahmad Byagowi Consultant. all rights reserved
20170508 Minor edits to improve clarity by James Newton.
*/
//c2 pin tied to ground for 3 wire mode
int cs = A3;
int dio = A5; //pull down resistor (10K-100K) required
int clk = A4;
void init_as5134(void){
digitalWrite(cs, LOW);
digitalWrite(clk, LOW);
pinMode(cs, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(dio, INPUT);
}
int read_as5134(void){
int clock_p;
unsigned int data=0;
digitalWrite(cs,LOW);
digitalWrite(clk,LOW);
digitalWrite(cs,HIGH);
delay(1);
for(clock_p=21;clock_p--;clock_p>0){
digitalWrite(clk,LOW);
delay(1);
digitalWrite(clk,HIGH);
delay(1);
if(clock_p<9){
if(digitalRead(dio)) {
data+=(1<<(clock_p));
}
}
}
digitalWrite(clk,LOW);
digitalWrite(cs,LOW);
return data;
}
// the setup routine runs once when you press reset:
void setup() {
init_as5134();
Serial.begin(115200);
}
// the loop routine runs over and over again forever:
void loop() {
Serial.println(read_as5134());
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment