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

Files at this revision

API Documentation at this revision

Comitter:
Generic
Date:
Fri Sep 16 03:43:22 2016 +0000
Parent:
1:2b21453e2c03
Commit message:
Initial working version

Changed in this revision

AS5045Controller.cpp Show annotated file Show diff for this revision Revisions of this file
AS5045Controller.hpp Show annotated file Show diff for this revision Revisions of this file
diff -r 2b21453e2c03 -r 02ea2289edb2 AS5045Controller.cpp
--- a/AS5045Controller.cpp	Fri Sep 16 04:56:51 2016 +0200
+++ b/AS5045Controller.cpp	Fri Sep 16 03:43:22 2016 +0000
@@ -1,34 +1,29 @@
 #include "AS5045Controller.hpp"
 
-AS5045Controller::AS5045Controller(PinName CS, int f, PinName D0, PinName CLK) :
-  _spi(NC,DO,CLK),
-  _cs(CS);
+AS5045Controller::AS5045Controller(PinName CS) :
+  _spi(NC,D12, D13),
+  _cs(CS)
 {
   _spi.format(9,2);
-  _spi.frequency(f);
+  _spi.frequency(500000);
   _cs = 1;
-  _valid = false;
 }
 
-int AS5045Controller::Read()
+int AS5045Controller::GetInt()
 {
   _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;
+  return ((upper << 3)+(lower >> 6));
 }
 
-bool AS5045Controller::IsValid()
+float AS5045Controller::GetFloat()
 {
-  return _valid;
+  float value = (float)GetInt();
+
+  return value*0.08789;
+
 }
diff -r 2b21453e2c03 -r 02ea2289edb2 AS5045Controller.hpp
--- a/AS5045Controller.hpp	Fri Sep 16 04:56:51 2016 +0200
+++ b/AS5045Controller.hpp	Fri Sep 16 03:43:22 2016 +0000
@@ -1,75 +1,21 @@
 #ifndef AS5045CONTROLLER_H
 #define AS5045CONTROLLER_H
 
-//Includes
 #include "mbed.h"
 
-/**
- * Reads data from a AS5045 Magnetic Rotary Encoder using the SPI interface.
- *
- * AS5045 Datasheet : http://ams.com/eng/content/download/1288/7223
- * @author CA Bezuidenhout
- * @section USAGE EXAMPLE
- * @code
- * #include "mbed.h"
- * #include "AS5045Controller.hpp"
- *
- * AS5045Controller mySensor(D2);
- * Serial pc(USBTX,USBRX);
- *
- * int main()
- * {
- *  while(1)
- *  {
- *    int sensorValue = mySensor.Read();
- *
- *    if( mySensor.IsValid() )
- *      pc.printf("Sensor Value = %d\n\r",sensorValue );
- *    else
- *      pc.printf("Invalid data read");
- *
- *    Thread::wait(500);
- *  }
- * @endcode
- */
 class AS5045Controller
 {
 public:
-  /**
-   * @param cs : Chip select pin
-   * @param f : SPI frequency (default 500kHz)
-   * @param DO : DO pin of AS5045 (default D12)
-   * @param CLK : CLK pin of AS5045 (default D13)
-   */
-  AS5045Controller(PinName CS, int f = 5000000, PinName DO = D12, PinName CLK = D13);
+  AS5045Controller(PinName cs);
 
-  /**
-  * Read data from the device
-  *
-  * @return int value read
-  */
-  int Read();
-
-  /**
-  * Check if read data is valid
-  *
-  * @return true if data is valid
-  */
-  int IsValid();
+  int GetInt();
+  float GetFloat();
 
 private:
   SPI _spi;
   DigitalOut _cs;
-  bool _magDEC;
-  bool _magINC;
-  bool _lin;
-  bool _cof;
-  bool _ocf;
-  bool _valid;
-  int _value;
-  static const float MAX_VALUE = 4095.0f;
+
+  static const float MAX_VALUE = 4095;
 
 };
-
-
 #endif