STMicroelectronics LPS331 I2C Library.

Dependents:   LPS331_HelloWorld mbed_vfd_thermometer

Files at this revision

API Documentation at this revision

Comitter:
nyamfg
Date:
Mon Oct 21 16:39:58 2013 +0000
Parent:
0:3fd57444bc65
Commit message:
I2C performance improvements.

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
--- a/LPS331_I2C.cpp	Sun Oct 20 15:22:55 2013 +0000
+++ b/LPS331_I2C.cpp	Mon Oct 21 16:39:58 2013 +0000
@@ -65,10 +65,14 @@
     
 float LPS331_I2C::getPressure()
 {
+    char data[3];
     float pressure = 0;
-    pressure += _read(0x28);
-    pressure += _read(0x29) << 8;
-    pressure += _read(0x2a) << 16;
+
+    _read_multibyte(0x28, data, 3);
+    
+    pressure  = data[0];
+    pressure += data[1] << 8;
+    pressure += data[2] << 16;
     pressure /= 4096.0;
     
     return pressure;
@@ -76,9 +80,13 @@
 
 float LPS331_I2C::getTemperature()
 {
+    char data[2];
     short temp = 0;
-    temp = _read(0x2b);
-    temp |= _read(0x2c) << 8;
+    
+    _read_multibyte(0x2b, data, 2);    
+    
+    temp  = data[0];
+    temp |= data[1] << 8;
     
     return (float)(42.5 + temp / 480.0);
 }
@@ -110,3 +118,19 @@
     return result;
 }
 
+void LPS331_I2C::_read_multibyte(char startsubaddress, char* data, char count)
+{
+    _i2c.start();
+    _i2c.write(_address);
+    _i2c.write(startsubaddress | 0x80);
+    
+    _i2c.start();
+    _i2c.write(_address | 1);
+
+    for(int i = 0; i < count; i++) {
+        data[i] = _i2c.read((i == count - 1) ? 0 : 1);
+    }
+    
+    _i2c.stop();
+}
+
--- a/LPS331_I2C.h	Sun Oct 20 15:22:55 2013 +0000
+++ b/LPS331_I2C.h	Mon Oct 21 16:39:58 2013 +0000
@@ -6,6 +6,7 @@
  *  Copyright(c) -2013 unos@NYAMFG, 
  *  Released under the MIT License: http://mbed.org/license/mit
  *
+ *  revision 1.1    22-Oct-2013     Add multibyte read, Change temperature and pressure reading method.
  *  revision 1.0    20-Oct-2013     1st release, Does not support interrupts.
  */
 
@@ -75,6 +76,7 @@
 
     void _write(char subaddress, char data);
     char _read(char subaddress);
+    void _read_multibyte(char startsubaddress, char* data, char count);
         
 private:
     I2C _i2c;