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:
Thu Jun 09 15:19:00 2016 +0000
Revision:
4:8723c07d4c45
Parent:
3:522d01930e6a
Add offset calibration for specific use in HPF-on situation and therefore remove (bool) HPF_state and isHPFEn() and associated code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Airium 0:7864abfabe2f 1 #include "mbed.h"
Airium 0:7864abfabe2f 2 #include "LSM303DLHC.h"
Airium 0:7864abfabe2f 3
Airium 0:7864abfabe2f 4 LSM303DLHC::LSM303DLHC(PinName sda, PinName scl) : i2c(sda, scl){
Airium 0:7864abfabe2f 5 i2c.frequency(400000);
Airium 0:7864abfabe2f 6
Airium 3:522d01930e6a 7 // HERE GIVES DEVICE DEFAULT
Airium 3:522d01930e6a 8 ACtrl(LP_OFF); // ACC Normal Power Mode
Airium 3:522d01930e6a 9 ACtrl(ADR3); // ACC ON and Date Rate 25Hz
Airium 3:522d01930e6a 10 ACtrl(XYZ); // ACC XYZ Axis Enable
Airium 3:522d01930e6a 11 ACtrl(HPF_ON); // ACC internal HPF IN USE
Airium 3:522d01930e6a 12 ACtrl(HPF_CF0); // ACC HPF Cutoff Freq = option 0
Airium 3:522d01930e6a 13 ACtrl(HPF_NORM_R); // ACC HPF Model = Normal
Airium 3:522d01930e6a 14 ACtrl(BDU_CONT); // ACC data continuous
Airium 3:522d01930e6a 15 ACtrl(G4); // ACC Range +/-4g
Airium 3:522d01930e6a 16 ACtrl(HIGH_R); // ACC in High Prec
Airium 3:522d01930e6a 17 MCtrl(MDR4); // MAG DR 15Hz
Airium 3:522d01930e6a 18 MCtrl(GN4); // MAG GN 4.0Gauss
Airium 3:522d01930e6a 19 MCtrl(MD_CONT); // MAG ON and MD Continuous
Airium 3:522d01930e6a 20 TCtrl(TEMP_ON); // TEMP ON
Airium 3:522d01930e6a 21
Airium 3:522d01930e6a 22 // DEFAULT CALIBRATION PARAMETER
Airium 0:7864abfabe2f 23 acc_offset[0] = 0;
Airium 0:7864abfabe2f 24 acc_offset[1] = 0;
Airium 0:7864abfabe2f 25 acc_offset[2] = 0;
Airium 0:7864abfabe2f 26 acc_scale[0] = 1;
Airium 0:7864abfabe2f 27 acc_scale[1] = 1;
Airium 0:7864abfabe2f 28 acc_scale[2] = 1;
Airium 0:7864abfabe2f 29
Airium 0:7864abfabe2f 30 mag_offset[0] = 0;
Airium 0:7864abfabe2f 31 mag_offset[1] = 0;
Airium 0:7864abfabe2f 32 mag_offset[2] = 0;
Airium 0:7864abfabe2f 33 mag_scale[0] = 1;
Airium 0:7864abfabe2f 34 mag_scale[1] = 1;
Airium 0:7864abfabe2f 35 mag_scale[2] = 1;
Airium 0:7864abfabe2f 36
Airium 0:7864abfabe2f 37 temp_offset[0] = 0;
Airium 0:7864abfabe2f 38 temp_scale[0] = 1;
Airium 0:7864abfabe2f 39 }
Airium 0:7864abfabe2f 40
Airium 0:7864abfabe2f 41 void LSM303DLHC::GetAcc(float arr[3]){
Airium 0:7864abfabe2f 42 data[0] = OUT_X_L_A | (1 << 7); // MSB=1 to read multiple bytes
Airium 0:7864abfabe2f 43 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 44 i2c.read(ACC_ADDRESS, data, 6);
Airium 0:7864abfabe2f 45
Airium 4:8723c07d4c45 46 arr[0] = acc_scale[0]*(acc_offset[0]+((short)(data[1]<<8 | data[0])>>4));
Airium 4:8723c07d4c45 47 arr[1] = acc_scale[1]*(acc_offset[1]+((short)(data[3]<<8 | data[2])>>4));
Airium 4:8723c07d4c45 48 arr[2] = acc_scale[2]*(acc_offset[2]+((short)(data[5]<<8 | data[4])>>4));
Airium 0:7864abfabe2f 49 }
Airium 0:7864abfabe2f 50
Airium 0:7864abfabe2f 51 void LSM303DLHC::GetMag(float arr[3]){
Airium 0:7864abfabe2f 52 data[0] = OUT_X_H_M;
Airium 0:7864abfabe2f 53 i2c.write(MAG_ADDRESS, data, 1);
Airium 0:7864abfabe2f 54 i2c.read(MAG_ADDRESS, data, 6);
Airium 0:7864abfabe2f 55 arr[0] = mag_scale[0]*(mag_offset[0]+(short)(data[0]<<8 | data[1]));
Airium 0:7864abfabe2f 56 arr[1] = mag_scale[1]*(mag_offset[1]+(short)(data[4]<<8 | data[5]));
Airium 0:7864abfabe2f 57 arr[2] = mag_scale[2]*(mag_offset[2]+(short)(data[2]<<8 | data[3]));
Airium 0:7864abfabe2f 58 }
Airium 0:7864abfabe2f 59
Airium 0:7864abfabe2f 60 void LSM303DLHC::GetTemp(float arr[1]){
Airium 0:7864abfabe2f 61 data[0] = TEMP_OUT_H_M;
Airium 0:7864abfabe2f 62 i2c.write(MAG_ADDRESS, data, 1);
Airium 0:7864abfabe2f 63 i2c.read(MAG_ADDRESS, data, 2);
Airium 0:7864abfabe2f 64 arr[0] = temp_scale[0]*(temp_offset[0]+((short)(data[0]<<8 | data[1])>>4));
Airium 0:7864abfabe2f 65 }
Airium 0:7864abfabe2f 66
Airium 0:7864abfabe2f 67 void LSM303DLHC::ACtrl(ACC_ODR cmd){
Airium 0:7864abfabe2f 68 data[0] = CTRL_REG1_A;
Airium 0:7864abfabe2f 69 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 70 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 71 data[1] = data[1] & (0b00001111) | (cmd<<4);
Airium 0:7864abfabe2f 72 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 73 }
Airium 0:7864abfabe2f 74
Airium 0:7864abfabe2f 75 void LSM303DLHC::ACtrl(ACC_LPen cmd){
Airium 0:7864abfabe2f 76 data[0] = CTRL_REG1_A;
Airium 0:7864abfabe2f 77 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 78 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 79 data[1] = data[1] & (0b11110111) | (cmd<<3);
Airium 0:7864abfabe2f 80 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 81 }
Airium 0:7864abfabe2f 82
Airium 1:e68ce5025dad 83 void LSM303DLHC::ACtrl(ACC_AXIS cmd){
Airium 1:e68ce5025dad 84 data[0] = CTRL_REG1_A;
Airium 1:e68ce5025dad 85 i2c.write(ACC_ADDRESS, data, 1);
Airium 1:e68ce5025dad 86 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 1:e68ce5025dad 87 data[1] = data[1] & (0b11111000) | (cmd<<0);
Airium 1:e68ce5025dad 88 i2c.write(ACC_ADDRESS, data, 2);
Airium 1:e68ce5025dad 89 }
Airium 1:e68ce5025dad 90
Airium 0:7864abfabe2f 91 void LSM303DLHC::ACtrl(ACC_HPM cmd){
Airium 0:7864abfabe2f 92 data[0] = CTRL_REG2_A;
Airium 0:7864abfabe2f 93 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 94 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 95 data[1] = data[1] & (0b00111111) | (cmd<<6);
Airium 0:7864abfabe2f 96 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 97 }
Airium 0:7864abfabe2f 98
Airium 0:7864abfabe2f 99 void LSM303DLHC::ACtrl(ACC_HPCF cmd){
Airium 0:7864abfabe2f 100 data[0] = CTRL_REG2_A;
Airium 0:7864abfabe2f 101 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 102 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 103 data[1] = data[1] & (0b11001111) | (cmd<<4);
Airium 0:7864abfabe2f 104 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 105 }
Airium 0:7864abfabe2f 106
Airium 0:7864abfabe2f 107 void LSM303DLHC::ACtrl(ACC_FDS cmd){
Airium 0:7864abfabe2f 108 data[0] = CTRL_REG2_A;
Airium 0:7864abfabe2f 109 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 110 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 111 data[1] = data[1] & (0b11110111) | (cmd<<3);
Airium 0:7864abfabe2f 112 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 113 }
Airium 0:7864abfabe2f 114
Airium 0:7864abfabe2f 115 void LSM303DLHC::ACtrl(ACC_BDU cmd){
Airium 0:7864abfabe2f 116 data[0] = CTRL_REG4_A;
Airium 0:7864abfabe2f 117 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 118 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 119 data[1] = data[1] & (0b01111111) | (cmd<<7);
Airium 0:7864abfabe2f 120 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 121 }
Airium 0:7864abfabe2f 122
Airium 0:7864abfabe2f 123 void LSM303DLHC::ACtrl(ACC_FS cmd){
Airium 0:7864abfabe2f 124 data[0] = CTRL_REG4_A;
Airium 0:7864abfabe2f 125 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 126 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 127 data[1] = data[1] & (0b11001111) | (cmd<<4);
Airium 0:7864abfabe2f 128 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 129 }
Airium 0:7864abfabe2f 130
Airium 0:7864abfabe2f 131 void LSM303DLHC::ACtrl(ACC_HR cmd){
Airium 0:7864abfabe2f 132 data[0] = CTRL_REG4_A;
Airium 0:7864abfabe2f 133 i2c.write(ACC_ADDRESS, data, 1);
Airium 0:7864abfabe2f 134 i2c.read(ACC_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 135 data[1] = data[1] & (0b11110111) | (cmd<<3);
Airium 0:7864abfabe2f 136 i2c.write(ACC_ADDRESS, data, 2);
Airium 0:7864abfabe2f 137 }
Airium 0:7864abfabe2f 138
Airium 0:7864abfabe2f 139 void LSM303DLHC::TCtrl(TEMP_EN cmd){
Airium 0:7864abfabe2f 140 data[0] = CRA_REG_M;
Airium 0:7864abfabe2f 141 i2c.write(MAG_ADDRESS, data, 1);
Airium 0:7864abfabe2f 142 i2c.read(MAG_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 143 data[1] = data[1] & (0b01111111) | (cmd<<7);
Airium 0:7864abfabe2f 144 i2c.write(MAG_ADDRESS, data, 2);
Airium 0:7864abfabe2f 145 }
Airium 0:7864abfabe2f 146
Airium 0:7864abfabe2f 147 void LSM303DLHC::MCtrl(MAG_DR cmd){
Airium 0:7864abfabe2f 148 data[0] = CRA_REG_M;
Airium 0:7864abfabe2f 149 i2c.write(MAG_ADDRESS, data, 1);
Airium 0:7864abfabe2f 150 i2c.read(MAG_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 151 data[1] = data[1] & (0b11100011) | (cmd<<2);
Airium 0:7864abfabe2f 152 i2c.write(MAG_ADDRESS, data, 2);
Airium 0:7864abfabe2f 153 }
Airium 0:7864abfabe2f 154
Airium 0:7864abfabe2f 155 void LSM303DLHC::MCtrl(MAG_GN cmd){
Airium 0:7864abfabe2f 156 data[0] = CRB_REG_M;
Airium 0:7864abfabe2f 157 i2c.write(MAG_ADDRESS, data, 1);
Airium 0:7864abfabe2f 158 i2c.read(MAG_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 159 data[1] = data[1] & (0b00011111) | (cmd<<5);
Airium 0:7864abfabe2f 160 i2c.write(MAG_ADDRESS, data, 2);
Airium 0:7864abfabe2f 161 }
Airium 0:7864abfabe2f 162
Airium 0:7864abfabe2f 163 void LSM303DLHC::MCtrl(MAG_MD cmd){
Airium 0:7864abfabe2f 164 data[0] = MR_REG_M;
Airium 0:7864abfabe2f 165 i2c.write(MAG_ADDRESS, data, 1);
Airium 0:7864abfabe2f 166 i2c.read(MAG_ADDRESS, &data[1], 1);
Airium 0:7864abfabe2f 167 data[1] = data[1] & (0b11111100) | (cmd<<0);
Airium 0:7864abfabe2f 168 i2c.write(MAG_ADDRESS, data, 2);
Airium 0:7864abfabe2f 169 }
Airium 0:7864abfabe2f 170
Airium 0:7864abfabe2f 171 void LSM303DLHC::WriteReg(int sad, char d[2]){
Airium 0:7864abfabe2f 172 i2c.write(sad, d, 2);
Airium 0:7864abfabe2f 173 }
Airium 0:7864abfabe2f 174
Airium 3:522d01930e6a 175 void LSM303DLHC::ACal(float offset[3], float scale[3]){
Airium 0:7864abfabe2f 176 acc_offset[0] = offset[0];
Airium 0:7864abfabe2f 177 acc_offset[1] = offset[1];
Airium 0:7864abfabe2f 178 acc_offset[2] = offset[2];
Airium 0:7864abfabe2f 179 acc_scale[0] = scale[0];
Airium 0:7864abfabe2f 180 acc_scale[1] = scale[1];
Airium 0:7864abfabe2f 181 acc_scale[2] = scale[2];
Airium 0:7864abfabe2f 182 }
Airium 0:7864abfabe2f 183
Airium 3:522d01930e6a 184 void LSM303DLHC::MCal(float offset[3], float scale[3]){
Airium 0:7864abfabe2f 185 mag_offset[0] = offset[0];
Airium 0:7864abfabe2f 186 mag_offset[1] = offset[1];
Airium 0:7864abfabe2f 187 mag_offset[2] = offset[2];
Airium 0:7864abfabe2f 188 mag_scale[0] = scale[0];
Airium 0:7864abfabe2f 189 mag_scale[1] = scale[1];
Airium 0:7864abfabe2f 190 mag_scale[2] = scale[2];
Airium 0:7864abfabe2f 191 }
Airium 0:7864abfabe2f 192
Airium 3:522d01930e6a 193 void LSM303DLHC::TCal(float offset[1], float scale[1]){
Airium 0:7864abfabe2f 194 temp_offset[0] = offset[0];
Airium 0:7864abfabe2f 195 temp_scale[0] = scale[0];
Airium 0:7864abfabe2f 196 }