STMicroelectronics LPS331 I2C Library.

Dependents:   LPS331_HelloWorld mbed_vfd_thermometer

Committer:
nyamfg
Date:
Mon Oct 21 16:39:58 2013 +0000
Revision:
1:b7d3d6e82049
Parent:
0:3fd57444bc65
I2C performance improvements.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyamfg 0:3fd57444bc65 1 /*
nyamfg 0:3fd57444bc65 2 * I2C/SPI digital pressure sensor "LPS331AP" library for I2C mode.
nyamfg 0:3fd57444bc65 3 *
nyamfg 0:3fd57444bc65 4 * Copyright(c) -2013 unos@NYAMFG,
nyamfg 0:3fd57444bc65 5 * Released under the MIT License: http://mbed.org/license/mit
nyamfg 0:3fd57444bc65 6 *
nyamfg 0:3fd57444bc65 7 * revision: see LPS331_I2C.h.
nyamfg 0:3fd57444bc65 8 */
nyamfg 0:3fd57444bc65 9
nyamfg 0:3fd57444bc65 10 #include "LPS331_I2C.h"
nyamfg 0:3fd57444bc65 11
nyamfg 0:3fd57444bc65 12 LPS331_I2C::LPS331_I2C(PinName sda, PinName scl, bool sa0) : _i2c(sda, scl)
nyamfg 0:3fd57444bc65 13 {
nyamfg 0:3fd57444bc65 14 if(sa0 == LPS331_I2C_SA0_HIGH) {
nyamfg 0:3fd57444bc65 15 _address = LPS331_I2C_ADDRESS_SA0_HIGH;
nyamfg 0:3fd57444bc65 16 } else {
nyamfg 0:3fd57444bc65 17 _address = LPS331_I2C_ADDRESS_SA0_LOW;
nyamfg 0:3fd57444bc65 18 }
nyamfg 0:3fd57444bc65 19 _i2c.frequency(400 * 1000);
nyamfg 0:3fd57444bc65 20 _ctrlreg1 = 0x20;
nyamfg 0:3fd57444bc65 21 }
nyamfg 0:3fd57444bc65 22
nyamfg 0:3fd57444bc65 23 LPS331_I2C::~LPS331_I2C()
nyamfg 0:3fd57444bc65 24 {
nyamfg 0:3fd57444bc65 25 }
nyamfg 0:3fd57444bc65 26
nyamfg 0:3fd57444bc65 27 char LPS331_I2C::whoami()
nyamfg 0:3fd57444bc65 28 {
nyamfg 0:3fd57444bc65 29 return _read(0x0f);
nyamfg 0:3fd57444bc65 30 }
nyamfg 0:3fd57444bc65 31
nyamfg 0:3fd57444bc65 32 bool LPS331_I2C::isAvailable()
nyamfg 0:3fd57444bc65 33 {
nyamfg 0:3fd57444bc65 34 if(whoami() == 0xbb) { return true; }
nyamfg 0:3fd57444bc65 35
nyamfg 0:3fd57444bc65 36 return false;
nyamfg 0:3fd57444bc65 37 }
nyamfg 0:3fd57444bc65 38
nyamfg 0:3fd57444bc65 39 void LPS331_I2C::setResolution(char pressure_avg, char temp_avg)
nyamfg 0:3fd57444bc65 40 {
nyamfg 0:3fd57444bc65 41 _write(0x10, ((temp_avg & 0x07) << 4) | (pressure_avg & 0x0f));
nyamfg 0:3fd57444bc65 42 }
nyamfg 0:3fd57444bc65 43
nyamfg 0:3fd57444bc65 44 void LPS331_I2C::setActive(bool is_active)
nyamfg 0:3fd57444bc65 45 {
nyamfg 0:3fd57444bc65 46 if(is_active) {
nyamfg 0:3fd57444bc65 47 _ctrlreg1 |= 0x80;
nyamfg 0:3fd57444bc65 48 } else {
nyamfg 0:3fd57444bc65 49 _ctrlreg1 &= ~0x80;
nyamfg 0:3fd57444bc65 50 }
nyamfg 0:3fd57444bc65 51
nyamfg 0:3fd57444bc65 52 _write(0x20, _ctrlreg1);
nyamfg 0:3fd57444bc65 53 }
nyamfg 0:3fd57444bc65 54
nyamfg 0:3fd57444bc65 55 void LPS331_I2C::setDataRate(char datarate)
nyamfg 0:3fd57444bc65 56 {
nyamfg 0:3fd57444bc65 57 datarate &= 0x07;
nyamfg 0:3fd57444bc65 58
nyamfg 0:3fd57444bc65 59 _ctrlreg1 &= ~(0x07 << 4);
nyamfg 0:3fd57444bc65 60 _ctrlreg1 |= datarate << 4;
nyamfg 0:3fd57444bc65 61
nyamfg 0:3fd57444bc65 62 _write(0x20, _ctrlreg1);
nyamfg 0:3fd57444bc65 63 }
nyamfg 0:3fd57444bc65 64
nyamfg 0:3fd57444bc65 65
nyamfg 0:3fd57444bc65 66 float LPS331_I2C::getPressure()
nyamfg 0:3fd57444bc65 67 {
nyamfg 1:b7d3d6e82049 68 char data[3];
nyamfg 0:3fd57444bc65 69 float pressure = 0;
nyamfg 1:b7d3d6e82049 70
nyamfg 1:b7d3d6e82049 71 _read_multibyte(0x28, data, 3);
nyamfg 1:b7d3d6e82049 72
nyamfg 1:b7d3d6e82049 73 pressure = data[0];
nyamfg 1:b7d3d6e82049 74 pressure += data[1] << 8;
nyamfg 1:b7d3d6e82049 75 pressure += data[2] << 16;
nyamfg 0:3fd57444bc65 76 pressure /= 4096.0;
nyamfg 0:3fd57444bc65 77
nyamfg 0:3fd57444bc65 78 return pressure;
nyamfg 0:3fd57444bc65 79 }
nyamfg 0:3fd57444bc65 80
nyamfg 0:3fd57444bc65 81 float LPS331_I2C::getTemperature()
nyamfg 0:3fd57444bc65 82 {
nyamfg 1:b7d3d6e82049 83 char data[2];
nyamfg 0:3fd57444bc65 84 short temp = 0;
nyamfg 1:b7d3d6e82049 85
nyamfg 1:b7d3d6e82049 86 _read_multibyte(0x2b, data, 2);
nyamfg 1:b7d3d6e82049 87
nyamfg 1:b7d3d6e82049 88 temp = data[0];
nyamfg 1:b7d3d6e82049 89 temp |= data[1] << 8;
nyamfg 0:3fd57444bc65 90
nyamfg 0:3fd57444bc65 91 return (float)(42.5 + temp / 480.0);
nyamfg 0:3fd57444bc65 92 }
nyamfg 0:3fd57444bc65 93
nyamfg 0:3fd57444bc65 94
nyamfg 0:3fd57444bc65 95 void LPS331_I2C::_write(char subaddress, char data)
nyamfg 0:3fd57444bc65 96 {
nyamfg 0:3fd57444bc65 97 _i2c.start();
nyamfg 0:3fd57444bc65 98 _i2c.write(_address);
nyamfg 0:3fd57444bc65 99 _i2c.write(subaddress);
nyamfg 0:3fd57444bc65 100 _i2c.write(data);
nyamfg 0:3fd57444bc65 101 _i2c.stop();
nyamfg 0:3fd57444bc65 102 }
nyamfg 0:3fd57444bc65 103
nyamfg 0:3fd57444bc65 104 char LPS331_I2C::_read(char subaddress)
nyamfg 0:3fd57444bc65 105 {
nyamfg 0:3fd57444bc65 106 char result = 0;
nyamfg 0:3fd57444bc65 107
nyamfg 0:3fd57444bc65 108 _i2c.start();
nyamfg 0:3fd57444bc65 109 _i2c.write(_address);
nyamfg 0:3fd57444bc65 110 _i2c.write(subaddress);
nyamfg 0:3fd57444bc65 111
nyamfg 0:3fd57444bc65 112 _i2c.start();
nyamfg 0:3fd57444bc65 113 _i2c.write(_address | 1);
nyamfg 0:3fd57444bc65 114 result = _i2c.read(0);
nyamfg 0:3fd57444bc65 115
nyamfg 0:3fd57444bc65 116 _i2c.stop();
nyamfg 0:3fd57444bc65 117
nyamfg 0:3fd57444bc65 118 return result;
nyamfg 0:3fd57444bc65 119 }
nyamfg 0:3fd57444bc65 120
nyamfg 1:b7d3d6e82049 121 void LPS331_I2C::_read_multibyte(char startsubaddress, char* data, char count)
nyamfg 1:b7d3d6e82049 122 {
nyamfg 1:b7d3d6e82049 123 _i2c.start();
nyamfg 1:b7d3d6e82049 124 _i2c.write(_address);
nyamfg 1:b7d3d6e82049 125 _i2c.write(startsubaddress | 0x80);
nyamfg 1:b7d3d6e82049 126
nyamfg 1:b7d3d6e82049 127 _i2c.start();
nyamfg 1:b7d3d6e82049 128 _i2c.write(_address | 1);
nyamfg 1:b7d3d6e82049 129
nyamfg 1:b7d3d6e82049 130 for(int i = 0; i < count; i++) {
nyamfg 1:b7d3d6e82049 131 data[i] = _i2c.read((i == count - 1) ? 0 : 1);
nyamfg 1:b7d3d6e82049 132 }
nyamfg 1:b7d3d6e82049 133
nyamfg 1:b7d3d6e82049 134 _i2c.stop();
nyamfg 1:b7d3d6e82049 135 }
nyamfg 1:b7d3d6e82049 136