read pressure and temperature from LPS25h.

Dependencies:   mbed

Committer:
onaka
Date:
Mon Apr 13 12:54:45 2015 +0000
Revision:
2:0c2bb6fe6885
Child:
3:844fb47ba7a2
LPS25H library to Folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
onaka 2:0c2bb6fe6885 1 //**********************
onaka 2:0c2bb6fe6885 2 // LPS25H.cpp for mbed
onaka 2:0c2bb6fe6885 3 //
onaka 2:0c2bb6fe6885 4 // LPS25H lps25h(P0_5,P0_4);
onaka 2:0c2bb6fe6885 5 // or
onaka 2:0c2bb6fe6885 6 // I2C i2c(P0_5,P0_4);
onaka 2:0c2bb6fe6885 7 // LPS25H lps25h(i2c);
onaka 2:0c2bb6fe6885 8 //
onaka 2:0c2bb6fe6885 9 // (C)Copyright 2014 All rights reserved by Y.Onodera
onaka 2:0c2bb6fe6885 10 // http://einstlab.web.fc2.com
onaka 2:0c2bb6fe6885 11 //**********************
onaka 2:0c2bb6fe6885 12
onaka 2:0c2bb6fe6885 13 #include "mbed.h"
onaka 2:0c2bb6fe6885 14 #include "LPS25H.h"
onaka 2:0c2bb6fe6885 15
onaka 2:0c2bb6fe6885 16 LPS25H::LPS25H (PinName sda, PinName scl) : _i2c(sda, scl) {
onaka 2:0c2bb6fe6885 17 init();
onaka 2:0c2bb6fe6885 18 }
onaka 2:0c2bb6fe6885 19 LPS25H::LPS25H (I2C& p_i2c) : _i2c(p_i2c) {
onaka 2:0c2bb6fe6885 20 init();
onaka 2:0c2bb6fe6885 21 }
onaka 2:0c2bb6fe6885 22
onaka 2:0c2bb6fe6885 23 void LPS25H::put(unsigned char a, unsigned char b)
onaka 2:0c2bb6fe6885 24 {
onaka 2:0c2bb6fe6885 25 buf[0]=a;
onaka 2:0c2bb6fe6885 26 buf[1]=b;
onaka 2:0c2bb6fe6885 27 _i2c.write(LPS25H_ADDR, buf, 2);
onaka 2:0c2bb6fe6885 28 }
onaka 2:0c2bb6fe6885 29
onaka 2:0c2bb6fe6885 30
onaka 2:0c2bb6fe6885 31 void LPS25H::get(unsigned char a)
onaka 2:0c2bb6fe6885 32 {
onaka 2:0c2bb6fe6885 33 buf[0] = a;
onaka 2:0c2bb6fe6885 34 _i2c.write(LPS25H_ADDR, buf, 1, true); // no stop, repeated
onaka 2:0c2bb6fe6885 35 _i2c.read( LPS25H_ADDR, buf, 1);
onaka 2:0c2bb6fe6885 36
onaka 2:0c2bb6fe6885 37 }
onaka 2:0c2bb6fe6885 38
onaka 2:0c2bb6fe6885 39 long LPS25H::pressure()
onaka 2:0c2bb6fe6885 40 {
onaka 2:0c2bb6fe6885 41
onaka 2:0c2bb6fe6885 42 // XL first and H last
onaka 2:0c2bb6fe6885 43 // get press_xl
onaka 2:0c2bb6fe6885 44 get(LPS25H_PRESS_OUT_XL);
onaka 2:0c2bb6fe6885 45 press.byte.LB=buf[0];
onaka 2:0c2bb6fe6885 46 // get tpress_low
onaka 2:0c2bb6fe6885 47 get(LPS25H_PRESS_OUT_L);
onaka 2:0c2bb6fe6885 48 press.byte.HB=buf[0];
onaka 2:0c2bb6fe6885 49 // get press_high
onaka 2:0c2bb6fe6885 50 get(LPS25H_PRESS_OUT_H);
onaka 2:0c2bb6fe6885 51 press.byte.UB=buf[0];
onaka 2:0c2bb6fe6885 52 return press.Val;
onaka 2:0c2bb6fe6885 53
onaka 2:0c2bb6fe6885 54 // hPa = press.Val / 4096
onaka 2:0c2bb6fe6885 55 // Pa = press.Val / 40.96
onaka 2:0c2bb6fe6885 56 }
onaka 2:0c2bb6fe6885 57
onaka 2:0c2bb6fe6885 58
onaka 2:0c2bb6fe6885 59 short LPS25H::temperature()
onaka 2:0c2bb6fe6885 60 {
onaka 2:0c2bb6fe6885 61
onaka 2:0c2bb6fe6885 62 // L first and H last
onaka 2:0c2bb6fe6885 63 // get tpress_low
onaka 2:0c2bb6fe6885 64 get(LPS25H_TEMP_OUT_L);
onaka 2:0c2bb6fe6885 65 temp.byte.LB=buf[0];
onaka 2:0c2bb6fe6885 66 // get press_high
onaka 2:0c2bb6fe6885 67 get(LPS25H_TEMP_OUT_H);
onaka 2:0c2bb6fe6885 68 temp.byte.HB=buf[0];
onaka 2:0c2bb6fe6885 69 return temp.S;
onaka 2:0c2bb6fe6885 70
onaka 2:0c2bb6fe6885 71 // C = 42.5 + temp.S / 480
onaka 2:0c2bb6fe6885 72 // range:0 to 80C
onaka 2:0c2bb6fe6885 73 // accuracy:+-2C
onaka 2:0c2bb6fe6885 74 }
onaka 2:0c2bb6fe6885 75
onaka 2:0c2bb6fe6885 76
onaka 2:0c2bb6fe6885 77 void LPS25H::init()
onaka 2:0c2bb6fe6885 78 {
onaka 2:0c2bb6fe6885 79 // Resolution config
onaka 2:0c2bb6fe6885 80 put(LPS25H_RES_CONF, 0x05);
onaka 2:0c2bb6fe6885 81 // FIFO 4samples moving average
onaka 2:0c2bb6fe6885 82 put(LPS25H_FIFO_CTRL, 0xC3);
onaka 2:0c2bb6fe6885 83 // FIFO Enable
onaka 2:0c2bb6fe6885 84 put(LPS25H_CTRL_REG2, 0x40);
onaka 2:0c2bb6fe6885 85 // Power ON Cycle=12.5Hz
onaka 2:0c2bb6fe6885 86 put(LPS25H_CTRL_REG1, 0xB0);
onaka 2:0c2bb6fe6885 87 }
onaka 2:0c2bb6fe6885 88
onaka 2:0c2bb6fe6885 89