KL46Z-lab2

Dependencies:   SLCD- mbed TSI MMA8451Q USBDevice MAG3110

Committer:
Lokkus
Date:
Thu Feb 21 07:41:00 2019 +0000
Revision:
0:29277ae50860
KL46Z-lab2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lokkus 0:29277ae50860 1 /*
Lokkus 0:29277ae50860 2 * MAG3110 Sensor Library for mbed
Lokkus 0:29277ae50860 3 * TODO: Add proper header
Lokkus 0:29277ae50860 4 */
Lokkus 0:29277ae50860 5
Lokkus 0:29277ae50860 6 #ifndef MAG3110_H
Lokkus 0:29277ae50860 7 #define MAG3110_H
Lokkus 0:29277ae50860 8
Lokkus 0:29277ae50860 9 #include "mbed.h"
Lokkus 0:29277ae50860 10
Lokkus 0:29277ae50860 11 #define PI 3.14159265359
Lokkus 0:29277ae50860 12
Lokkus 0:29277ae50860 13 #define MAG_ADDR 0x1D
Lokkus 0:29277ae50860 14
Lokkus 0:29277ae50860 15 // define registers
Lokkus 0:29277ae50860 16 #define MAG_DR_STATUS 0x00
Lokkus 0:29277ae50860 17 #define MAG_OUT_X_MSB 0x01
Lokkus 0:29277ae50860 18 #define MAG_OUT_X_LSB 0x02
Lokkus 0:29277ae50860 19 #define MAG_OUT_Y_MSB 0x03
Lokkus 0:29277ae50860 20 #define MAG_OUT_Y_LSB 0x04
Lokkus 0:29277ae50860 21 #define MAG_OUT_Z_MSB 0x05
Lokkus 0:29277ae50860 22 #define MAG_OUT_Z_LSB 0x06
Lokkus 0:29277ae50860 23 #define MAG_WHO_AM_I 0x07
Lokkus 0:29277ae50860 24 #define MAG_SYSMOD 0x08
Lokkus 0:29277ae50860 25 #define MAG_OFF_X_MSB 0x09
Lokkus 0:29277ae50860 26 #define MAG_OFF_X_LSB 0x0A
Lokkus 0:29277ae50860 27 #define MAG_OFF_Y_MSB 0x0B
Lokkus 0:29277ae50860 28 #define MAG_OFF_Y_LSB 0x0C
Lokkus 0:29277ae50860 29 #define MAG_OFF_Z_MSB 0x0D
Lokkus 0:29277ae50860 30 #define MAG_OFF_Z_LSB 0x0E
Lokkus 0:29277ae50860 31 #define MAG_DIE_TEMP 0x0F
Lokkus 0:29277ae50860 32 #define MAG_CTRL_REG1 0x10
Lokkus 0:29277ae50860 33 #define MAG_CTRL_REG2 0x11
Lokkus 0:29277ae50860 34
Lokkus 0:29277ae50860 35 // what should WHO_AM_I return?
Lokkus 0:29277ae50860 36 #define MAG_3110_WHO_AM_I_VALUE 0xC4
Lokkus 0:29277ae50860 37
Lokkus 0:29277ae50860 38
Lokkus 0:29277ae50860 39 // Fields in registers
Lokkus 0:29277ae50860 40 // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac
Lokkus 0:29277ae50860 41
Lokkus 0:29277ae50860 42 // Sampling rate from 80Hz down to 0.625Hz
Lokkus 0:29277ae50860 43 #define MAG_3110_SAMPLE80 0
Lokkus 0:29277ae50860 44 #define MAG_3110_SAMPLE40 0x20
Lokkus 0:29277ae50860 45 #define MAG_3110_SAMPLE20 0x40
Lokkus 0:29277ae50860 46 #define MAG_3110_SAMPLE10 0x60
Lokkus 0:29277ae50860 47 #define MAG_3110_SAMPLE5 0x80
Lokkus 0:29277ae50860 48 #define MAG_3110_SAMPLE2_5 0xA0
Lokkus 0:29277ae50860 49 #define MAG_3110_SAMPLE1_25 0xC0
Lokkus 0:29277ae50860 50 #define MAG_3110_SAMPLE0_625 0xE0
Lokkus 0:29277ae50860 51
Lokkus 0:29277ae50860 52 // How many samples to average (lowers data rate)
Lokkus 0:29277ae50860 53 #define MAG_3110_OVERSAMPLE1 0
Lokkus 0:29277ae50860 54 #define MAG_3110_OVERSAMPLE2 0x08
Lokkus 0:29277ae50860 55 #define MAG_3110_OVERSAMPLE3 0x10
Lokkus 0:29277ae50860 56 #define MAG_3110_OVERSAMPLE4 0x18
Lokkus 0:29277ae50860 57
Lokkus 0:29277ae50860 58 // read only 1 byte per axis
Lokkus 0:29277ae50860 59 #define MAG_3110_FASTREAD 0x04
Lokkus 0:29277ae50860 60 // do one measurement (even if in standby mode)
Lokkus 0:29277ae50860 61 #define MAG_3110_TRIGGER 0x02
Lokkus 0:29277ae50860 62 // put in active mode
Lokkus 0:29277ae50860 63 #define MAG_3110_ACTIVE 0x01
Lokkus 0:29277ae50860 64
Lokkus 0:29277ae50860 65 // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _
Lokkus 0:29277ae50860 66 // reset sensor after each reading
Lokkus 0:29277ae50860 67 #define MAG_3110_AUTO_MRST_EN 0x80
Lokkus 0:29277ae50860 68 // don't subtract user offsets
Lokkus 0:29277ae50860 69 #define MAG_3110_RAW 0x20
Lokkus 0:29277ae50860 70 // reset magnetic sensor after too-large field
Lokkus 0:29277ae50860 71 #define MAG_3110_MAG_RST 0x10
Lokkus 0:29277ae50860 72
Lokkus 0:29277ae50860 73 // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR
Lokkus 0:29277ae50860 74 #define MAG_3110_ZYXDR 0x08
Lokkus 0:29277ae50860 75
Lokkus 0:29277ae50860 76 /**
Lokkus 0:29277ae50860 77 * MAG3110 Class to read X/Y/Z data from the magentometer
Lokkus 0:29277ae50860 78 *
Lokkus 0:29277ae50860 79 */
Lokkus 0:29277ae50860 80 class MAG3110
Lokkus 0:29277ae50860 81 {
Lokkus 0:29277ae50860 82 public:
Lokkus 0:29277ae50860 83 /**
Lokkus 0:29277ae50860 84 * Main constructor
Lokkus 0:29277ae50860 85 * @param sda SDA pin
Lokkus 0:29277ae50860 86 * @param sdl SCL pin
Lokkus 0:29277ae50860 87 * @param addr addr of the I2C peripheral
Lokkus 0:29277ae50860 88 */
Lokkus 0:29277ae50860 89 MAG3110(PinName sda, PinName scl);
Lokkus 0:29277ae50860 90 /**
Lokkus 0:29277ae50860 91 * Debug version of constructor
Lokkus 0:29277ae50860 92 * @param sda SDA pin
Lokkus 0:29277ae50860 93 * @param sdl SCL pin
Lokkus 0:29277ae50860 94 * @param addr Address of the I2C peripheral
Lokkus 0:29277ae50860 95 * @param pc Serial object to output debug messages
Lokkus 0:29277ae50860 96 */
Lokkus 0:29277ae50860 97 MAG3110(PinName sda, PinName scl, Serial *pc); //pass serial for debug
Lokkus 0:29277ae50860 98 /**
Lokkus 0:29277ae50860 99 * Setup the Magnetometer
Lokkus 0:29277ae50860 100 *
Lokkus 0:29277ae50860 101 */
Lokkus 0:29277ae50860 102 void begin();
Lokkus 0:29277ae50860 103 /**
Lokkus 0:29277ae50860 104 * Read a register, return its value as int
Lokkus 0:29277ae50860 105 * @param regAddr The address to read
Lokkus 0:29277ae50860 106 * @return value in register
Lokkus 0:29277ae50860 107 */
Lokkus 0:29277ae50860 108 int readReg(char regAddr);
Lokkus 0:29277ae50860 109 /**
Lokkus 0:29277ae50860 110 * Read a value from a pair of registers, return as int
Lokkus 0:29277ae50860 111 * @param regAddr The address to read
Lokkus 0:29277ae50860 112 * @return Value from 2 consecutive registers
Lokkus 0:29277ae50860 113 */
Lokkus 0:29277ae50860 114 int readVal(char regAddr);
Lokkus 0:29277ae50860 115 /**
Lokkus 0:29277ae50860 116 * Calculate the heading
Lokkus 0:29277ae50860 117 * @return heading in degrees
Lokkus 0:29277ae50860 118 */
Lokkus 0:29277ae50860 119 float getHeading();
Lokkus 0:29277ae50860 120 /**
Lokkus 0:29277ae50860 121 * Perform a read on the X, Y and Z values.
Lokkus 0:29277ae50860 122 * @param xVal Pointer to X value
Lokkus 0:29277ae50860 123 * @param yVal Pointer to Y value
Lokkus 0:29277ae50860 124 * @param zVal Pointer to Z value
Lokkus 0:29277ae50860 125 */
Lokkus 0:29277ae50860 126 void getValues(int *xVal, int *yVal, int *zVal);
Lokkus 0:29277ae50860 127 /**
Lokkus 0:29277ae50860 128 * Set the calibration parameters if required.
Lokkus 0:29277ae50860 129 * @param minX Minimum value for X range
Lokkus 0:29277ae50860 130 * @param maxX Maximum value for X range
Lokkus 0:29277ae50860 131 * @param minY Minimum value for Y range
Lokkus 0:29277ae50860 132 * @param maxY maximum value for Y range
Lokkus 0:29277ae50860 133 */
Lokkus 0:29277ae50860 134 void setCalibration(int minX, int maxX, int minY, int maxY);
Lokkus 0:29277ae50860 135
Lokkus 0:29277ae50860 136 private:
Lokkus 0:29277ae50860 137 I2C _i2c;
Lokkus 0:29277ae50860 138 int _i2c_address;
Lokkus 0:29277ae50860 139 Serial *_pc;
Lokkus 0:29277ae50860 140 bool _debug;
Lokkus 0:29277ae50860 141 int _avgX, _avgY;
Lokkus 0:29277ae50860 142
Lokkus 0:29277ae50860 143 };
Lokkus 0:29277ae50860 144 #endif