A short demo code for AS5311 with SSI protocol and Arduino UNO.
Test performed with SSI protocol on the demo board AS5311-TS_EK_AB and 1mm pole length magnet strip provided with the kit.
I didn't find a good example online so here is mine.
I only find this library online: GitHub - muerzi/AS5311: Arduino Library for ams ag AS5311 12bit linear position sensor
There is 10ms delays in it which are not necessary because Read-out frequency is 1MHZ
I use shiftIn in the code rather than digitalRead
00:00 With magnet away mag is weak ~250, control code is 101 and position is random
00:05 With magnet on the AS5030 mag=1023 and pos=3135
00:08 Position overflow when pos over 4096
Sorry, the comment in the code are mainly in french.
//Only 6 wire to connectr
#define DO 2
#define CLK 3
#define CS 4
//GND: GND
//Prog: GND
//5V: 5V
void setup() {
pinMode(DO, INPUT);
pinMode(CLK, OUTPUT);
pinMode(CS, OUTPUT);
digitalWrite(CLK, 1); //CLK
digitalWrite(CS, 1); //Csn
Serial.begin(9600);
delay(1000);
}
void loop() {
//lecture pos shiftin
digitalWrite(CLK, 1); //position-> CLK=1 avant CS=0
digitalWrite(CS, 0); //debut lecture
digitalWrite(CLK, 0); //passage à 0 pour lecture sur front montant
unsigned int pos1 = shiftIn(DO, CLK, MSBFIRST); // lecture un octet
unsigned int pos2 = shiftIn(DO, CLK, MSBFIRST); //lecture octet suivant
digitalWrite(CS, 1); //fin de lecture
unsigned int pos = (pos1 << 4) + (pos2 >> 4); //assemblage des 2 octets:on garde les 12 premiers bits lus
Serial.print("pos:" + (String)pos);
Serial.print(",");
//lecture mag shiftin
digitalWrite(CLK, 0); //magnitude->CLK=0 avant CS=0
digitalWrite(CS, 0); //debut lecture
unsigned int mag1 = shiftIn(DO, CLK, MSBFIRST);
unsigned int mag2 = shiftIn(DO, CLK, MSBFIRST);
digitalWrite(CS, 1); //fin de lecture
unsigned int mag = (mag1 << 4) + (mag2 >> 4); //assemblage des 2 octets:on garde les 12 premiers bits lus
Serial.print("mag:" + (String)mag);
Serial.print(",");
//Data validity verification
//Must be 100 for valid data
//OCF (Offset Compensation Finished), logic high indicates the finished Offset Compensation Algorithm. If this bit is not set, the data at D11:D0 (likewise M11:M0) may be invalid.
//COF (Cordic Overflow), logic high indicates an out of range error in the CORDIC part. When this bit is set, the data at D11:D0 (likewise M11:M0) is invalid.
//LIN (Linearity Alarm), logic high indicates that the input field generates a critical output linearity.
//Note: La meme opération est possible sur pos pour extraire OCF+COF+LIN
Serial.print("control:");
Serial.print((mag2 >> 1) & (0b111), BIN); //OCF+COF+LIN -- 100=OK
//fin
Serial.println();
//delay(1000);
}
The code may be improved with SPI
No help needed here(my test is fully functional) but comments or improvements welcome!