STMicroelectronics LPS331 I2C Library.
Dependents: LPS331_HelloWorld mbed_vfd_thermometer lon-lof
LPS331_I2C.cpp@0:3fd57444bc65, 2013-10-20 (annotated)
- Committer:
- nyamfg
- Date:
- Sun Oct 20 15:22:55 2013 +0000
- Revision:
- 0:3fd57444bc65
- Child:
- 1:b7d3d6e82049
1st release.
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:3fd57444bc65 | 68 | float pressure = 0; |
nyamfg | 0:3fd57444bc65 | 69 | pressure += _read(0x28); |
nyamfg | 0:3fd57444bc65 | 70 | pressure += _read(0x29) << 8; |
nyamfg | 0:3fd57444bc65 | 71 | pressure += _read(0x2a) << 16; |
nyamfg | 0:3fd57444bc65 | 72 | pressure /= 4096.0; |
nyamfg | 0:3fd57444bc65 | 73 | |
nyamfg | 0:3fd57444bc65 | 74 | return pressure; |
nyamfg | 0:3fd57444bc65 | 75 | } |
nyamfg | 0:3fd57444bc65 | 76 | |
nyamfg | 0:3fd57444bc65 | 77 | float LPS331_I2C::getTemperature() |
nyamfg | 0:3fd57444bc65 | 78 | { |
nyamfg | 0:3fd57444bc65 | 79 | short temp = 0; |
nyamfg | 0:3fd57444bc65 | 80 | temp = _read(0x2b); |
nyamfg | 0:3fd57444bc65 | 81 | temp |= _read(0x2c) << 8; |
nyamfg | 0:3fd57444bc65 | 82 | |
nyamfg | 0:3fd57444bc65 | 83 | return (float)(42.5 + temp / 480.0); |
nyamfg | 0:3fd57444bc65 | 84 | } |
nyamfg | 0:3fd57444bc65 | 85 | |
nyamfg | 0:3fd57444bc65 | 86 | |
nyamfg | 0:3fd57444bc65 | 87 | void LPS331_I2C::_write(char subaddress, char data) |
nyamfg | 0:3fd57444bc65 | 88 | { |
nyamfg | 0:3fd57444bc65 | 89 | _i2c.start(); |
nyamfg | 0:3fd57444bc65 | 90 | _i2c.write(_address); |
nyamfg | 0:3fd57444bc65 | 91 | _i2c.write(subaddress); |
nyamfg | 0:3fd57444bc65 | 92 | _i2c.write(data); |
nyamfg | 0:3fd57444bc65 | 93 | _i2c.stop(); |
nyamfg | 0:3fd57444bc65 | 94 | } |
nyamfg | 0:3fd57444bc65 | 95 | |
nyamfg | 0:3fd57444bc65 | 96 | char LPS331_I2C::_read(char subaddress) |
nyamfg | 0:3fd57444bc65 | 97 | { |
nyamfg | 0:3fd57444bc65 | 98 | char result = 0; |
nyamfg | 0:3fd57444bc65 | 99 | |
nyamfg | 0:3fd57444bc65 | 100 | _i2c.start(); |
nyamfg | 0:3fd57444bc65 | 101 | _i2c.write(_address); |
nyamfg | 0:3fd57444bc65 | 102 | _i2c.write(subaddress); |
nyamfg | 0:3fd57444bc65 | 103 | |
nyamfg | 0:3fd57444bc65 | 104 | _i2c.start(); |
nyamfg | 0:3fd57444bc65 | 105 | _i2c.write(_address | 1); |
nyamfg | 0:3fd57444bc65 | 106 | result = _i2c.read(0); |
nyamfg | 0:3fd57444bc65 | 107 | |
nyamfg | 0:3fd57444bc65 | 108 | _i2c.stop(); |
nyamfg | 0:3fd57444bc65 | 109 | |
nyamfg | 0:3fd57444bc65 | 110 | return result; |
nyamfg | 0:3fd57444bc65 | 111 | } |
nyamfg | 0:3fd57444bc65 | 112 |