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