Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
LPS331.cpp
00001 00002 #include <mbed.h> 00003 #include "LPS331.h" 00004 00005 #define STATUS_REG 0x27 00006 #define MULTI_BYTE 0x40 00007 #define REF_P_XL 0x08 00008 #define REF_P_L 0x09 00009 #define REF_P_H 0x0A 00010 #define WHO_AM_I 0x0F 00011 #define RES_CONF 0x10 00012 #define CTRL_REG1 0x20 00013 #define PRESS_POUT_XL_REH 0x28 00014 #define PRESS_OUT_L 0x29 00015 #define PRESS_OUT_H 0x2A 00016 #define TEMP_OUT_L 0x2B 00017 #define TEMP_OUT_H 0x2C 00018 00019 #define WRITE 0x00 00020 #define READ 0x80 00021 00022 #define pSensor 0x02 00023 #define tSensor 0x01 00024 #define bothSensors 0x03 00025 00026 lps331::lps331(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) { 00027 } 00028 00029 uint16_t lps331::read_temperature() { 00030 // check if updated value is available 00031 if ( ready(tSensor) ) 00032 { 00033 select(); 00034 // do multi byte read 00035 spi.write(READ | MULTI_BYTE | TEMP_OUT_L); 00036 uint8_t tempL = spi.write(READ); 00037 uint8_t tempH = spi.write(READ); 00038 deselect(); 00039 00040 uint16_t temperature = (tempH<<8) | tempL; 00041 // t[C] = 42.5 + temperature /480.0 00042 //float temp = (float)(42.5 + ((short)temperature*1.0/480.0)); 00043 return temperature; 00044 } 00045 return 0; 00046 } 00047 00048 uint32_t lps331::read_pressure() { 00049 // check if updated value is available 00050 if ( ready(pSensor) ) 00051 { 00052 select(); 00053 // do multi byte read 00054 spi.write(READ | MULTI_BYTE | PRESS_POUT_XL_REH); 00055 uint8_t pressureLX = spi.write(READ); 00056 uint8_t pressureL = spi.write(READ); 00057 uint8_t pressureH = spi.write(READ); 00058 deselect(); 00059 00060 uint32_t pressure = (pressureH<<16) | (pressureL<<8) | pressureLX; 00061 // P[mBar] = pressure/4096 00062 // float P = pressure*1.0/4096.0; 00063 return pressure; 00064 } 00065 return 0; 00066 } 00067 00068 float lps331::get_Pressure_mbar() 00069 { 00070 uint32_t airP = read_pressure(); 00071 float pressure = airP*1.0/4096.0; 00072 return pressure; 00073 } 00074 00075 float lps331::get_Temp_C() 00076 { 00077 short airT = read_temperature(); 00078 float temp = (float)(42.5 + (airT*1.0/480.0)); 00079 return temp; 00080 } 00081 00082 float lps331::get_Temp_F() 00083 { 00084 short airT = read_temperature(); 00085 float temp = (float)(108.5 + (airT*1.8/480.0)); 00086 return temp; 00087 } 00088 00089 void lps331::select() { 00090 //Set CS low to start transmission (interrupts conversion) 00091 ncs = 0; 00092 } 00093 00094 void lps331::deselect() { 00095 //Set CS high to stop transmission (restarts conversion) 00096 ncs = 1; 00097 } 00098 00099 bool lps331::initialize(int setType) { 00100 deselect(); 00101 spi.format(8,0); 00102 spi.frequency(400000); 00103 00104 // set chip to update pressure and temp at 25Hz 00105 writeRegister(CTRL_REG1, 0xE0); 00106 wait_ms(1); 00107 00108 // check board is responding 00109 int whoami = readRegister(WHO_AM_I); 00110 if (whoami == 0xBB) // addresss of chip 00111 return true; 00112 return false; 00113 } 00114 00115 int lps331::ready(uint8_t sensor) { 00116 uint8_t status = readRegister(STATUS_REG); 00117 if( (status & sensor)) 00118 return 1; 00119 return 0; 00120 } 00121 00122 uint8_t lps331::readRegister(uint8_t address){ 00123 select(); 00124 spi.write(READ | address); 00125 uint8_t val = spi.write(READ); 00126 deselect(); 00127 return val; 00128 } 00129 00130 void lps331::writeRegister(uint8_t address, uint8_t value){ 00131 select(); 00132 spi.write(WRITE | address); 00133 spi.write(value); 00134 deselect(); 00135 }
Generated on Thu Jul 14 2022 09:48:01 by
1.7.2