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.
as5048spi.cpp@0:3edcf58e51e7, 2014-09-18 (annotated)
- Committer:
- JSpikker
- Date:
- Thu Sep 18 10:20:22 2014 +0000
- Revision:
- 0:3edcf58e51e7
- Child:
- 1:f2ed22514649
Version 0.3: reads single sensor value, 10000 samples speed test at 10MHz SPI, writes data to local storage
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JSpikker | 0:3edcf58e51e7 | 1 | #include "as5048spi.h" |
JSpikker | 0:3edcf58e51e7 | 2 | |
JSpikker | 0:3edcf58e51e7 | 3 | |
JSpikker | 0:3edcf58e51e7 | 4 | |
JSpikker | 0:3edcf58e51e7 | 5 | |
JSpikker | 0:3edcf58e51e7 | 6 | |
JSpikker | 0:3edcf58e51e7 | 7 | As5048Spi::As5048Spi(PinName mosi, PinName miso, PinName sclk, PinName chipselect, int ndevices) : |
JSpikker | 0:3edcf58e51e7 | 8 | _nDevices(ndevices), |
JSpikker | 0:3edcf58e51e7 | 9 | _chipSelectN(chipselect), |
JSpikker | 0:3edcf58e51e7 | 10 | _spi(mosi, miso, sclk) |
JSpikker | 0:3edcf58e51e7 | 11 | { |
JSpikker | 0:3edcf58e51e7 | 12 | _chipSelectN.write(1); |
JSpikker | 0:3edcf58e51e7 | 13 | // AS5048 needs 16-bits for is commands |
JSpikker | 0:3edcf58e51e7 | 14 | // Mode = 1: |
JSpikker | 0:3edcf58e51e7 | 15 | // clock polarity = 0 --> clock pulse is high |
JSpikker | 0:3edcf58e51e7 | 16 | // clock phase = 1 --> sample on falling edge of clock pulse |
JSpikker | 0:3edcf58e51e7 | 17 | _spi.format(16, 1); |
JSpikker | 0:3edcf58e51e7 | 18 | |
JSpikker | 0:3edcf58e51e7 | 19 | // Set clock frequency to 1 MHz (max is 10Mhz) |
JSpikker | 0:3edcf58e51e7 | 20 | _spi.frequency(10000000); |
JSpikker | 0:3edcf58e51e7 | 21 | |
JSpikker | 0:3edcf58e51e7 | 22 | _readBuffer = new int[ndevices]; |
JSpikker | 0:3edcf58e51e7 | 23 | } |
JSpikker | 0:3edcf58e51e7 | 24 | |
JSpikker | 0:3edcf58e51e7 | 25 | As5048Spi::~As5048Spi() |
JSpikker | 0:3edcf58e51e7 | 26 | { |
JSpikker | 0:3edcf58e51e7 | 27 | delete [] _readBuffer; |
JSpikker | 0:3edcf58e51e7 | 28 | } |
JSpikker | 0:3edcf58e51e7 | 29 | |
JSpikker | 0:3edcf58e51e7 | 30 | int As5048Spi::degrees(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 31 | { |
JSpikker | 0:3edcf58e51e7 | 32 | return mask(sensor_result) * 36000 / 0x4000; |
JSpikker | 0:3edcf58e51e7 | 33 | } |
JSpikker | 0:3edcf58e51e7 | 34 | |
JSpikker | 0:3edcf58e51e7 | 35 | |
JSpikker | 0:3edcf58e51e7 | 36 | int As5048Spi::radian(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 37 | { |
JSpikker | 0:3edcf58e51e7 | 38 | return mask(sensor_result) * 62832 / 0x4000; |
JSpikker | 0:3edcf58e51e7 | 39 | } |
JSpikker | 0:3edcf58e51e7 | 40 | |
JSpikker | 0:3edcf58e51e7 | 41 | bool As5048Spi::error(int device) |
JSpikker | 0:3edcf58e51e7 | 42 | { |
JSpikker | 0:3edcf58e51e7 | 43 | if( device == -1 ) { |
JSpikker | 0:3edcf58e51e7 | 44 | for(int i = 0; i < _nDevices; ++i) { |
JSpikker | 0:3edcf58e51e7 | 45 | if( _readBuffer[i] & 0x4000 ) { |
JSpikker | 0:3edcf58e51e7 | 46 | return true; |
JSpikker | 0:3edcf58e51e7 | 47 | } |
JSpikker | 0:3edcf58e51e7 | 48 | } |
JSpikker | 0:3edcf58e51e7 | 49 | } else if( device < _nDevices ) { |
JSpikker | 0:3edcf58e51e7 | 50 | return (_readBuffer[device] & 0x4000) == 0x4000; |
JSpikker | 0:3edcf58e51e7 | 51 | } |
JSpikker | 0:3edcf58e51e7 | 52 | return false; |
JSpikker | 0:3edcf58e51e7 | 53 | } |
JSpikker | 0:3edcf58e51e7 | 54 | |
JSpikker | 0:3edcf58e51e7 | 55 | |
JSpikker | 0:3edcf58e51e7 | 56 | void As5048Spi::frequency(int hz) |
JSpikker | 0:3edcf58e51e7 | 57 | { |
JSpikker | 0:3edcf58e51e7 | 58 | _spi.frequency(hz); |
JSpikker | 0:3edcf58e51e7 | 59 | } |
JSpikker | 0:3edcf58e51e7 | 60 | |
JSpikker | 0:3edcf58e51e7 | 61 | int As5048Spi::mask(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 62 | { |
JSpikker | 0:3edcf58e51e7 | 63 | return sensor_result & 0x3FFF; // return lowest 14-bits |
JSpikker | 0:3edcf58e51e7 | 64 | } |
JSpikker | 0:3edcf58e51e7 | 65 | |
JSpikker | 0:3edcf58e51e7 | 66 | bool As5048Spi::parity_check(int sensor_result) |
JSpikker | 0:3edcf58e51e7 | 67 | { |
JSpikker | 0:3edcf58e51e7 | 68 | // Use the LSb of result to keep track of parity (0 = even, 1 = odd) |
JSpikker | 0:3edcf58e51e7 | 69 | int result = sensor_result; |
JSpikker | 0:3edcf58e51e7 | 70 | |
JSpikker | 0:3edcf58e51e7 | 71 | for(int i = 1; i <= 15; ++i) { |
JSpikker | 0:3edcf58e51e7 | 72 | sensor_result >>= 1; |
JSpikker | 0:3edcf58e51e7 | 73 | result ^= sensor_result; |
JSpikker | 0:3edcf58e51e7 | 74 | } |
JSpikker | 0:3edcf58e51e7 | 75 | // Parity should be even |
JSpikker | 0:3edcf58e51e7 | 76 | return (result & 0x0001) == 0; |
JSpikker | 0:3edcf58e51e7 | 77 | } |
JSpikker | 0:3edcf58e51e7 | 78 | |
JSpikker | 0:3edcf58e51e7 | 79 | const int* As5048Spi::read(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 80 | { |
JSpikker | 0:3edcf58e51e7 | 81 | _read(command); // Send command to device(s) |
JSpikker | 0:3edcf58e51e7 | 82 | return _read(AS_CMD_NOP); // Read-out device(s) |
JSpikker | 0:3edcf58e51e7 | 83 | } |
JSpikker | 0:3edcf58e51e7 | 84 | |
JSpikker | 0:3edcf58e51e7 | 85 | const int* As5048Spi::read_sequential(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 86 | { |
JSpikker | 0:3edcf58e51e7 | 87 | return _read(command); |
JSpikker | 0:3edcf58e51e7 | 88 | } |
JSpikker | 0:3edcf58e51e7 | 89 | |
JSpikker | 0:3edcf58e51e7 | 90 | const int* As5048Spi::read_angle() |
JSpikker | 0:3edcf58e51e7 | 91 | { |
JSpikker | 0:3edcf58e51e7 | 92 | _read(AS_CMD_ANGLE); // Send command to device(s) |
JSpikker | 0:3edcf58e51e7 | 93 | return _read(AS_CMD_NOP); // Read-out device(s) |
JSpikker | 0:3edcf58e51e7 | 94 | } |
JSpikker | 0:3edcf58e51e7 | 95 | |
JSpikker | 0:3edcf58e51e7 | 96 | const int* As5048Spi::read_angle_sequential() |
JSpikker | 0:3edcf58e51e7 | 97 | { |
JSpikker | 0:3edcf58e51e7 | 98 | return _read(AS_CMD_ANGLE); |
JSpikker | 0:3edcf58e51e7 | 99 | } |
JSpikker | 0:3edcf58e51e7 | 100 | |
JSpikker | 0:3edcf58e51e7 | 101 | |
JSpikker | 0:3edcf58e51e7 | 102 | int* As5048Spi::_read(As5048Command command) |
JSpikker | 0:3edcf58e51e7 | 103 | { |
JSpikker | 0:3edcf58e51e7 | 104 | if(_nDevices == 1) |
JSpikker | 0:3edcf58e51e7 | 105 | { |
JSpikker | 0:3edcf58e51e7 | 106 | // Give command to start reading the angle |
JSpikker | 0:3edcf58e51e7 | 107 | _chipSelectN.write(0); |
JSpikker | 0:3edcf58e51e7 | 108 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 109 | _readBuffer[0] = _spi.write(command); |
JSpikker | 0:3edcf58e51e7 | 110 | _chipSelectN.write(1); |
JSpikker | 0:3edcf58e51e7 | 111 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 112 | } else |
JSpikker | 0:3edcf58e51e7 | 113 | { |
JSpikker | 0:3edcf58e51e7 | 114 | // Enable the sensor on the chain |
JSpikker | 0:3edcf58e51e7 | 115 | _chipSelectN.write(0); |
JSpikker | 0:3edcf58e51e7 | 116 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 117 | for(int i = 0; i < _nDevices; ++i) |
JSpikker | 0:3edcf58e51e7 | 118 | { |
JSpikker | 0:3edcf58e51e7 | 119 | _readBuffer[i] = _spi.write(command); |
JSpikker | 0:3edcf58e51e7 | 120 | } |
JSpikker | 0:3edcf58e51e7 | 121 | _chipSelectN.write(1); |
JSpikker | 0:3edcf58e51e7 | 122 | wait_us(1); // Wait at least 350ns after chip select |
JSpikker | 0:3edcf58e51e7 | 123 | } |
JSpikker | 0:3edcf58e51e7 | 124 | return _readBuffer; |
JSpikker | 0:3edcf58e51e7 | 125 | } |
JSpikker | 0:3edcf58e51e7 | 126 | |
JSpikker | 0:3edcf58e51e7 | 127 | |
JSpikker | 0:3edcf58e51e7 | 128 | |
JSpikker | 0:3edcf58e51e7 | 129 | |
JSpikker | 0:3edcf58e51e7 | 130 | |
JSpikker | 0:3edcf58e51e7 | 131 |