Mbed Pololu LPS331 SPI library ouputs for Air Pessure & Temperature in unsigned values calculation to be done by user (though notes on calculations can be found within)
Revision 2:77cb7992a1a3, committed 2014-07-24
- Comitter:
- jbeatch
- Date:
- Thu Jul 24 14:53:59 2014 +0000
- Parent:
- 1:a1e0d54a6be3
- Commit message:
- added functions to output Temp [C], Temp [F], and Pressure [mbar]
Changed in this revision
diff -r a1e0d54a6be3 -r 77cb7992a1a3 LPS331.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPS331.cpp Thu Jul 24 14:53:59 2014 +0000 @@ -0,0 +1,135 @@ + +#include <mbed.h> +#include "LPS331.h" + +#define STATUS_REG 0x27 +#define MULTI_BYTE 0x40 +#define REF_P_XL 0x08 +#define REF_P_L 0x09 +#define REF_P_H 0x0A +#define WHO_AM_I 0x0F +#define RES_CONF 0x10 +#define CTRL_REG1 0x20 +#define PRESS_POUT_XL_REH 0x28 +#define PRESS_OUT_L 0x29 +#define PRESS_OUT_H 0x2A +#define TEMP_OUT_L 0x2B +#define TEMP_OUT_H 0x2C + +#define WRITE 0x00 +#define READ 0x80 + +#define pSensor 0x02 +#define tSensor 0x01 +#define bothSensors 0x03 + +lps331::lps331(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) { +} + +uint16_t lps331::read_temperature() { + // check if updated value is available + if ( ready(tSensor) ) + { + select(); + // do multi byte read + spi.write(READ | MULTI_BYTE | TEMP_OUT_L); + uint8_t tempL = spi.write(READ); + uint8_t tempH = spi.write(READ); + deselect(); + + uint16_t temperature = (tempH<<8) | tempL; + // t[C] = 42.5 + temperature /480.0 + //float temp = (float)(42.5 + ((short)temperature*1.0/480.0)); + return temperature; + } + return 0; +} + +uint32_t lps331::read_pressure() { + // check if updated value is available + if ( ready(pSensor) ) + { + select(); + // do multi byte read + spi.write(READ | MULTI_BYTE | PRESS_POUT_XL_REH); + uint8_t pressureLX = spi.write(READ); + uint8_t pressureL = spi.write(READ); + uint8_t pressureH = spi.write(READ); + deselect(); + + uint32_t pressure = (pressureH<<16) | (pressureL<<8) | pressureLX; + // P[mBar] = pressure/4096 + // float P = pressure*1.0/4096.0; + return pressure; + } + return 0; +} + +float lps331::get_Pressure_mbar() +{ + uint32_t airP = read_pressure(); + float pressure = airP*1.0/4096.0; + return pressure; +} + +float lps331::get_Temp_C() +{ + short airT = read_temperature(); + float temp = (float)(42.5 + (airT*1.0/480.0)); + return temp; +} + +float lps331::get_Temp_F() +{ + short airT = read_temperature(); + float temp = (float)(108.5 + (airT*1.8/480.0)); + return temp; +} + +void lps331::select() { + //Set CS low to start transmission (interrupts conversion) + ncs = 0; +} + +void lps331::deselect() { + //Set CS high to stop transmission (restarts conversion) + ncs = 1; +} + +bool lps331::initialize(int setType) { + deselect(); + spi.format(8,0); + spi.frequency(400000); + + // set chip to update pressure and temp at 25Hz + writeRegister(CTRL_REG1, 0xE0); + wait_ms(1); + + // check board is responding + int whoami = readRegister(WHO_AM_I); + if (whoami == 0xBB) // addresss of chip + return true; + return false; +} + +int lps331::ready(uint8_t sensor) { + uint8_t status = readRegister(STATUS_REG); + if( (status & sensor)) + return 1; + return 0; +} + +uint8_t lps331::readRegister(uint8_t address){ + select(); + spi.write(READ | address); + uint8_t val = spi.write(READ); + deselect(); + return val; + } + +void lps331::writeRegister(uint8_t address, uint8_t value){ + select(); + spi.write(WRITE | address); + spi.write(value); + deselect(); +} \ No newline at end of file
diff -r a1e0d54a6be3 -r 77cb7992a1a3 LPS331.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPS331.h Thu Jul 24 14:53:59 2014 +0000 @@ -0,0 +1,34 @@ +#ifndef LPS331AP_h +#define LPS331AP_h + +#include "mbed.h" + +class lps331 +{ + SPI& spi; + DigitalOut ncs; + public: + + lps331(SPI& _spi, PinName _ncs); + void select(); + void deselect(); + bool initialize(int setType=0); + + int ready(uint8_t sensor); + //float read_pressure(); + uint32_t read_pressure(); + uint16_t read_temperature(); + float get_Pressure_mbar(); + float get_Temp_C(); + float get_Temp_F(); + private: + PinName _CS_pin; + PinName _SO_pin; + PinName _SCK_pin; + uint8_t readRegister(uint8_t address); + void writeRegister(uint8_t address, uint8_t value); + int _units; + float _error; +}; + +#endif
diff -r a1e0d54a6be3 -r 77cb7992a1a3 lps331.cpp --- a/lps331.cpp Wed Jul 23 22:30:35 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,114 +0,0 @@ - -#include <mbed.h> -#include "lps331.h" - -#define STATUS_REG 0x27 -#define MULTI_BYTE 0x40 -#define REF_P_XL 0x08 -#define REF_P_L 0x09 -#define REF_P_H 0x0A -#define WHO_AM_I 0x0F -#define RES_CONF 0x10 -#define CTRL_REG1 0x20 -#define PRESS_POUT_XL_REH 0x28 -#define PRESS_OUT_L 0x29 -#define PRESS_OUT_H 0x2A -#define TEMP_OUT_L 0x2B -#define TEMP_OUT_H 0x2C - -#define WRITE 0x00 -#define READ 0x80 - -#define pSensor 0x02 -#define tSensor 0x01 -#define bothSensors 0x03 - -lps331::lps331(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) { -} - -uint16_t lps331::read_temperature() { - // check if updated value is available - if ( ready(tSensor) ) - { - //select(); - // do multi byte read - //spi.write(READ | MULTI_BYTE | TEMP_OUT_L); - uint8_t tempL = readRegister(TEMP_OUT_L); - uint8_t tempH = readRegister(TEMP_OUT_H); - //deselect(); - - uint16_t temperature = (tempH<<8) | tempL; - // t[C] = 42.5 + temperature /480.0 - //float temp = (float)(42.5 + ((short)temperature*1.0/480.0)); - return temperature; - } - return 0; -} - -uint32_t lps331::read_pressure() { - // check if updated value is available - if ( ready(pSensor) ) - { - select(); - // do multi byte read - spi.write(READ | MULTI_BYTE | PRESS_POUT_XL_REH); - uint8_t pressureLX = spi.write(READ); - uint8_t pressureL = spi.write(READ); - uint8_t pressureH = spi.write(READ); - deselect(); - - uint32_t pressure = (pressureH<<16) | (pressureL<<8) | pressureLX; - // P[mBar] = pressure/4096 - // float P = pressure*1.0/4096.0; - return pressure; - } - return 0; -} - -void lps331::select() { - //Set CS low to start transmission (interrupts conversion) - ncs = 0; -} - -void lps331::deselect() { - //Set CS high to stop transmission (restarts conversion) - ncs = 1; -} - -bool lps331::initialize(int setType) { - deselect(); - spi.format(8,0); - spi.frequency(400000); - - // set chip to update pressure and temp at 25Hz - writeRegister(CTRL_REG1, 0xE0); - wait_ms(1); - - // check board is responding - int whoami = readRegister(WHO_AM_I); - if (whoami == 0xBB) // addresss of chip - return true; - return false; -} - -int lps331::ready(uint8_t sensor) { - uint8_t status = readRegister(STATUS_REG); - if( (status & sensor)) - return 1; - return 0; -} - -uint8_t lps331::readRegister(uint8_t address){ - select(); - spi.write(READ | address); - uint8_t val = spi.write(READ); - deselect(); - return val; - } - -void lps331::writeRegister(uint8_t address, uint8_t value){ - select(); - spi.write(WRITE | address); - spi.write(value); - deselect(); -} \ No newline at end of file
diff -r a1e0d54a6be3 -r 77cb7992a1a3 lps331.h --- a/lps331.h Wed Jul 23 22:30:35 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#ifndef LPS331AP_h -#define LPS331AP_h - -#include "mbed.h" - -class lps331 -{ - SPI& spi; - DigitalOut ncs; - public: - - lps331(SPI& _spi, PinName _ncs); - void select(); - void deselect(); - bool initialize(int setType=0); - - int ready(uint8_t sensor); - //float read_pressure(); - uint32_t read_pressure(); - uint16_t read_temperature(); - private: - PinName _CS_pin; - PinName _SO_pin; - PinName _SCK_pin; - uint8_t readRegister(uint8_t address); - void writeRegister(uint8_t address, uint8_t value); - int _units; - float _error; -}; - -#endif