MPL115A2 library
Embed:
(wiki syntax)
Show/hide line numbers
MPL115.cpp
00001 //********************** 00002 // MPL115.cpp for mbed 00003 // 00004 // MPL115A2 mpl115a2(P0_5,P0_4); 00005 // or 00006 // I2C i2c(P0_5,P0_4); 00007 // MPL115A2 mpl115a2(i2c); 00008 // 00009 // (C)Copyright 2014 All rights reserved by Y.Onodera 00010 // http://einstlab.web.fc2.com 00011 //********************** 00012 00013 #include "mbed.h" 00014 #include "MPL115.h" 00015 00016 MPL115A2::MPL115A2 (PinName sda, PinName scl) : _i2c(sda, scl) { 00017 init(); 00018 } 00019 MPL115A2::MPL115A2 (I2C& p_i2c) : _i2c(p_i2c) { 00020 init(); 00021 } 00022 00023 00024 void MPL115A2::start() 00025 { 00026 00027 // Start Conversion 00028 buf[0] = 0x12; 00029 buf[1] = 0x01; 00030 _i2c.write(MPL115_ADDR, buf, 2); 00031 00032 wait_ms(5); 00033 00034 // Read Results 00035 buf[0] = 0x00; 00036 _i2c.write(MPL115_ADDR, buf, 1, false); // no stop, repeated 00037 _i2c.read( MPL115_ADDR, buf, 4); 00038 00039 Padc.byte.HB=buf[0]; 00040 Padc.byte.LB=buf[1]; 00041 Tadc.byte.HB=buf[2]; 00042 Tadc.byte.LB=buf[3]; 00043 00044 } 00045 00046 00047 short MPL115A2::temperature() 00048 { 00049 start(); 00050 00051 Tadc.Val >>= 6; // to adjust 10 bit 00052 return Tadc.S; 00053 } 00054 00055 00056 short MPL115A2::pressure() 00057 { 00058 00059 // Pcomp = a0 + (b1 + c12 * Tadc) * Padc + b2 * Tadc 00060 int c12x2, a1, a1x1, y1, a2x2, Pcomp; 00061 00062 start(); 00063 00064 Padc.Val >>= 6; // to adjust 10 bit 00065 Tadc.Val >>= 6; // to adjust 10 bit 00066 c12x2 = (((int)c12.S) * (int)Tadc.S) >> 11; // c12x2 = c12 * Tadc 00067 a1 = (int)b1.S + c12x2; // a1 = b1 + c12x2 00068 a1x1 = a1 * (int)Padc.S; // a1x1 = a1 * Padc 00069 y1 = (((int)a0.S) << 10) + a1x1; // y1 = a0 + a1x1 00070 a2x2 = (((int)b2.S) * (int)Tadc.S) >> 1; // a2x2 = b2 * Tadc 00071 Pcomp = (y1 + a2x2) >> 9; // Pcomp = y1 + a2x2 00072 00073 return (short)Pcomp; 00074 00075 // hPa = (Pcomp/16) * (115.0-50.0)/1023.0 + 50.0 00076 } 00077 00078 void MPL115A2::init() 00079 { 00080 00081 buf[0] = 0x04; 00082 _i2c.write(MPL115_ADDR, buf, 1, false); // no stop, repeated 00083 _i2c.read( MPL115_ADDR, buf, 8); 00084 00085 a0.byte.HB=buf[0]; 00086 a0.byte.LB=buf[1]; 00087 b1.byte.HB=buf[2]; 00088 b1.byte.LB=buf[3]; 00089 b2.byte.HB=buf[4]; 00090 b2.byte.LB=buf[5]; 00091 c12.byte.HB=buf[6]; 00092 c12.byte.LB=buf[7]; 00093 00094 } 00095 00096
Generated on Fri Jul 22 2022 16:00:12 by
1.7.2