SHT30-DIS-B Library
Dependents: M1DK_Skywire_Demo M1DK_Skywire_Demo1
Fork of LPS331 by
LPS331.cpp@1:f70e671b008f, 2014-10-10 (annotated)
- Committer:
- yasuyuki
- Date:
- Fri Oct 10 04:37:48 2014 +0000
- Revision:
- 1:f70e671b008f
- Parent:
- 0:ef4f624dc3ec
H last
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yasuyuki | 0:ef4f624dc3ec | 1 | //********************** |
yasuyuki | 0:ef4f624dc3ec | 2 | // LPS331.cpp for mbed |
yasuyuki | 0:ef4f624dc3ec | 3 | // |
yasuyuki | 0:ef4f624dc3ec | 4 | // LPS331 barometer(P0_5,P0_4); |
yasuyuki | 0:ef4f624dc3ec | 5 | // or |
yasuyuki | 0:ef4f624dc3ec | 6 | // I2C i2c(P0_5,P0_4); |
yasuyuki | 0:ef4f624dc3ec | 7 | // LPS331 barometer(i2c); |
yasuyuki | 0:ef4f624dc3ec | 8 | // |
yasuyuki | 0:ef4f624dc3ec | 9 | // (C)Copyright 2014 All rights reserved by Y.Onodera |
yasuyuki | 0:ef4f624dc3ec | 10 | // http://einstlab.web.fc2.com |
yasuyuki | 0:ef4f624dc3ec | 11 | //********************** |
yasuyuki | 0:ef4f624dc3ec | 12 | |
yasuyuki | 0:ef4f624dc3ec | 13 | #include "mbed.h" |
yasuyuki | 0:ef4f624dc3ec | 14 | #include "LPS331.h" |
yasuyuki | 0:ef4f624dc3ec | 15 | |
yasuyuki | 0:ef4f624dc3ec | 16 | LPS331::LPS331 (PinName sda, PinName scl) : _i2c(sda, scl) { |
yasuyuki | 0:ef4f624dc3ec | 17 | init(); |
yasuyuki | 0:ef4f624dc3ec | 18 | } |
yasuyuki | 0:ef4f624dc3ec | 19 | LPS331::LPS331 (I2C& p_i2c) : _i2c(p_i2c) { |
yasuyuki | 0:ef4f624dc3ec | 20 | init(); |
yasuyuki | 0:ef4f624dc3ec | 21 | } |
yasuyuki | 0:ef4f624dc3ec | 22 | |
yasuyuki | 0:ef4f624dc3ec | 23 | void LPS331::put(unsigned char a, unsigned char b) |
yasuyuki | 0:ef4f624dc3ec | 24 | { |
yasuyuki | 0:ef4f624dc3ec | 25 | buf[0]=a; |
yasuyuki | 0:ef4f624dc3ec | 26 | buf[1]=b; |
yasuyuki | 0:ef4f624dc3ec | 27 | _i2c.write(LPS331_ADDR, buf, 2); |
yasuyuki | 0:ef4f624dc3ec | 28 | } |
yasuyuki | 0:ef4f624dc3ec | 29 | |
yasuyuki | 0:ef4f624dc3ec | 30 | |
yasuyuki | 0:ef4f624dc3ec | 31 | void LPS331::get(unsigned char a) |
yasuyuki | 0:ef4f624dc3ec | 32 | { |
yasuyuki | 0:ef4f624dc3ec | 33 | buf[0] = a; |
yasuyuki | 0:ef4f624dc3ec | 34 | _i2c.write(LPS331_ADDR, buf, 1, true); // no stop, repeated |
yasuyuki | 0:ef4f624dc3ec | 35 | _i2c.read( LPS331_ADDR, buf, 1); |
yasuyuki | 0:ef4f624dc3ec | 36 | |
yasuyuki | 0:ef4f624dc3ec | 37 | } |
yasuyuki | 0:ef4f624dc3ec | 38 | |
yasuyuki | 0:ef4f624dc3ec | 39 | long LPS331::value() |
yasuyuki | 0:ef4f624dc3ec | 40 | { |
yasuyuki | 0:ef4f624dc3ec | 41 | |
yasuyuki | 1:f70e671b008f | 42 | // XL first and H last |
yasuyuki | 1:f70e671b008f | 43 | // get press_xl |
yasuyuki | 1:f70e671b008f | 44 | get(LPS331_PRESS_POUT_XL_REH); |
yasuyuki | 1:f70e671b008f | 45 | press.byte.LB=buf[0]; |
yasuyuki | 0:ef4f624dc3ec | 46 | // get tpress_low |
yasuyuki | 0:ef4f624dc3ec | 47 | get(LPS331_PRESS_OUT_L); |
yasuyuki | 0:ef4f624dc3ec | 48 | press.byte.HB=buf[0]; |
yasuyuki | 1:f70e671b008f | 49 | // get press_high |
yasuyuki | 1:f70e671b008f | 50 | get(LPS331_PRESS_OUT_H); |
yasuyuki | 1:f70e671b008f | 51 | press.byte.UB=buf[0]; |
yasuyuki | 0:ef4f624dc3ec | 52 | return press.Val; |
yasuyuki | 1:f70e671b008f | 53 | |
yasuyuki | 1:f70e671b008f | 54 | // hPa = press.Val / 4096 |
yasuyuki | 1:f70e671b008f | 55 | // Pa = press.Val / 40.96 |
yasuyuki | 0:ef4f624dc3ec | 56 | } |
yasuyuki | 0:ef4f624dc3ec | 57 | |
yasuyuki | 1:f70e671b008f | 58 | |
yasuyuki | 1:f70e671b008f | 59 | short LPS331::temperature() |
yasuyuki | 1:f70e671b008f | 60 | { |
yasuyuki | 1:f70e671b008f | 61 | |
yasuyuki | 1:f70e671b008f | 62 | // L first and H last |
yasuyuki | 1:f70e671b008f | 63 | // get tpress_low |
yasuyuki | 1:f70e671b008f | 64 | get(LPS331_TEMP_OUT_L); |
yasuyuki | 1:f70e671b008f | 65 | temp.byte.LB=buf[0]; |
yasuyuki | 1:f70e671b008f | 66 | // get press_high |
yasuyuki | 1:f70e671b008f | 67 | get(LPS331_TEMP_OUT_H); |
yasuyuki | 1:f70e671b008f | 68 | temp.byte.HB=buf[0]; |
yasuyuki | 1:f70e671b008f | 69 | return temp.S; |
yasuyuki | 1:f70e671b008f | 70 | |
yasuyuki | 1:f70e671b008f | 71 | // C = 42.5 + temp.S / 480 |
yasuyuki | 1:f70e671b008f | 72 | // range:0 to 80C |
yasuyuki | 1:f70e671b008f | 73 | // accuracy:+-2C |
yasuyuki | 1:f70e671b008f | 74 | } |
yasuyuki | 1:f70e671b008f | 75 | |
yasuyuki | 1:f70e671b008f | 76 | |
yasuyuki | 0:ef4f624dc3ec | 77 | void LPS331::init() |
yasuyuki | 0:ef4f624dc3ec | 78 | { |
yasuyuki | 0:ef4f624dc3ec | 79 | // Power ON Cycle=1Hz |
yasuyuki | 0:ef4f624dc3ec | 80 | put(LPS331_CTRL_REG1, 0x90); |
yasuyuki | 0:ef4f624dc3ec | 81 | } |
yasuyuki | 0:ef4f624dc3ec | 82 | |
yasuyuki | 0:ef4f624dc3ec | 83 | |
yasuyuki | 1:f70e671b008f | 84 |