read pressure and temperature from LPS25h.
Dependencies: mbed
Revision 2:0c2bb6fe6885, committed 2015-04-13
- Comitter:
- onaka
- Date:
- Mon Apr 13 12:54:45 2015 +0000
- Parent:
- 1:813b3c3eb644
- Child:
- 3:844fb47ba7a2
- Commit message:
- LPS25H library to Folder
Changed in this revision
--- a/LPS25H.lib Mon Apr 13 12:47:42 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/yasuyuki/code/LPS25H/#a46488a74343
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPS25H/LPS25H.cpp Mon Apr 13 12:54:45 2015 +0000 @@ -0,0 +1,89 @@ +//********************** +// LPS25H.cpp for mbed +// +// LPS25H lps25h(P0_5,P0_4); +// or +// I2C i2c(P0_5,P0_4); +// LPS25H lps25h(i2c); +// +// (C)Copyright 2014 All rights reserved by Y.Onodera +// http://einstlab.web.fc2.com +//********************** + +#include "mbed.h" +#include "LPS25H.h" + +LPS25H::LPS25H (PinName sda, PinName scl) : _i2c(sda, scl) { + init(); +} +LPS25H::LPS25H (I2C& p_i2c) : _i2c(p_i2c) { + init(); +} + +void LPS25H::put(unsigned char a, unsigned char b) +{ + buf[0]=a; + buf[1]=b; + _i2c.write(LPS25H_ADDR, buf, 2); +} + + +void LPS25H::get(unsigned char a) +{ + buf[0] = a; + _i2c.write(LPS25H_ADDR, buf, 1, true); // no stop, repeated + _i2c.read( LPS25H_ADDR, buf, 1); + +} + +long LPS25H::pressure() +{ + + // XL first and H last + // get press_xl + get(LPS25H_PRESS_OUT_XL); + press.byte.LB=buf[0]; + // get tpress_low + get(LPS25H_PRESS_OUT_L); + press.byte.HB=buf[0]; + // get press_high + get(LPS25H_PRESS_OUT_H); + press.byte.UB=buf[0]; + return press.Val; + + // hPa = press.Val / 4096 + // Pa = press.Val / 40.96 +} + + +short LPS25H::temperature() +{ + + // L first and H last + // get tpress_low + get(LPS25H_TEMP_OUT_L); + temp.byte.LB=buf[0]; + // get press_high + get(LPS25H_TEMP_OUT_H); + temp.byte.HB=buf[0]; + return temp.S; + + // C = 42.5 + temp.S / 480 + // range:0 to 80C + // accuracy:+-2C +} + + +void LPS25H::init() +{ + // Resolution config + put(LPS25H_RES_CONF, 0x05); + // FIFO 4samples moving average + put(LPS25H_FIFO_CTRL, 0xC3); + // FIFO Enable + put(LPS25H_CTRL_REG2, 0x40); + // Power ON Cycle=12.5Hz + put(LPS25H_CTRL_REG1, 0xB0); +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPS25H/LPS25H.h Mon Apr 13 12:54:45 2015 +0000 @@ -0,0 +1,62 @@ +//********************** +// LPS25H.h for mbed +// +// (C)Copyright 2014 All rights reserved by Y.Onodera +// http://einstlab.web.fc2.com +//********************** + +#ifndef LPS25H_H_ +#define LPS25H_H_ + +#define LPS25H_ADDR 0xB8 +#define LPS25H_REF_P_XL 0x08 +#define LPS25H_REF_P_L 0x09 +#define LPS25H_REF_P_H 0x0A +#define LPS25H_WHO_AM_I 0x0F +#define LPS25H_RES_CONF 0x10 +#define LPS25H_CTRL_REG1 0x20 +#define LPS25H_CTRL_REG2 0x21 +#define LPS25H_CTRL_REG3 0x22 +#define LPS25H_CTRL_REG4 0x23 +#define LPS25H_INT_CFG 0x24 +#define LPS25H_INT_SOURCE 0x25 +#define LPS25H_STATUS_REG 0x27 +#define LPS25H_PRESS_OUT_XL 0x28 +#define LPS25H_PRESS_OUT_L 0x29 +#define LPS25H_PRESS_OUT_H 0x2A +#define LPS25H_TEMP_OUT_L 0x2B +#define LPS25H_TEMP_OUT_H 0x2C +#define LPS25H_FIFO_CTRL 0x2E +#define LPS25H_FIFO_STATUS 0x2F +#define LPS25H_THS_P_L 0x30 +#define LPS25H_THS_P_H 0x31 +#define LPS25H_RPDS_L 0x39 +#define LPS25H_RPDS_H 0x3A + +#include "mbed.h" +#include "typedef.h" + +class LPS25H{ +public: + LPS25H (PinName sda, PinName scl); + LPS25H (I2C& p_i2c); + void init(); + + void put(unsigned char a, unsigned char b); + void get(unsigned char a); + long pressure(); + short temperature(); + +protected: + + I2C _i2c; + + DWORD_VAL press; + WORD_VAL temp; + char buf[2]; + +}; + +#endif /* LPS25H_H_ */ + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPS25H/typedef.h Mon Apr 13 12:54:45 2015 +0000 @@ -0,0 +1,55 @@ +//********************** +// typedef for mbed +// +// Condition: +// +// (C)Copyright 2014 All rights reserved by Y.Onodera +// http://einstlab.web.fc2.com +//********************** +#ifndef TYPEDEF_H +#define TYPEDEF_H + +typedef unsigned char BYTE; /* 8-bit unsigned */ +typedef unsigned short int WORD; /* 16-bit unsigned */ +typedef unsigned int DWORD; /* 32-bit unsigned */ +typedef unsigned long long QWORD; /* 64-bit unsigned */ + + +typedef union +{ + WORD Val; + BYTE v[2]; + short S; + struct + { + BYTE LB; + BYTE HB; + } byte; +} WORD_VAL; + +typedef union +{ + DWORD Val; + WORD w[2]; + BYTE v[4]; + struct + { + WORD LW; + WORD HW; + } word; + struct + { + BYTE LB; + BYTE HB; + BYTE UB; + BYTE MB; + } byte; + struct + { + WORD_VAL low; + WORD_VAL high; + }wordUnion; + +} DWORD_VAL; + +#endif /* TYPEDEF_H */