LSM303DLHC Full Driver: Readings For Acc, Mag and Temp; Class Method for frequently-used 13 sensor parameters; Class Method to direct modify registers; Support Calibration (offset+scale);

Dependencies:   mbed

Committer:
Airium
Date:
Tue Jun 07 15:18:48 2016 +0000
Revision:
3:522d01930e6a
Parent:
2:192a6c228644
Child:
4:8723c07d4c45
Add Compact User Instruction in header; Some names changed for convenience;  Reg addrs defined in a separate header; if no bug this shall be the final version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Airium 3:522d01930e6a 1 /* COMPACT USER INSTRUCTION OF LSM303DLHC
Airium 3:522d01930e6a 2
Airium 3:522d01930e6a 3 -- ACCELEROMETER PART
Airium 3:522d01930e6a 4 void GetAcc( float arr[3] ) // Get Acceleration Value
Airium 3:522d01930e6a 5 arr[3] is the xyz-axis acc data
Airium 3:522d01930e6a 6 // default range from -2048 to +2047 with step=1 (12bit high prec) or step=4 (10bit normal prec)
Airium 3:522d01930e6a 7 // and roughly 1000/g(+/-2g) 500/g(+/-4g) 200/g(+/-8g) 80/g(+/-16g) depend on your device and ACC_FS
Airium 3:522d01930e6a 8 // note your calibration setting will change the range
Airium 3:522d01930e6a 9 void ACtrl( enum cmd ) e.g. ACtrl(LSM303DLHC::LP_ON)
Airium 3:522d01930e6a 10 enum ACC_LPen {LP_OFF,LP_ON} // to ctrl acc power mode
Airium 3:522d01930e6a 11 enum ACC_ODR {PW_D,ADR1,ADR2,ADR3,ADR4,ADR5,ADR6,ADR7,ADR8,ADR9} // to ctrl acc data rate
Airium 3:522d01930e6a 12 enum ACC_AXIS {NONE,X,Y,XY,Z,XZ,YZ,XYZ} // to sel the acc data of axis you want
Airium 3:522d01930e6a 13 enum ACC_HPM {HPF_NORM_R,HPF_REF,HPF_NORM,HPF_AUTO} // to sel acc internal HPF model
Airium 3:522d01930e6a 14 enum ACC_HPCF {HPF_CF0,HPF_CF1,HPF_CF2,HPF_CF3} // to switch acc internal HPF cutoff freq
Airium 3:522d01930e6a 15 enum ACC_FDS {HPF_OFF,HPF_ON} // to switch on/off acc internal HPF for output
Airium 3:522d01930e6a 16 enum ACC_BDU {BDU_CONT,BDU_HOLD} // to sel acc data update mode
Airium 3:522d01930e6a 17 enum ACC_FS {G2,G4,G8,G16} // to sel acc full scale of data
Airium 3:522d01930e6a 18 enum ACC_HR {LOW_R,HIGH_R} // to sel acc output data precision
Airium 3:522d01930e6a 19 void ACal( float offset[3], float scale[3] ); // Acc Calibration Setting
Airium 3:522d01930e6a 20 // affect data of GetAcc() by output = scale * ( offset + original )
Airium 3:522d01930e6a 21 // but if internal HPF enable then output = scale * original
Airium 3:522d01930e6a 22 // note linear acc has no offset
Airium 3:522d01930e6a 23 bool isHPFEn(); // report HPF ON/OFF state for output
Airium 3:522d01930e6a 24 return bool = true if ACC_FDS = HPF_ON and vice versa
Airium 3:522d01930e6a 25
Airium 3:522d01930e6a 26 -- MAGNETOMETER PART
Airium 3:522d01930e6a 27 void GetMag( float arr[3] ); // Get magnetic flux density
Airium 3:522d01930e6a 28 arr[3] is the xyz-axis mag data
Airium 3:522d01930e6a 29 // default range from -2048 to +2047 with step=1
Airium 3:522d01930e6a 30 // note your calibration setting will change the range
Airium 3:522d01930e6a 31 void MCtrl( enum cmd ) e.g. MCtrl(LSM303DLHC::MDR3);
Airium 3:522d01930e6a 32 enum MAG_DR {MDR0,MDR1,MDR2,MDR3,MDR4,MDR5,MDR6,MDR7} // to ctrl mag data rate
Airium 3:522d01930e6a 33 enum MAG_GN {GN1,GN2,GN3,GN4,GN5,GN6,GN7) // to ctrl mag full scale of data
Airium 3:522d01930e6a 34 enum MAG_MD {MD_CONT,MD_SING,MD_SLP,MD_SLP2};
Airium 3:522d01930e6a 35 void MCal(float offset[3], float scale[3]); // Mag Calibration
Airium 3:522d01930e6a 36 // affect data of GetMag() by output = scale * ( offset + original )
Airium 3:522d01930e6a 37
Airium 3:522d01930e6a 38 -- TEMPERATURE METER PART
Airium 3:522d01930e6a 39 void GetTemp( float arr[1] ); // Get temperature
Airium 3:522d01930e6a 40 arr[1] is the temperture
Airium 3:522d01930e6a 41 // default range from -2048 to +2047
Airium 3:522d01930e6a 42 void TCal( enum cmd )
Airium 3:522d01930e6a 43 enum TEMP_EN{TEMP_OFF,TEMP_ON}; // to switch on/off TEMP
Airium 3:522d01930e6a 44 void TempCal(float offset[1], float scale[1]); // Mag Calibration
Airium 3:522d01930e6a 45 //affect data of GetMag() by output = scale * ( offset + original )
Airium 3:522d01930e6a 46
Airium 3:522d01930e6a 47 -- DIRECT WRITE REG
Airium 3:522d01930e6a 48 void WriteReg(int sad, char d[2]); // Write value to reg
Airium 3:522d01930e6a 49 sad = ACC_ADDRESS or MAG_ADDRESS
Airium 3:522d01930e6a 50 d[0] = Register Address
Airium 3:522d01930e6a 51 d[1] = Register Value
Airium 3:522d01930e6a 52 CAUTION: THE FUNTION USES UNFRIENDLY ASSIGN
Airium 3:522d01930e6a 53 CAUTION: WRITING TO RESERVED REG MAY CAUSE PERMANENT DAMAGE
Airium 3:522d01930e6a 54 PLEASE FOLLOW DATASHEET RIGIDLY
Airium 3:522d01930e6a 55 */
Airium 3:522d01930e6a 56
Airium 3:522d01930e6a 57
Airium 0:7864abfabe2f 58 #ifndef LSM303DLHC_H
Airium 0:7864abfabe2f 59 #define LSM303DLHC_H
Airium 0:7864abfabe2f 60 #include "mbed.h"
Airium 3:522d01930e6a 61 #include "LSM303DLHC_REG.h"
Airium 0:7864abfabe2f 62
Airium 0:7864abfabe2f 63 class LSM303DLHC {
Airium 0:7864abfabe2f 64 public:
Airium 0:7864abfabe2f 65 //// PORT
Airium 0:7864abfabe2f 66 LSM303DLHC(PinName sda, PinName scl);
Airium 0:7864abfabe2f 67
Airium 0:7864abfabe2f 68 //// REG CTRL
Airium 0:7864abfabe2f 69 // modify the default in LSM303DLHC.cpp
Airium 0:7864abfabe2f 70
Airium 0:7864abfabe2f 71 //// CTRL_REG1_A (20h)
Airium 0:7864abfabe2f 72 // ACC Low Power Mode ON/OFF
Airium 0:7864abfabe2f 73 enum ACC_LPen { LP_OFF = 0, LP_ON = 1} ;
Airium 0:7864abfabe2f 74 void ACtrl(ACC_LPen cmd);
Airium 0:7864abfabe2f 75 // ACC Data Rate NOTE: ACC SWITCH IS HERE
Airium 3:522d01930e6a 76 enum ACC_ODR { PW_D = 0, ADR1 = 1, ADR2 = 2, ADR3 = 3, ADR4 = 4, ADR5 = 5, ADR6 = 6, ADR7 = 7, ADR8 = 8, ADR9 = 9};
Airium 3:522d01930e6a 77 // PW Down 1Hz 10Hz 25Hz 50Hz 100Hz 200Hz 400Hz 1620Hz@LP 1344Hz@NM/5376HZ@LP
Airium 0:7864abfabe2f 78 void ACtrl(ACC_ODR cmd);
Airium 1:e68ce5025dad 79 // ACC AXIS SEL
Airium 2:192a6c228644 80 enum ACC_AXIS { NONE = 0, X = 1, Y = 2, XY = 3, Z = 4, XZ = 5, YZ = 6, XYZ = 7 };
Airium 1:e68ce5025dad 81 void ACtrl(ACC_AXIS cmd);
Airium 0:7864abfabe2f 82
Airium 0:7864abfabe2f 83 //// CTRL_REG2_A (21h)
Airium 0:7864abfabe2f 84 // ACC HPF Mode
Airium 0:7864abfabe2f 85 enum ACC_HPM { HPF_NORM_R = 0, HPF_REF = 1, HPF_NORM = 2, HPF_AUTO = 3 };
Airium 0:7864abfabe2f 86 void ACtrl(ACC_HPM cmd);
Airium 0:7864abfabe2f 87 // ACC HPF Cutoff Freq
Airium 0:7864abfabe2f 88 enum ACC_HPCF { HPF_CF0 = 0, HPF_CF1 = 1, HPF_CF2 = 2, HPF_CF3 = 3};
Airium 0:7864abfabe2f 89 void ACtrl(ACC_HPCF cmd);
Airium 0:7864abfabe2f 90 // ACC HPF ON/OFF for output
Airium 0:7864abfabe2f 91 enum ACC_FDS { HPF_OFF = 0, HPF_ON = 1 };
Airium 0:7864abfabe2f 92 void ACtrl(ACC_FDS cmd);
Airium 0:7864abfabe2f 93
Airium 0:7864abfabe2f 94 //// CTRL_REG4_A (23h)
Airium 0:7864abfabe2f 95 // ACC Block Date Update Mode
Airium 0:7864abfabe2f 96 enum ACC_BDU { BDU_CONT = 0, BDU_HOLD = 1 };
Airium 0:7864abfabe2f 97 void ACtrl(ACC_BDU cmd);
Airium 0:7864abfabe2f 98 // ACC Output Full-Scale SEL
Airium 0:7864abfabe2f 99 enum ACC_FS {G2 = 0b00, G4 = 0b01, G8 = 0b10, G16= 0b11 };
Airium 0:7864abfabe2f 100 // +/-2g +/-4g +/-8g +/-16g
Airium 0:7864abfabe2f 101 void ACtrl(ACC_FS cmd);
Airium 0:7864abfabe2f 102 // ACC Output in High/Low Resolution (Precision SEL)
Airium 0:7864abfabe2f 103 enum ACC_HR { LOW_R = 0, HIGH_R = 1 };
Airium 0:7864abfabe2f 104 // 10bit 12bit
Airium 0:7864abfabe2f 105 void ACtrl(ACC_HR cmd);
Airium 0:7864abfabe2f 106
Airium 0:7864abfabe2f 107 //// CRA_REG_M (00h)
Airium 0:7864abfabe2f 108 // TEMP Sensor En NOTE: TEMP SWITCH IS HERE
Airium 0:7864abfabe2f 109 enum TEMP_EN { TEMP_OFF = 0, TEMP_ON = 1};
Airium 0:7864abfabe2f 110 void TCtrl(TEMP_EN cmd);
Airium 0:7864abfabe2f 111 // MAG Data Rate (Note: MAG_DR determines DRDY pin)
Airium 3:522d01930e6a 112 enum MAG_DR { MDR0 = 0, MDR1 = 1, MDR2 = 2, MDR3 = 3, MDR4 = 4, MDR5 = 5, MDR6 = 6, MDR7 = 7};
Airium 3:522d01930e6a 113 // 0.75Hz 1.5Hz 3Hz 7.5Hz 15Hz 30Hz 75Hz 220Hz
Airium 0:7864abfabe2f 114 void MCtrl(MAG_DR cmd); // MAG Date Rate
Airium 0:7864abfabe2f 115
Airium 0:7864abfabe2f 116 //// CRB_REG_M (01h)
Airium 0:7864abfabe2f 117 // MAG Gain (Range SEL)
Airium 3:522d01930e6a 118 enum MAG_GN { GN1 = 1, GN2 = 2, GN3 = 3, GN4 = 4, GN5 = 5, GN6 = 6, GN7 = 7};
Airium 3:522d01930e6a 119 // 1.3 1.9 2.5 4.0 4.7 5.6 8.1 (+/- Gauss)
Airium 0:7864abfabe2f 120 void MCtrl(MAG_GN cmd); // MAG Measure Range
Airium 0:7864abfabe2f 121
Airium 0:7864abfabe2f 122 //// MR_REG_M (02h)
Airium 0:7864abfabe2f 123 // MAG Mode NOTE: MAG SWITCH IS HERE
Airium 0:7864abfabe2f 124 enum MAG_MD { MD_CONT = 0, MD_SING = 1, MD_SLP = 2, MD_SLP2 = 3};
Airium 0:7864abfabe2f 125 // Continuous single-conv sleep-mode sleep-mode
Airium 0:7864abfabe2f 126 void MCtrl(MAG_MD cmd);
Airium 0:7864abfabe2f 127
Airium 0:7864abfabe2f 128 //// Change more regs
Airium 0:7864abfabe2f 129 void WriteReg(int sad, char d[2]); // Write value to reg
Airium 3:522d01930e6a 130 // sad = ACC_ADDRESS or MAG_ADDRESS
Airium 3:522d01930e6a 131 // d[0] = Register Address and d[1] = Register Value
Airium 3:522d01930e6a 132 // CAUTION: THE FUNTION USES UNFRIENDLY ASSIGN AND WRITING TO RESERVED REG MAY CAUSE PERMANENT DAMAGE
Airium 0:7864abfabe2f 133 // PLEASE FOLLOW DATASHEET RIGIDLY
Airium 0:7864abfabe2f 134
Airium 0:7864abfabe2f 135 //// Get Data
Airium 3:522d01930e6a 136 // please refer to the top: COMPACT USER INSTRUCTION
Airium 3:522d01930e6a 137 void GetAcc(float arr[3]); // Get acc
Airium 3:522d01930e6a 138 void GetMag(float arr[3]); // Get mag
Airium 3:522d01930e6a 139 void GetTemp(float arr[1]); // Get temp
Airium 0:7864abfabe2f 140
Airium 3:522d01930e6a 141 //// sensor reading calibration
Airium 3:522d01930e6a 142 // please refer to the top: COMPACT USER INSTRUCTION
Airium 3:522d01930e6a 143 void ACal(float offset[3], float scale[3]); // Acc Calibration
Airium 3:522d01930e6a 144 void MCal(float offset[3], float scale[3]); // Mag Calibration
Airium 3:522d01930e6a 145 void TCal(float offset[1], float scale[1]); // Mag Calibration
Airium 0:7864abfabe2f 146
Airium 0:7864abfabe2f 147 //// Other functions
Airium 0:7864abfabe2f 148 bool isHPFEn(); // report HPF ON/OFF state for output
Airium 0:7864abfabe2f 149 // return HPF_state
Airium 0:7864abfabe2f 150
Airium 0:7864abfabe2f 151 //// Not implemented function
Airium 0:7864abfabe2f 152 bool isAccRdy(); // Check if acc has new data
Airium 0:7864abfabe2f 153 // one way is to use high freq ticker to check STATUS_REG_A (27h)
Airium 0:7864abfabe2f 154 // I2C@400kHz just enough to check Max Normal Mode Data Rate (1344Hz)
Airium 3:522d01930e6a 155 // or someone to teach me how to use CTRL_REG6_A THX THX THX (:
Airium 0:7864abfabe2f 156 bool isMagRdy(); // check if mag has new data
Airium 0:7864abfabe2f 157 // one way is to use InterruptIn to trigger from DRDY pin
Airium 0:7864abfabe2f 158 // DRDY pin freq defined by MAG_DR
Airium 0:7864abfabe2f 159
Airium 0:7864abfabe2f 160 private:
Airium 0:7864abfabe2f 161 I2C i2c;
Airium 3:522d01930e6a 162
Airium 0:7864abfabe2f 163 char data[6]; // used as main data exchange
Airium 0:7864abfabe2f 164 bool HPF_state; // state of internal HPF
Airium 0:7864abfabe2f 165
Airium 0:7864abfabe2f 166 // use offset to calibrate zero reading
Airium 0:7864abfabe2f 167 float acc_offset[3];
Airium 0:7864abfabe2f 168 float mag_offset[3];
Airium 0:7864abfabe2f 169 float temp_offset[1];
Airium 0:7864abfabe2f 170
Airium 0:7864abfabe2f 171 //use scale to adjust the range to required
Airium 0:7864abfabe2f 172 float acc_scale[3];
Airium 0:7864abfabe2f 173 float mag_scale[3];
Airium 0:7864abfabe2f 174 float temp_scale[1];
Airium 0:7864abfabe2f 175 };
Airium 0:7864abfabe2f 176
Airium 0:7864abfabe2f 177 #endif