Basic library for interfacing the AK8975 using I2C. It does not include more advanced functions. The datasheet does not include what the self-test should return for example, so this library does not include the self-test function.
AK8975.h@1:76f65416ae1b, 2012-05-07 (annotated)
- Committer:
- Sissors
- Date:
- Mon May 07 12:01:10 2012 +0000
- Revision:
- 1:76f65416ae1b
- Parent:
- 0:bceb91239894
v1.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:bceb91239894 | 1 | /* Library for AK8975 digital compass IC, for use with I2C |
Sissors | 0:bceb91239894 | 2 | |
Sissors | 0:bceb91239894 | 3 | This library contains basic functionality, if you want more, make it ;) |
Sissors | 0:bceb91239894 | 4 | |
Sissors | 0:bceb91239894 | 5 | */ |
Sissors | 0:bceb91239894 | 6 | |
Sissors | 0:bceb91239894 | 7 | #ifndef AK8975_H |
Sissors | 0:bceb91239894 | 8 | #define AK8975_H |
Sissors | 0:bceb91239894 | 9 | |
Sissors | 0:bceb91239894 | 10 | /** |
Sissors | 0:bceb91239894 | 11 | * Includes |
Sissors | 0:bceb91239894 | 12 | */ |
Sissors | 0:bceb91239894 | 13 | #include "mbed.h" |
Sissors | 0:bceb91239894 | 14 | |
Sissors | 0:bceb91239894 | 15 | /** |
Sissors | 0:bceb91239894 | 16 | * Registers |
Sissors | 0:bceb91239894 | 17 | */ |
Sissors | 0:bceb91239894 | 18 | #define AK8975_ID_REG 0x00 |
Sissors | 0:bceb91239894 | 19 | #define AK8975_ST1_REG 0x02 |
Sissors | 0:bceb91239894 | 20 | #define AK8975_X_REG 0x03 |
Sissors | 0:bceb91239894 | 21 | #define AK8975_Y_REG 0x05 |
Sissors | 0:bceb91239894 | 22 | #define AK8975_Z_REG 0x07 |
Sissors | 0:bceb91239894 | 23 | #define AK8975_ST2_REG 0x09 |
Sissors | 0:bceb91239894 | 24 | #define AK8975_CONTROL_REG 0x0A |
Sissors | 0:bceb91239894 | 25 | #define AK8975_SELFTEST_REG 0x0C |
Sissors | 0:bceb91239894 | 26 | |
Sissors | 0:bceb91239894 | 27 | /** |
Sissors | 0:bceb91239894 | 28 | * Bits |
Sissors | 0:bceb91239894 | 29 | */ |
Sissors | 0:bceb91239894 | 30 | #define AK8975_DRDY_BIT 0 |
Sissors | 0:bceb91239894 | 31 | #define AK8975_DERROR_BIT 2 |
Sissors | 0:bceb91239894 | 32 | #define AK8975_OFLOW_BIT 3 |
Sissors | 0:bceb91239894 | 33 | |
Sissors | 0:bceb91239894 | 34 | #define AK8975_SINGLE_MEASUREMENT 1 |
Sissors | 0:bceb91239894 | 35 | |
Sissors | 0:bceb91239894 | 36 | #define AK8975_SENSITIVITY 0.3 |
Sissors | 0:bceb91239894 | 37 | |
Sissors | 1:76f65416ae1b | 38 | /** AK8975 magnetometer/digital compass simple library. |
Sissors | 1:76f65416ae1b | 39 | * |
Sissors | 1:76f65416ae1b | 40 | * Example: |
Sissors | 1:76f65416ae1b | 41 | * @code |
Sissors | 1:76f65416ae1b | 42 | * #include "mbed.h" |
Sissors | 1:76f65416ae1b | 43 | * #include "AK8975.h" |
Sissors | 1:76f65416ae1b | 44 | * |
Sissors | 1:76f65416ae1b | 45 | * DigitalOut led1(LED1); |
Sissors | 1:76f65416ae1b | 46 | * Serial pc(USBTX, USBRX); // tx, rx |
Sissors | 1:76f65416ae1b | 47 | * AK8975 mag(p9,p10,0x0E); |
Sissors | 1:76f65416ae1b | 48 | * |
Sissors | 1:76f65416ae1b | 49 | * |
Sissors | 1:76f65416ae1b | 50 | * int main() { |
Sissors | 1:76f65416ae1b | 51 | * int data[3]; |
Sissors | 1:76f65416ae1b | 52 | * if (mag.testConnection()) |
Sissors | 1:76f65416ae1b | 53 | * pc.printf("Connection succeeded. \n"); |
Sissors | 1:76f65416ae1b | 54 | * else |
Sissors | 1:76f65416ae1b | 55 | * pc.printf("Connection failed. \n"); |
Sissors | 1:76f65416ae1b | 56 | * |
Sissors | 1:76f65416ae1b | 57 | * while(1) { |
Sissors | 1:76f65416ae1b | 58 | * led1=!led1; |
Sissors | 1:76f65416ae1b | 59 | * mag.startMeasurement(); |
Sissors | 1:76f65416ae1b | 60 | * wait(0.5); //(or use the isReady() function) |
Sissors | 1:76f65416ae1b | 61 | * mag.getAll(data); |
Sissors | 1:76f65416ae1b | 62 | * pc.printf("X: %d", data[0]); |
Sissors | 1:76f65416ae1b | 63 | * pc.putc('\n'); |
Sissors | 1:76f65416ae1b | 64 | * pc.printf("Y: %d", data[1]); |
Sissors | 1:76f65416ae1b | 65 | * pc.putc('\n'); |
Sissors | 1:76f65416ae1b | 66 | * pc.printf("Z: %d", data[2]); |
Sissors | 1:76f65416ae1b | 67 | * pc.putc('\n'); |
Sissors | 1:76f65416ae1b | 68 | * } |
Sissors | 1:76f65416ae1b | 69 | * } |
Sissors | 1:76f65416ae1b | 70 | * @endcode |
Sissors | 1:76f65416ae1b | 71 | */ |
Sissors | 0:bceb91239894 | 72 | class AK8975 { |
Sissors | 0:bceb91239894 | 73 | public: |
Sissors | 0:bceb91239894 | 74 | /** |
Sissors | 0:bceb91239894 | 75 | * Constructor. |
Sissors | 0:bceb91239894 | 76 | * |
Sissors | 0:bceb91239894 | 77 | * @param sda - mbed pin to use for the SDA I2C line. |
Sissors | 0:bceb91239894 | 78 | * @param scl - mbed pin to use for the SCL I2C line. |
Sissors | 0:bceb91239894 | 79 | * @param I2Caddress - the I2C address of the device (0x0C - 0x0F) |
Sissors | 0:bceb91239894 | 80 | */ |
Sissors | 0:bceb91239894 | 81 | AK8975(PinName sda, PinName scl, char address); |
Sissors | 0:bceb91239894 | 82 | |
Sissors | 0:bceb91239894 | 83 | /** |
Sissors | 0:bceb91239894 | 84 | * Checks connection with the device. |
Sissors | 0:bceb91239894 | 85 | * |
Sissors | 0:bceb91239894 | 86 | * @return - true if working connection, otherwise false |
Sissors | 0:bceb91239894 | 87 | */ |
Sissors | 0:bceb91239894 | 88 | bool testConnection( void ); |
Sissors | 0:bceb91239894 | 89 | |
Sissors | 0:bceb91239894 | 90 | /** |
Sissors | 0:bceb91239894 | 91 | * Checks if measurement is ready |
Sissors | 0:bceb91239894 | 92 | * |
Sissors | 0:bceb91239894 | 93 | * @return - true if available measurement, otherwise false |
Sissors | 0:bceb91239894 | 94 | */ |
Sissors | 0:bceb91239894 | 95 | bool isReady( void ); |
Sissors | 0:bceb91239894 | 96 | |
Sissors | 0:bceb91239894 | 97 | /** |
Sissors | 0:bceb91239894 | 98 | * Gets the X data |
Sissors | 0:bceb91239894 | 99 | * |
Sissors | 0:bceb91239894 | 100 | * @return - signed integer containing the raw data |
Sissors | 0:bceb91239894 | 101 | */ |
Sissors | 0:bceb91239894 | 102 | int getX( void ); |
Sissors | 0:bceb91239894 | 103 | |
Sissors | 0:bceb91239894 | 104 | /** |
Sissors | 0:bceb91239894 | 105 | * Gets the Y data |
Sissors | 0:bceb91239894 | 106 | * |
Sissors | 0:bceb91239894 | 107 | * @return - signed integer containing the raw data |
Sissors | 0:bceb91239894 | 108 | */ |
Sissors | 0:bceb91239894 | 109 | int getY( void ); |
Sissors | 0:bceb91239894 | 110 | |
Sissors | 0:bceb91239894 | 111 | /** |
Sissors | 0:bceb91239894 | 112 | * Gets the Z data |
Sissors | 0:bceb91239894 | 113 | * |
Sissors | 0:bceb91239894 | 114 | * @return - signed integer containing the raw data |
Sissors | 0:bceb91239894 | 115 | */ |
Sissors | 0:bceb91239894 | 116 | int getZ( void ); |
Sissors | 0:bceb91239894 | 117 | |
Sissors | 0:bceb91239894 | 118 | /** |
Sissors | 0:bceb91239894 | 119 | * Gets all the data, this is more efficient than calling the functions individually |
Sissors | 0:bceb91239894 | 120 | * |
Sissors | 0:bceb91239894 | 121 | * @param data - pointer to integer array with length 3 where data is stored (data[0]=X - data[1]=Y - data[2]=Z) |
Sissors | 0:bceb91239894 | 122 | */ |
Sissors | 0:bceb91239894 | 123 | void getAll( int *data ); |
Sissors | 0:bceb91239894 | 124 | |
Sissors | 0:bceb91239894 | 125 | |
Sissors | 0:bceb91239894 | 126 | |
Sissors | 0:bceb91239894 | 127 | /** |
Sissors | 0:bceb91239894 | 128 | * Checks if there is a data error due to reading at wrong moment |
Sissors | 0:bceb91239894 | 129 | * |
Sissors | 0:bceb91239894 | 130 | * @return - true for error, false for no error |
Sissors | 0:bceb91239894 | 131 | */ |
Sissors | 0:bceb91239894 | 132 | bool getDataError( void ); |
Sissors | 0:bceb91239894 | 133 | |
Sissors | 0:bceb91239894 | 134 | /** |
Sissors | 0:bceb91239894 | 135 | * Checks if a magnetic overflow happened (sensor saturation) |
Sissors | 0:bceb91239894 | 136 | * |
Sissors | 0:bceb91239894 | 137 | * @return - true for error, false for no error |
Sissors | 0:bceb91239894 | 138 | */ |
Sissors | 0:bceb91239894 | 139 | bool getOverflow( void ); |
Sissors | 0:bceb91239894 | 140 | |
Sissors | 0:bceb91239894 | 141 | /** |
Sissors | 0:bceb91239894 | 142 | * Starts a measurement cycle |
Sissors | 0:bceb91239894 | 143 | */ |
Sissors | 0:bceb91239894 | 144 | void startMeasurement( void ); |
Sissors | 0:bceb91239894 | 145 | |
Sissors | 0:bceb91239894 | 146 | |
Sissors | 0:bceb91239894 | 147 | |
Sissors | 0:bceb91239894 | 148 | private: |
Sissors | 0:bceb91239894 | 149 | I2C connection; |
Sissors | 0:bceb91239894 | 150 | char deviceAddress; |
Sissors | 0:bceb91239894 | 151 | |
Sissors | 0:bceb91239894 | 152 | /** |
Sissors | 0:bceb91239894 | 153 | * Writes data to the device |
Sissors | 0:bceb91239894 | 154 | * |
Sissors | 0:bceb91239894 | 155 | * @param adress - register address to write to |
Sissors | 0:bceb91239894 | 156 | * @param data - data to write |
Sissors | 0:bceb91239894 | 157 | */ |
Sissors | 0:bceb91239894 | 158 | void write( char address, char data); |
Sissors | 0:bceb91239894 | 159 | |
Sissors | 0:bceb91239894 | 160 | /** |
Sissors | 0:bceb91239894 | 161 | * Read data from the device |
Sissors | 0:bceb91239894 | 162 | * |
Sissors | 0:bceb91239894 | 163 | * @param adress - register address to write to |
Sissors | 0:bceb91239894 | 164 | * @return - data from the register specified by RA |
Sissors | 0:bceb91239894 | 165 | */ |
Sissors | 0:bceb91239894 | 166 | char read( char adress); |
Sissors | 0:bceb91239894 | 167 | |
Sissors | 0:bceb91239894 | 168 | /** |
Sissors | 0:bceb91239894 | 169 | * Read multtiple regigsters from the device, more efficient than using multiple normal reads. |
Sissors | 0:bceb91239894 | 170 | * |
Sissors | 0:bceb91239894 | 171 | * @param adress - register address to write to |
Sissors | 0:bceb91239894 | 172 | * @param length - number of bytes to read |
Sissors | 0:bceb91239894 | 173 | * @param data - pointer where the data needs to be written to |
Sissors | 0:bceb91239894 | 174 | */ |
Sissors | 0:bceb91239894 | 175 | void read( char adress, char *data, int length); |
Sissors | 0:bceb91239894 | 176 | }; |
Sissors | 0:bceb91239894 | 177 | #endif |