Forked from JamesNewton/MassMind.org ENC1 3Wire ReadAngle Arduino
Created
March 11, 2018 23:16
-
-
Save cnc4less/9b4a12264962e46933fac397642f931e 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Source from | |
http://forum.arduino.cc/index.php?topic=78881.msg1375885#msg1375885 | |
* Ahmad Byagowi ([email protected]) | |
* 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