mpl115A2 i2c pressure and temperature sensor

Dependents:   HTTPClient_SuperTweet_MPL115A2 mpl115a2_display_local

Committer:
joeh
Date:
Mon May 07 14:04:39 2012 +0000
Revision:
1:6dbe3322f203
Parent:
0:5a72175e5cb2
edited comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joeh 0:5a72175e5cb2 1 /******************************************************************************
joeh 0:5a72175e5cb2 2 * Includes
joeh 0:5a72175e5cb2 3 ******************************************************************************/
joeh 0:5a72175e5cb2 4 #include "MPL115A2.h"
joeh 0:5a72175e5cb2 5 #include "mbed.h"
joeh 0:5a72175e5cb2 6
joeh 0:5a72175e5cb2 7 /******************************************************************************
joeh 0:5a72175e5cb2 8 * Constructors
joeh 0:5a72175e5cb2 9 ******************************************************************************/
joeh 0:5a72175e5cb2 10 MPL115A2::MPL115A2(I2C *i2c, const PinName shdnPin):
joeh 0:5a72175e5cb2 11 _i2c_address(0xc0), _i2c(i2c), _shdnPin(shdnPin),_debug(false),_pc(NULL)
joeh 0:5a72175e5cb2 12 {
joeh 0:5a72175e5cb2 13
joeh 0:5a72175e5cb2 14 }
joeh 0:5a72175e5cb2 15
joeh 0:5a72175e5cb2 16 MPL115A2::MPL115A2(I2C *i2c, Serial *pc, const PinName shdnPin):
joeh 0:5a72175e5cb2 17 _i2c_address(0xc0), _i2c(i2c), _shdnPin(shdnPin), _pc(pc), _debug(true)
joeh 0:5a72175e5cb2 18 {
joeh 0:5a72175e5cb2 19
joeh 0:5a72175e5cb2 20 }
joeh 0:5a72175e5cb2 21
joeh 0:5a72175e5cb2 22 /******************************************************************************
joeh 0:5a72175e5cb2 23 * Global Functions
joeh 0:5a72175e5cb2 24 ******************************************************************************/
joeh 0:5a72175e5cb2 25 void MPL115A2::ReadSensor()
joeh 0:5a72175e5cb2 26 {
joeh 0:5a72175e5cb2 27 int res;
joeh 0:5a72175e5cb2 28
joeh 0:5a72175e5cb2 29 //wakeup
joeh 0:5a72175e5cb2 30 _shdnPin=1;
joeh 0:5a72175e5cb2 31 _bShutdown = false;
joeh 0:5a72175e5cb2 32 wait(0.001); // wait the device to be ready
joeh 0:5a72175e5cb2 33
joeh 0:5a72175e5cb2 34 // start AD conversions
joeh 0:5a72175e5cb2 35 char cmd[2];
joeh 0:5a72175e5cb2 36 cmd[0]=0x12;
joeh 0:5a72175e5cb2 37 cmd[1]=0x01;
joeh 0:5a72175e5cb2 38 res = _i2c->write(_i2c_address,cmd,2);
joeh 0:5a72175e5cb2 39
joeh 0:5a72175e5cb2 40 if(_debug)
joeh 0:5a72175e5cb2 41 {
joeh 0:5a72175e5cb2 42 if(res==0)
joeh 0:5a72175e5cb2 43 {
joeh 0:5a72175e5cb2 44 _pc->printf("Success: Requested temp and pressure \n");
joeh 0:5a72175e5cb2 45 }
joeh 0:5a72175e5cb2 46 else
joeh 0:5a72175e5cb2 47 {
joeh 0:5a72175e5cb2 48 _pc->printf("Failed : Requested temp and pressure %d\n", res);
joeh 0:5a72175e5cb2 49 }
joeh 0:5a72175e5cb2 50 }
joeh 0:5a72175e5cb2 51
joeh 0:5a72175e5cb2 52 wait(0.01);
joeh 0:5a72175e5cb2 53 cmd[0]=0x00;
joeh 0:5a72175e5cb2 54 res = _i2c->write(_i2c_address,cmd,1);
joeh 0:5a72175e5cb2 55
joeh 0:5a72175e5cb2 56 if(_debug)
joeh 0:5a72175e5cb2 57 {
joeh 0:5a72175e5cb2 58 if(res==0)
joeh 0:5a72175e5cb2 59 {
joeh 0:5a72175e5cb2 60 _pc->printf("Success: Select temp and pressure register \n");
joeh 0:5a72175e5cb2 61 }
joeh 0:5a72175e5cb2 62 else
joeh 0:5a72175e5cb2 63 {
joeh 0:5a72175e5cb2 64 _pc->printf("Failed : Select temp and pressure register %d\n", res);
joeh 0:5a72175e5cb2 65 }
joeh 0:5a72175e5cb2 66 }
joeh 0:5a72175e5cb2 67
joeh 0:5a72175e5cb2 68 // compensation
joeh 0:5a72175e5cb2 69 char data[4];
joeh 0:5a72175e5cb2 70 res = _i2c->read(_i2c_address, data, 4);
joeh 0:5a72175e5cb2 71
joeh 0:5a72175e5cb2 72 if(_debug)
joeh 0:5a72175e5cb2 73 {
joeh 0:5a72175e5cb2 74 if(res==0)
joeh 0:5a72175e5cb2 75 {
joeh 0:5a72175e5cb2 76 _pc->printf("Success: Read temp and pressure register \n");
joeh 0:5a72175e5cb2 77 }
joeh 0:5a72175e5cb2 78 else
joeh 0:5a72175e5cb2 79 {
joeh 0:5a72175e5cb2 80 _pc->printf("Failed : Read temp and pressure register %d\n", res);
joeh 0:5a72175e5cb2 81 }
joeh 0:5a72175e5cb2 82 }
joeh 0:5a72175e5cb2 83
joeh 0:5a72175e5cb2 84 if(res == 0)
joeh 0:5a72175e5cb2 85 {
joeh 0:5a72175e5cb2 86 _uiPadc = (unsigned short) data[0] << 8;
joeh 0:5a72175e5cb2 87 _uiPadc += (unsigned short) data[1] & 0x00FF;
joeh 0:5a72175e5cb2 88 _uiTadc = (unsigned short) data[2] << 8;
joeh 0:5a72175e5cb2 89 _uiTadc += (unsigned short) data[3] & 0x00FF;
joeh 0:5a72175e5cb2 90
joeh 0:5a72175e5cb2 91 // Coefficient 9 equation compensation
joeh 0:5a72175e5cb2 92 _uiPadc = _uiPadc >> 6;
joeh 0:5a72175e5cb2 93 _uiTadc = _uiTadc >> 6;
joeh 0:5a72175e5cb2 94 }
joeh 0:5a72175e5cb2 95 }
joeh 0:5a72175e5cb2 96
joeh 0:5a72175e5cb2 97
joeh 0:5a72175e5cb2 98 /**********************************************************
joeh 0:5a72175e5cb2 99 * GetPressure
joeh 0:5a72175e5cb2 100 * Gets the current pressure from the sensor.
joeh 0:5a72175e5cb2 101 *
joeh 0:5a72175e5cb2 102 * @return float - The local pressure in kPa
joeh 0:5a72175e5cb2 103 **********************************************************/
joeh 0:5a72175e5cb2 104 float MPL115A2::GetPressure()
joeh 0:5a72175e5cb2 105 {
joeh 0:5a72175e5cb2 106 short siPcomp;
joeh 0:5a72175e5cb2 107 int lt1, lt2, lt3, si_c11x1, si_a11, si_c12x2;
joeh 0:5a72175e5cb2 108 int si_a1, si_c22x2, si_a2, si_a1x1, si_y1, si_a2x2;
joeh 0:5a72175e5cb2 109
joeh 0:5a72175e5cb2 110 // Step 1 c11x1 = c11 * Padc
joeh 0:5a72175e5cb2 111 lt1 = (int) _sic11;
joeh 0:5a72175e5cb2 112 lt2 = (int) _uiPadc;
joeh 0:5a72175e5cb2 113 lt3 = lt1*lt2;
joeh 0:5a72175e5cb2 114 si_c11x1 = (int) lt3;
joeh 0:5a72175e5cb2 115
joeh 0:5a72175e5cb2 116 // Step 2 a11 = b1 + c11x1
joeh 0:5a72175e5cb2 117 lt1 = ((int)_sib1)<<14;
joeh 0:5a72175e5cb2 118 lt2 = (int) si_c11x1;
joeh 0:5a72175e5cb2 119 lt3 = lt1 + lt2;
joeh 0:5a72175e5cb2 120 si_a11 = (int)(lt3>>14);
joeh 0:5a72175e5cb2 121
joeh 0:5a72175e5cb2 122 // Step 3 c12x2 = c12 * Tadc
joeh 0:5a72175e5cb2 123 lt1 = (int) _sic12;
joeh 0:5a72175e5cb2 124 lt2 = (int) _uiTadc;
joeh 0:5a72175e5cb2 125 lt3 = lt1*lt2;
joeh 0:5a72175e5cb2 126 si_c12x2 = (int)lt3;
joeh 0:5a72175e5cb2 127
joeh 0:5a72175e5cb2 128 // Step 4 a1 = a11 + c12x2
joeh 0:5a72175e5cb2 129 lt1 = ((int)si_a11<<11);
joeh 0:5a72175e5cb2 130 lt2 = (int)si_c12x2;
joeh 0:5a72175e5cb2 131 lt3 = lt1 + lt2;
joeh 0:5a72175e5cb2 132 si_a1 = (int) lt3>>11;
joeh 0:5a72175e5cb2 133
joeh 0:5a72175e5cb2 134 // Step 5 c22x2 = c22*Tadc
joeh 0:5a72175e5cb2 135 lt1 = (int)_sic22;
joeh 0:5a72175e5cb2 136 lt2 = (int)_uiTadc;
joeh 0:5a72175e5cb2 137 lt3 = lt1 * lt2;
joeh 0:5a72175e5cb2 138 si_c22x2 = (int)(lt3);
joeh 0:5a72175e5cb2 139
joeh 0:5a72175e5cb2 140 // Step 6 a2 = b2 + c22x2
joeh 0:5a72175e5cb2 141 lt1 = ((int)_sib2<<15);
joeh 0:5a72175e5cb2 142 lt2 = ((int)si_c22x2>1);
joeh 0:5a72175e5cb2 143 lt3 = lt1+lt2;
joeh 0:5a72175e5cb2 144 si_a2 = ((int)lt3>>16);
joeh 0:5a72175e5cb2 145
joeh 0:5a72175e5cb2 146 // Step 7 a1x1 = a1 * Padc
joeh 0:5a72175e5cb2 147 lt1 = (int)si_a1;
joeh 0:5a72175e5cb2 148 lt2 = (int)_uiPadc;
joeh 0:5a72175e5cb2 149 lt3 = lt1*lt2;
joeh 0:5a72175e5cb2 150 si_a1x1 = (int)(lt3);
joeh 0:5a72175e5cb2 151
joeh 0:5a72175e5cb2 152 // Step 8 y1 = a0 + a1x1
joeh 0:5a72175e5cb2 153 lt1 = ((int)_sia0<<10);
joeh 0:5a72175e5cb2 154 lt2 = (int)si_a1x1;
joeh 0:5a72175e5cb2 155 lt3 = lt1+lt2;
joeh 0:5a72175e5cb2 156 si_y1 = ((int)lt3>>10);
joeh 0:5a72175e5cb2 157
joeh 0:5a72175e5cb2 158 // Step 9 a2x2 = a2 * Tadc
joeh 0:5a72175e5cb2 159 lt1 = (int)si_a2;
joeh 0:5a72175e5cb2 160 lt2 = (int)_uiTadc;
joeh 0:5a72175e5cb2 161 lt3 = lt1*lt2;
joeh 0:5a72175e5cb2 162 si_a2x2 = (int)(lt3);
joeh 0:5a72175e5cb2 163
joeh 0:5a72175e5cb2 164 // Step 10 pComp = y1 + a2x2
joeh 0:5a72175e5cb2 165 lt1 = ((int)si_y1<<10);
joeh 0:5a72175e5cb2 166 lt2 = (int)si_a2x2;
joeh 0:5a72175e5cb2 167 lt3 = lt1+lt2;
joeh 0:5a72175e5cb2 168
joeh 0:5a72175e5cb2 169 // Fixed point result with rounding
joeh 0:5a72175e5cb2 170 siPcomp = ((int)lt3>>13);
joeh 0:5a72175e5cb2 171
joeh 0:5a72175e5cb2 172 if(_debug)
joeh 0:5a72175e5cb2 173 {
joeh 0:5a72175e5cb2 174 _pc->printf("Success: Calculated Pressure Compesation Value %d\n", siPcomp);
joeh 0:5a72175e5cb2 175 }
joeh 0:5a72175e5cb2 176
joeh 0:5a72175e5cb2 177 // decPcomp is defined as a floating point number
joeh 0:5a72175e5cb2 178 // Conversion to decimal value from 1023 ADC count value
joeh 0:5a72175e5cb2 179 // ADC counts are 0 to 1023, pressure is 50 to 115kPa respectively
joeh 0:5a72175e5cb2 180 return (((650.0/1023.0)*siPcomp)+500);
joeh 0:5a72175e5cb2 181 }
joeh 0:5a72175e5cb2 182
joeh 0:5a72175e5cb2 183
joeh 0:5a72175e5cb2 184 /**********************************************************
joeh 0:5a72175e5cb2 185 * GetTemperature
joeh 0:5a72175e5cb2 186 * Gets the current temperature from the sensor.
joeh 0:5a72175e5cb2 187 *
joeh 0:5a72175e5cb2 188 * @return float - The local pressure in °C
joeh 0:5a72175e5cb2 189 **********************************************************/
joeh 0:5a72175e5cb2 190 float MPL115A2::GetTemperature()
joeh 0:5a72175e5cb2 191 {
joeh 0:5a72175e5cb2 192 return (25 + ((_uiTadc - 498.0) / -5.35));
joeh 0:5a72175e5cb2 193 }
joeh 0:5a72175e5cb2 194
joeh 0:5a72175e5cb2 195
joeh 0:5a72175e5cb2 196 /**********************************************************
joeh 0:5a72175e5cb2 197 * Begin
joeh 0:5a72175e5cb2 198 * Gets the coefficients from the sensor.
joeh 0:5a72175e5cb2 199 *
joeh 0:5a72175e5cb2 200 **********************************************************/
joeh 0:5a72175e5cb2 201 void MPL115A2::begin()
joeh 0:5a72175e5cb2 202 {
joeh 0:5a72175e5cb2 203 short sia0MSB, sia0LSB;
joeh 0:5a72175e5cb2 204 short sib1MSB, sib1LSB;
joeh 0:5a72175e5cb2 205 short sib2MSB, sib2LSB;
joeh 0:5a72175e5cb2 206 short sic12MSB, sic12LSB;
joeh 0:5a72175e5cb2 207 short sic11MSB, sic11LSB;
joeh 0:5a72175e5cb2 208 short sic22MSB, sic22LSB;
joeh 0:5a72175e5cb2 209
joeh 0:5a72175e5cb2 210 //wakeup
joeh 0:5a72175e5cb2 211 _shdnPin=1;
joeh 0:5a72175e5cb2 212 _bShutdown = false;
joeh 0:5a72175e5cb2 213 wait(0.001); // wait the device to be ready
joeh 0:5a72175e5cb2 214
joeh 0:5a72175e5cb2 215 //send request for coefficients
joeh 0:5a72175e5cb2 216 char cmd[1];
joeh 0:5a72175e5cb2 217 cmd[0] = 0x04;
joeh 0:5a72175e5cb2 218 _i2c->write(_i2c_address, cmd, 1);
joeh 0:5a72175e5cb2 219
joeh 0:5a72175e5cb2 220 // read out coefficients
joeh 0:5a72175e5cb2 221 char coeff[12];
joeh 0:5a72175e5cb2 222 int res = _i2c->read(_i2c_address, coeff, 12);
joeh 0:5a72175e5cb2 223 if (res == 0)
joeh 0:5a72175e5cb2 224 {
joeh 0:5a72175e5cb2 225 sia0MSB = coeff[0];
joeh 0:5a72175e5cb2 226 sia0LSB = coeff[1];
joeh 0:5a72175e5cb2 227 sib1MSB = coeff[2];
joeh 0:5a72175e5cb2 228 sib1LSB = coeff[3];
joeh 0:5a72175e5cb2 229 sib2MSB = coeff[4];
joeh 0:5a72175e5cb2 230 sib2LSB = coeff[5];
joeh 0:5a72175e5cb2 231 sic12MSB = coeff[6];
joeh 0:5a72175e5cb2 232 sic12LSB = coeff[7];
joeh 0:5a72175e5cb2 233 sic11MSB = coeff[8];
joeh 0:5a72175e5cb2 234 sic11LSB = coeff[9];
joeh 0:5a72175e5cb2 235 sic22MSB = coeff[10];
joeh 0:5a72175e5cb2 236 sic22LSB = coeff[11];
joeh 0:5a72175e5cb2 237
joeh 0:5a72175e5cb2 238
joeh 0:5a72175e5cb2 239 // Placing coefficients into 16-bit Variables
joeh 0:5a72175e5cb2 240 // a0
joeh 0:5a72175e5cb2 241 _sia0 = (signed int) sia0MSB << 8;
joeh 0:5a72175e5cb2 242 _sia0 += (signed int) sia0LSB & 0x00FF;
joeh 0:5a72175e5cb2 243 // b1
joeh 0:5a72175e5cb2 244 _sib1 = (signed int) sib1MSB << 8;
joeh 0:5a72175e5cb2 245 _sib1 += (signed int) sib1LSB & 0x00FF;
joeh 0:5a72175e5cb2 246 // b2
joeh 0:5a72175e5cb2 247 _sib2 = (signed int) sib2MSB << 8;
joeh 0:5a72175e5cb2 248 _sib2 += (signed int) sib2LSB & 0x00FF;
joeh 0:5a72175e5cb2 249 // c12
joeh 0:5a72175e5cb2 250 _sic12 = (signed int) sic12MSB << 8;
joeh 0:5a72175e5cb2 251 _sic12 += (signed int) sic12LSB & 0x00FF;
joeh 0:5a72175e5cb2 252 // c11
joeh 0:5a72175e5cb2 253 _sic11 = (signed int) sic11MSB << 8;
joeh 0:5a72175e5cb2 254 _sic11 += (signed int) sic11LSB & 0x00FF;
joeh 0:5a72175e5cb2 255 // c22
joeh 0:5a72175e5cb2 256 _sic22 = (signed int) sic22MSB << 8;
joeh 0:5a72175e5cb2 257 _sic22 += (signed int) sic22LSB & 0x00FF;
joeh 0:5a72175e5cb2 258 }
joeh 0:5a72175e5cb2 259 }
joeh 0:5a72175e5cb2 260
joeh 0:5a72175e5cb2 261
joeh 0:5a72175e5cb2 262 void MPL115A2::shutdown()
joeh 0:5a72175e5cb2 263 {
joeh 0:5a72175e5cb2 264 _shdnPin = 0;
joeh 0:5a72175e5cb2 265 _bShutdown = true;
joeh 0:5a72175e5cb2 266 }