Today's morning started from AS5140 commissioning.
You can see wiring connection on the pictures below:
Here is simple arduino sketch:
/*
This scetch was developed buy Ing. Dmitry Kochubey
in Technical university of Liberec
Czech Republic
year 2013
*/
#define cs 2
#define clk 3
#define din 4
#define prog 5
unsigned short data = 0;
unsigned short stat = 0;
unsigned short pos = 0;
boolean OCF = false;
boolean COF = false;
boolean LIN = false;
boolean MagINCn = false;
boolean MagDECn = false;
boolean EvenParity = false;
void AS5140_Init(void)
{
pinMode(cs, OUTPUT);
pinMode(clk, OUTPUT);
pinMode(prog, OUTPUT);
pinMode(din, INPUT);
digitalWrite(din, LOW);
digitalWrite(cs, LOW);
digitalWrite(clk, HIGH);
digitalWrite(prog, LOW); //or you can connect prog to the ground
delayMicroseconds(50);
}
unsigned short AS5140_Ssi(void)
{
unsigned short i, data;
digitalWrite(cs, LOW);
digitalWrite(clk, LOW);
data = 0;
for (i = 0; i < 16; i++)
{
digitalWrite(clk, HIGH);
delayMicroseconds(1);
digitalWrite(clk, LOW);
data = data << 1; // shift bits in target variable one to the left
if ( digitalRead(din) ) // read port bit
{
data |=0x01; // set new bit in 16 bit variable
}
delayMicroseconds(1);
}
digitalWrite(cs, HIGH);
return data;
}
void setup() {
AS5140_Init();
Serial.begin(9600);
}
void loop() {
data = AS5140_Ssi();
stat = (data & 0b0000000000111111);
OCF = data & 32;
COF = data & 16;
LIN = data & 8;
MagINCn = data & 4;
MagDECn = data & 2;
EvenParity = data & 1;
Serial.print("Status=");
Serial.println(stat,BIN);
if ((stat>>1)==0b10000) //data validation check
{
pos = (data & 0b11111111111000000) >> 6;
Serial.print("Position=");
Serial.println(pos,DEC);
}
delay(100);
}
Enjoy!
No comments:
Post a Comment