AS5048A SPI - Magnetic rotary encoder library

Library for communication over the SPI interface with the ams.com AS5048A magnetic rotary encoder.

Example:

Simple readout of a single angle measurement from a single sensor on the SPI-bus

#include "mbed.h"
#include <as5048spi.h>

// The sensors connection are attached to pins 5-8
As5048Spi sensor(p5, p6, p7, p8);
Serial pc(USBTX, USBRX); // tx, rx

int main() {
    while(1) {
        // 
        const int* angles = sensor.read_angle();
        int angle = angles[0];
        
        // The read angle returns the value returned over the SPI bus, including parity bit
        pc.printf("Read result: %x\r\n", angle);
        
        if( As5048Spi::parity_check(angle) )
        {
            // Convert range from 0 to 2^14-1 to 0 - 360 degrees
            int degrees = As5048Spi::degrees(angle)/100;
            pc.printf("Parity check succesfull.\r\n");
            pc.printf("Angle: %i degrees\r\n", degrees );
        }
        else
        {
            pc.printf("Parity check failed.\r\n");
        }
            
        wait_ms(500);
    }
}

The class supports daisy chaining multiple sensors on the SPI-bus as well. See: SPI-Daisy-Chaining.

Revision:
1:f2ed22514649
Parent:
0:3edcf58e51e7
Child:
2:2958500883e0
diff -r 3edcf58e51e7 -r f2ed22514649 as5048spi.cpp
--- a/as5048spi.cpp	Thu Sep 18 10:20:22 2014 +0000
+++ b/as5048spi.cpp	Thu Sep 18 19:28:25 2014 +0000
@@ -1,9 +1,5 @@
 #include "as5048spi.h"
 
-
-
-
-
 As5048Spi::As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int ndevices) :
     _nDevices(ndevices),
     _chipSelectN(chipselect),
@@ -17,7 +13,7 @@
     _spi.format(16, 1);
     
     // Set clock frequency to 1 MHz (max is 10Mhz)
-    _spi.frequency(10000000);
+    _spi.frequency(1000000);
     
     _readBuffer = new int[ndevices];
 }
@@ -108,7 +104,7 @@
         wait_us(1); // Wait at least 350ns after chip select
         _readBuffer[0] = _spi.write(command);
         _chipSelectN.write(1);
-        wait_us(1); // Wait at least 350ns after chip select
+        wait_us(1); // Wait at least 350ns after chip select       
     } else
     {
         // Enable the sensor on the chain