Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LPS331_HelloWorld mbed_vfd_thermometer lon-lof
Revision 0:3fd57444bc65, committed 2013-10-20
- Comitter:
- nyamfg
- Date:
- Sun Oct 20 15:22:55 2013 +0000
- Child:
- 1:b7d3d6e82049
- Commit message:
- 1st release.
Changed in this revision
| LPS331_I2C.cpp | Show annotated file Show diff for this revision Revisions of this file |
| LPS331_I2C.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPS331_I2C.cpp Sun Oct 20 15:22:55 2013 +0000
@@ -0,0 +1,112 @@
+/*
+ * I2C/SPI digital pressure sensor "LPS331AP" library for I2C mode.
+ *
+ * Copyright(c) -2013 unos@NYAMFG,
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * revision: see LPS331_I2C.h.
+ */
+
+#include "LPS331_I2C.h"
+
+LPS331_I2C::LPS331_I2C(PinName sda, PinName scl, bool sa0) : _i2c(sda, scl)
+{
+ if(sa0 == LPS331_I2C_SA0_HIGH) {
+ _address = LPS331_I2C_ADDRESS_SA0_HIGH;
+ } else {
+ _address = LPS331_I2C_ADDRESS_SA0_LOW;
+ }
+ _i2c.frequency(400 * 1000);
+ _ctrlreg1 = 0x20;
+}
+
+LPS331_I2C::~LPS331_I2C()
+{
+}
+
+char LPS331_I2C::whoami()
+{
+ return _read(0x0f);
+}
+
+bool LPS331_I2C::isAvailable()
+{
+ if(whoami() == 0xbb) { return true; }
+
+ return false;
+}
+
+void LPS331_I2C::setResolution(char pressure_avg, char temp_avg)
+{
+ _write(0x10, ((temp_avg & 0x07) << 4) | (pressure_avg & 0x0f));
+}
+
+void LPS331_I2C::setActive(bool is_active)
+{
+ if(is_active) {
+ _ctrlreg1 |= 0x80;
+ } else {
+ _ctrlreg1 &= ~0x80;
+ }
+
+ _write(0x20, _ctrlreg1);
+}
+
+void LPS331_I2C::setDataRate(char datarate)
+{
+ datarate &= 0x07;
+
+ _ctrlreg1 &= ~(0x07 << 4);
+ _ctrlreg1 |= datarate << 4;
+
+ _write(0x20, _ctrlreg1);
+}
+
+
+float LPS331_I2C::getPressure()
+{
+ float pressure = 0;
+ pressure += _read(0x28);
+ pressure += _read(0x29) << 8;
+ pressure += _read(0x2a) << 16;
+ pressure /= 4096.0;
+
+ return pressure;
+}
+
+float LPS331_I2C::getTemperature()
+{
+ short temp = 0;
+ temp = _read(0x2b);
+ temp |= _read(0x2c) << 8;
+
+ return (float)(42.5 + temp / 480.0);
+}
+
+
+void LPS331_I2C::_write(char subaddress, char data)
+{
+ _i2c.start();
+ _i2c.write(_address);
+ _i2c.write(subaddress);
+ _i2c.write(data);
+ _i2c.stop();
+}
+
+char LPS331_I2C::_read(char subaddress)
+{
+ char result = 0;
+
+ _i2c.start();
+ _i2c.write(_address);
+ _i2c.write(subaddress);
+
+ _i2c.start();
+ _i2c.write(_address | 1);
+ result = _i2c.read(0);
+
+ _i2c.stop();
+
+ return result;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LPS331_I2C.h Sun Oct 20 15:22:55 2013 +0000
@@ -0,0 +1,87 @@
+/*
+ * I2C/SPI digital pressure sensor "LPS331AP" library for I2C mode.
+ *
+ * http://www.st.com/web/en/resource/technical/document/datasheet/DM00036196.pdf
+ *
+ * Copyright(c) -2013 unos@NYAMFG,
+ * Released under the MIT License: http://mbed.org/license/mit
+ *
+ * revision 1.0 20-Oct-2013 1st release, Does not support interrupts.
+ */
+
+#ifndef LPS331_I2C_H
+#define LPS331_I2C_H
+
+#include "mbed.h"
+
+// SA0 status configuration values.
+#define LPS331_I2C_SA0_HIGH true
+#define LPS331_I2C_SA0_LOW false
+
+// Pressure configuration values.
+#define LPS331_I2C_PRESSURE_AVG_1 0x00
+#define LPS331_I2C_PRESSURE_AVG_2 0x01
+#define LPS331_I2C_PRESSURE_AVG_4 0x02
+#define LPS331_I2C_PRESSURE_AVG_8 0x03
+#define LPS331_I2C_PRESSURE_AVG_16 0x04
+#define LPS331_I2C_PRESSURE_AVG_32 0x05
+#define LPS331_I2C_PRESSURE_AVG_64 0x06
+#define LPS331_I2C_PRESSURE_AVG_128 0x07
+#define LPS331_I2C_PRESSURE_AVG_256 0x08
+#define LPS331_I2C_PRESSURE_AVG_384 0x09
+#define LPS331_I2C_PRESSURE_AVG_512 0x0a
+
+// Temperature configuration values.
+#define LPS331_I2C_TEMP_AVG_1 0x00
+#define LPS331_I2C_TEMP_AVG_2 0x01
+#define LPS331_I2C_TEMP_AVG_4 0x02
+#define LPS331_I2C_TEMP_AVG_8 0x03
+#define LPS331_I2C_TEMP_AVG_16 0x04
+#define LPS331_I2C_TEMP_AVG_32 0x05
+#define LPS331_I2C_TEMP_AVG_64 0x06
+#define LPS331_I2C_TEMP_AVG_128 0x07
+
+// Data Rate Pressure / Temperature
+#define LPS331_I2C_DATARATE_ONESHOT 0x00 // OneShot OneShot
+#define LPS331_I2C_DATARATE_1HZ 0x01 // 1Hz 1Hz
+#define LPS331_I2C_DATARATE_7HZ 0x02 // 7Hz 1Hz
+#define LPS331_I2C_DATARATE_12_5HZ 0x03 // 12.5Hz 1Hz
+#define LPS331_I2C_DATARATE_25HZ 0x04 // 25Hz 1Hz
+#define LPS331_I2C_DATARATE_7HZ_T 0x05 // 7Hz 7Hz
+#define LPS331_I2C_DATARATE_12_5HZ_T 0x06 // 12.5Hz 12.5Hz
+#define LPS331_I2C_DATARATE_25HZ_T 0x07 // 25Hz 25Hz (*)
+// (*) Not allowed with PRESSURE_AVG_512 & TEMP_AVG_128.
+// More information , see datasheet.
+
+// I2C Address.
+#define LPS331_I2C_ADDRESS_SA0_HIGH 0xba
+#define LPS331_I2C_ADDRESS_SA0_LOW 0xb8
+
+class LPS331_I2C
+{
+public:
+ LPS331_I2C(PinName sda, PinName scl, bool sa0);
+ ~LPS331_I2C();
+
+ char whoami();
+ bool isAvailable();
+
+ void setResolution(char pressure_avg, char temp_avg);
+ void setActive(bool is_active);
+ void setDataRate(char datarate);
+
+ float getPressure();
+ float getTemperature();
+
+ void _write(char subaddress, char data);
+ char _read(char subaddress);
+
+private:
+ I2C _i2c;
+ char _address;
+ char _ctrlreg1;
+};
+
+
+#endif /* LPC331_I2C_H */
+
LPS331 Pressure sensor