AS5050 / AS5055 Magnetic Rotary Encoder - SPI

Hey people!

I'm trying to connect a AS5050 / AS5055 Magnetic Rotary Encoder to my arduino mega but I must admit I'm not a programming guy!
So I was wondering if someone could give me some help writing the code...?

This is the code in C... does anyone has a "arduino" translation :slight_smile:
this is de dataset of chip ams OSRAM a global leader in optical solutions | ams OSRAM

and this is de dataset of adapter for for this chip ams OSRAM a global leader in optical solutions | ams OSRAM.

/*!


  • Reads out chip data via SPI interface
  • This function is used to read out cordic value from chips supporting SPI
  • interface.

/
#define SPI_CMD_READ 0x8000 /
!< flag indicating read attempt when using SPI interface /
#define SPI_REG_DATA 0x7ffe /
!< data register when using SPI /
#define SPI_REG_AGC 0x7ff0 /
!< agc register when using SPI /
#define SPI_REG_CLRERR 0x6700 /
!< clear error register when using SPI */

void spiReadData()
{
u16 dat; // 16-bit data buffer for SPI communication
ushort angle, agcreg;
ubyte agc;
ushort value;
bit alarmHi, alarmLo;

/* Send READ AGC command. Received data is thrown away: this data comes from the precedent
command (unknown)/
dat = SPI_CMD_READ | SPI_REG_AGC;
dat |= spiCalcEvenParity(dat);
spiTransfer((u8
)&dat, sizeof(u16));

/* Send READ ANGLE command. Received data is the AGC value, from the precedent command /
dat = SPI_CMD_READ | SPI_REG_DATA;
dat |= spiCalcEvenParity(dat);
spiTransfer((u8
)&dat, sizeof(u16));
agcreg = dat;

/* Send NOP command. Received data is the ANGLE value, from the precedent command /
dat = 0x0000; // NOP command.
spiTransfer((u8
)&dat, sizeof(u16));
angle = dat >> 2;

}

if (((dat >> 1) & 0x1) || ((agcreg >> 1) & 0x1))
{
/* error flag set - need to reset it /
dat = SPI_CMD_READ | SPI_REG_CLRERR;
dat |= spiCalcEvenParity(dat);
spiTransfer((u8
)&dat, sizeof(u16));
}
else
{
agc = (agcreg >> 2) & 0x3f; // AGC value (0..63)
value = (dat >> 2) & 0x3fff; // Angle value (0..4095 for AS5055)
angle = (value * 360) / 4095; // Angle value in degree (0..359.9°)
alarmLo = (dat >> 14) & 0x1;
alarmHi = (dat >> 15) & 0x1;
}
}

/*!


  • Calculate even parity of a 16 bit unsigned integer
  • This function is used by the SPI interface to calculate the even parity
  • of the data which will be sent via SPI to the encoder.
  • \param[in] value : 16 bit unsigned integer whose parity shall be calculated
  • \return : Even parity

*/
static u8 spiCalcEvenParity(ushort value)
{
u8 cnt = 0;
u8 i;

for (i = 0; i < 16; i++)
{
if (value & 0x1)
{
cnt++;
}
value >>= 1;
}
return cnt & 0x1;
}

Did you solve it somehow? I'm working on the same thing right now, so if you have working code, it would be great to share it :slight_smile:

Thank you.

Here is a code for AS5055
This program reads data by SPI

//AS5055 READ DATA PROGRAM
//www.AleyRobotics.com, 2013
//Email: info@AleyRobotics.com

#include "SPI.h"
  unsigned int result = 0;
  unsigned int result1 = 0;
  unsigned int result2 = 0;

void setup ()
{
Serial.begin(9600);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV64); //you can chose faster SPI frequency 
}

void loop () {
  digitalWrite(SS, LOW);
  result1 = SPI.transfer(0xff);
  result2 = SPI.transfer(0xff);  
  digitalWrite(SS, HIGH);
  result1 &= 0b00111111;
  result1 = result1 << 8;
  result = (result1 | result2) >> 1;
  Serial.print("Result: ");
  Serial.print(" = ");
  Serial.println(result, DEC);
  delay(100);
}

Thanks so much AleyRobotics!

Here is a code for AS5048A
This program reads data by SPI

//AS5048A READ DATA PROGRAM
//www.AleyRobotics.com 2013

#include "SPI.h"
  unsigned int result = 0;
  unsigned int result1 = 0;
  unsigned int result2 = 0;

void setup ()
{
Serial.begin(9600);


SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV64); 
}

void loop () {
  digitalWrite(SS, LOW);
  result1 = SPI.transfer(0xff);
  result2 = SPI.transfer(0xff);  
  digitalWrite(SS, HIGH);
  result1 &= 0b00111111;
  result1 = result1 << 8;
//  result = (result1 | result2)>>1;//drop last bit for low noise (13 bit resolution)
//  result = (result1 | result2)>>2;//drop last bits for low noise (12 bit resolution)
  result = (result1 | result2);//original resolution (FULL 14 bit resolution)
  Serial.print("Result: ");
  Serial.print(" = ");
  Serial.println(result, DEC);
  delay(10);
}