BM1383GLV pressure sensor library
Dependents: BM1383GLV_Hello LazuriteGraph_Hello GR-PEACH_IoT_Platform_HTTP_sample
BM1383GLV.cpp@1:6e1c9edabeee, 2016-03-23 (annotated)
- Committer:
- MACRUM
- Date:
- Wed Mar 23 03:29:04 2016 +0000
- Revision:
- 1:6e1c9edabeee
- Parent:
- 0:b240c59ffaff
Fixed default slave address definition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:b240c59ffaff | 1 | /* Copyright (c) 2015 ARM Ltd., MIT License |
MACRUM | 0:b240c59ffaff | 2 | * |
MACRUM | 0:b240c59ffaff | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
MACRUM | 0:b240c59ffaff | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
MACRUM | 0:b240c59ffaff | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
MACRUM | 0:b240c59ffaff | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
MACRUM | 0:b240c59ffaff | 7 | * Software is furnished to do so, subject to the following conditions: |
MACRUM | 0:b240c59ffaff | 8 | * |
MACRUM | 0:b240c59ffaff | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
MACRUM | 0:b240c59ffaff | 10 | * substantial portions of the Software. |
MACRUM | 0:b240c59ffaff | 11 | * |
MACRUM | 0:b240c59ffaff | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
MACRUM | 0:b240c59ffaff | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
MACRUM | 0:b240c59ffaff | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
MACRUM | 0:b240c59ffaff | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
MACRUM | 0:b240c59ffaff | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
MACRUM | 0:b240c59ffaff | 17 | * |
MACRUM | 0:b240c59ffaff | 18 | * |
MACRUM | 0:b240c59ffaff | 19 | * BM1383GLV Pressure sensor library |
MACRUM | 0:b240c59ffaff | 20 | * |
MACRUM | 0:b240c59ffaff | 21 | * @author Toyomasa Watarai |
MACRUM | 0:b240c59ffaff | 22 | * @version 1.0 |
MACRUM | 0:b240c59ffaff | 23 | * @date 30-December-2015 |
MACRUM | 0:b240c59ffaff | 24 | * |
MACRUM | 0:b240c59ffaff | 25 | * Library for "BM1383GLV Pressure sensor library" |
MACRUM | 0:b240c59ffaff | 26 | * http://www.rohm.com/web/eu/news-detail?news-title=2014-12-11_ad_pressure&defaultGroupId=false |
MACRUM | 0:b240c59ffaff | 27 | * |
MACRUM | 0:b240c59ffaff | 28 | */ |
MACRUM | 0:b240c59ffaff | 29 | |
MACRUM | 0:b240c59ffaff | 30 | #include "mbed.h" |
MACRUM | 0:b240c59ffaff | 31 | #include "BM1383GLV.h" |
MACRUM | 0:b240c59ffaff | 32 | |
MACRUM | 0:b240c59ffaff | 33 | BM1383GLV::BM1383GLV(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) |
MACRUM | 0:b240c59ffaff | 34 | { |
MACRUM | 0:b240c59ffaff | 35 | initialize(); |
MACRUM | 0:b240c59ffaff | 36 | } |
MACRUM | 0:b240c59ffaff | 37 | |
MACRUM | 0:b240c59ffaff | 38 | BM1383GLV::BM1383GLV(I2C &i2c_obj, int addr) : m_i2c(i2c_obj), m_addr(addr) |
MACRUM | 0:b240c59ffaff | 39 | { |
MACRUM | 0:b240c59ffaff | 40 | initialize(); |
MACRUM | 0:b240c59ffaff | 41 | } |
MACRUM | 0:b240c59ffaff | 42 | |
MACRUM | 0:b240c59ffaff | 43 | BM1383GLV::~BM1383GLV() |
MACRUM | 0:b240c59ffaff | 44 | { |
MACRUM | 0:b240c59ffaff | 45 | } |
MACRUM | 0:b240c59ffaff | 46 | |
MACRUM | 0:b240c59ffaff | 47 | void BM1383GLV::initialize() |
MACRUM | 0:b240c59ffaff | 48 | { |
MACRUM | 0:b240c59ffaff | 49 | unsigned char buf; |
MACRUM | 0:b240c59ffaff | 50 | unsigned char reg[2]; |
MACRUM | 0:b240c59ffaff | 51 | |
MACRUM | 0:b240c59ffaff | 52 | readRegs(BM1383GLV_ID, &buf, sizeof(buf)); |
MACRUM | 0:b240c59ffaff | 53 | if (buf != BM1383GLV_ID_VAL) { |
MACRUM | 0:b240c59ffaff | 54 | DEBUG_PRINT("BM1383GLV initialization error. (%d)\n", buf); |
MACRUM | 0:b240c59ffaff | 55 | return; |
MACRUM | 0:b240c59ffaff | 56 | } |
MACRUM | 0:b240c59ffaff | 57 | |
MACRUM | 0:b240c59ffaff | 58 | reg[0] = BM1383GLV_POWER_DOWN; |
MACRUM | 0:b240c59ffaff | 59 | reg[1] = 0x01; |
MACRUM | 0:b240c59ffaff | 60 | writeRegs(reg, 2); |
MACRUM | 0:b240c59ffaff | 61 | |
MACRUM | 0:b240c59ffaff | 62 | wait(1); |
MACRUM | 0:b240c59ffaff | 63 | |
MACRUM | 0:b240c59ffaff | 64 | reg[0] = BM1383GLV_SLEEP; |
MACRUM | 0:b240c59ffaff | 65 | reg[1] = 0x01; |
MACRUM | 0:b240c59ffaff | 66 | writeRegs(reg, 2); |
MACRUM | 0:b240c59ffaff | 67 | |
MACRUM | 0:b240c59ffaff | 68 | reg[0] = BM1383GLV_MODE_CONTROL; |
MACRUM | 0:b240c59ffaff | 69 | reg[1] = 0xC4; |
MACRUM | 0:b240c59ffaff | 70 | writeRegs(reg, 2); |
MACRUM | 0:b240c59ffaff | 71 | } |
MACRUM | 0:b240c59ffaff | 72 | |
MACRUM | 0:b240c59ffaff | 73 | float BM1383GLV::getPressure() |
MACRUM | 0:b240c59ffaff | 74 | { |
MACRUM | 0:b240c59ffaff | 75 | uint32_t rawPressure; |
MACRUM | 0:b240c59ffaff | 76 | |
MACRUM | 0:b240c59ffaff | 77 | readRegs(BM1383GLV_PRESSURE_MSB, m_buf, 3); |
MACRUM | 0:b240c59ffaff | 78 | rawPressure = (((uint32_t)m_buf[0] << 16) | ((uint32_t)m_buf[1] << 8) | m_buf[2]&0xFC) >> 2; |
MACRUM | 0:b240c59ffaff | 79 | return (float)rawPressure / (2048); |
MACRUM | 0:b240c59ffaff | 80 | } |
MACRUM | 0:b240c59ffaff | 81 | |
MACRUM | 0:b240c59ffaff | 82 | float BM1383GLV::getTemperature() |
MACRUM | 0:b240c59ffaff | 83 | { |
MACRUM | 0:b240c59ffaff | 84 | int32_t rawTemerature; |
MACRUM | 0:b240c59ffaff | 85 | |
MACRUM | 0:b240c59ffaff | 86 | readRegs(BM1383GLV_TEMPERATURE_MSB, m_buf, 2); |
MACRUM | 0:b240c59ffaff | 87 | rawTemerature = ((int32_t)m_buf[0] << 8) | (m_buf[1]); |
MACRUM | 0:b240c59ffaff | 88 | return (float)rawTemerature / 32; |
MACRUM | 0:b240c59ffaff | 89 | } |
MACRUM | 0:b240c59ffaff | 90 | |
MACRUM | 0:b240c59ffaff | 91 | void BM1383GLV::readRegs(int addr, uint8_t * data, int len) |
MACRUM | 0:b240c59ffaff | 92 | { |
MACRUM | 0:b240c59ffaff | 93 | char t[1] = {addr}; |
MACRUM | 0:b240c59ffaff | 94 | m_i2c.write(m_addr, t, 1, true); |
MACRUM | 0:b240c59ffaff | 95 | m_i2c.read(m_addr, (char *)data, len); |
MACRUM | 0:b240c59ffaff | 96 | } |
MACRUM | 0:b240c59ffaff | 97 | |
MACRUM | 0:b240c59ffaff | 98 | void BM1383GLV::writeRegs(uint8_t * data, int len) |
MACRUM | 0:b240c59ffaff | 99 | { |
MACRUM | 0:b240c59ffaff | 100 | m_i2c.write(m_addr, (char *)data, len); |
MACRUM | 0:b240c59ffaff | 101 | } |