A library for the AMS AS5045 magnetic encoder using the SPI interface

AS5045Controller.cpp

Committer:
Kerneels Bezuidenhout
Date:
2016-09-16
Revision:
1:2b21453e2c03
Parent:
0:f2ebf8862df5
Child:
2:02ea2289edb2

File content as of revision 1:2b21453e2c03:

#include "AS5045Controller.hpp"

AS5045Controller::AS5045Controller(PinName CS, int f, PinName D0, PinName CLK) :
  _spi(NC,DO,CLK),
  _cs(CS);
{
  _spi.format(9,2);
  _spi.frequency(f);
  _cs = 1;
  _valid = false;
}

int AS5045Controller::Read()
{
  _cs = 0;
  int upper = _spi.write(0x00);
  int lower = _spi.write(0x00);
  _cs = 1;

  _magDEC = (lower >> 1) & 1;
  _magINC = (lower >> 2) & 1;
  _lin = (lower >> 3) & 1;
  _cof = (lower >> 4) & 1;
  _ocf = (lower >> 5) & 1;
  _valid = (_ocf & !_cof & !_lin & (_magINC | _magDEC) );
  _value = ((upper << 3)+(lower >> 6));

  return _value;
}

bool AS5045Controller::IsValid()
{
  return _valid;
}