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.
Diff: as5048spi.h
- Revision:
- 3:a8ad32e439d4
- Parent:
- 2:2958500883e0
- Child:
- 4:06b89a41109e
--- a/as5048spi.h Thu Dec 04 10:00:42 2014 +0000 +++ b/as5048spi.h Tue Mar 17 14:46:45 2015 +0000 @@ -6,7 +6,7 @@ typedef enum { AS_CMD_NOP = 0x0000, - AS_CMD_ERROR = 0x0001 | AS_FLAG_READ, // Reads error ireguster and clear error flags + AS_CMD_ERROR = 0x0001 | AS_FLAG_READ, // Reads error register of sensor and clear error flags AS_CMD_DIAGNOSTICS = 0x3FFD | AS_FLAG_READ, // Reads automatic gain control and diagnostics info AS_CMD_MAGNITUDE = 0x3FFE | AS_FLAG_READ, @@ -29,8 +29,18 @@ ~As5048Spi(); bool error(int device = -1); + + /// Sets the SPI clock frequency in Hz. Maximum tested frequency is 10MHz. void frequency(int frequency = 1000000); + + /// Sends a read command to the sensor. const int* read(As5048Command command); + + /// Sends a read command to the sensor. + /// A call to this function will not directly return the requested value. The + /// requested value will be returned in a next read_sequential call. + /// Use this function to read sensor values with minimum speed impact on SPI-bus + /// and microcontroller. const int* read_sequential(As5048Command command); /// Performs a single angle measurement on all sensors @@ -49,11 +59,19 @@ /// Returns lowest 14-bits static int mask(int sensor_result); + + /// Applies the mask to the first n bytes in the read buffer (for daisychained sensors). static void mask(int* sensor_results, int n); /// Checks if the return value from the sensor has the right parity /// @return true if ok static bool parity_check(int sensor_result); + + /// Returns an angle from 0 to 36000 (degrees times 100). + /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential static int degrees(int sensor_result); + + /// Returns an angle from 0 to 2*PI*100 + /// @param sensor_result is one of the values returned by read_angle or read_angle_sequential static int radian(int sensor_result);