-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLPS22HB_Sensor.py
90 lines (80 loc) · 3.63 KB
/
LPS22HB_Sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
# -*- coding:utf-8 -*-
import time
import smbus
#i2c address
LPS22HB_I2C_ADDRESS = 0x5C
#
LPS_ID = 0xB1
#Register
LPS_INT_CFG = 0x0B #Interrupt register
LPS_THS_P_L = 0x0C #Pressure threshold registers
LPS_THS_P_H = 0x0D
LPS_WHO_AM_I = 0x0F #Who am I
LPS_CTRL_REG1 = 0x10 #Control registers
LPS_CTRL_REG2 = 0x11
LPS_CTRL_REG3 = 0x12
LPS_FIFO_CTRL = 0x14 #FIFO configuration register
LPS_REF_P_XL = 0x15 #Reference pressure registers
LPS_REF_P_L = 0x16
LPS_REF_P_H = 0x17
LPS_RPDS_L = 0x18 #Pressure offset registers
LPS_RPDS_H = 0x19
LPS_RES_CONF = 0x1A #Resolution register
LPS_INT_SOURCE = 0x25 #Interrupt register
LPS_FIFO_STATUS = 0x26 #FIFO status register
LPS_STATUS = 0x27 #Status register
LPS_PRESS_OUT_XL = 0x28 #Pressure output registers
LPS_PRESS_OUT_L = 0x29
LPS_PRESS_OUT_H = 0x2A
LPS_TEMP_OUT_L = 0x2B #Temperature output registers
LPS_TEMP_OUT_H = 0x2C
LPS_RES = 0x33 #Filter reset register
class LPS22HB(object):
def __init__(self,address=LPS22HB_I2C_ADDRESS):
print(' ...Initializing "' + str(self) + '"...')
self._address = address
self._bus = smbus.SMBus(1)
self.LPS22HB_RESET() #Wait for reset to complete
self._write_byte(LPS_CTRL_REG1 ,0x02) #Low-pass filter disabled , output registers not updated until MSB and LSB have been read ,
#Enable Block Data Update , Set Output Data Rate to 0
def LPS22HB_RESET(self):
Buf=self._read_u16(LPS_CTRL_REG2)
Buf|=0x04
self._write_byte(LPS_CTRL_REG2,Buf) #SWRESET Set 1
while Buf:
Buf=self._read_u16(LPS_CTRL_REG2)
Buf&=0x04
def LPS22HB_START_ONESHOT(self):
Buf=self._read_u16(LPS_CTRL_REG2)
Buf|=0x01 #ONE_SHOT Set 1
self._write_byte(LPS_CTRL_REG2,Buf)
def _read_byte(self,cmd):
return self._bus.read_byte_data(self._address,cmd)
def _read_u16(self,cmd):
LSB = self._bus.read_byte_data(self._address,cmd)
MSB = self._bus.read_byte_data(self._address,cmd+1)
return (MSB << 8) + LSB
def _write_byte(self,cmd,val):
self._bus.write_byte_data(self._address,cmd,val)
def getSensorReading(self):
#if __name__ == '__main__':
PRESS_DATA = 997.15
TEMP_DATA = 31.86
u8Buf=[0,0,0]
#print("\nPressure Sensor Test Program ...\n")
lps22hb=LPS22HB()
# while True:
# time.sleep(0.1)
lps22hb.LPS22HB_START_ONESHOT()
if (lps22hb._read_byte(LPS_STATUS)&0x01)==0x01: # a new pressure data is generated
u8Buf[0]=lps22hb._read_byte(LPS_PRESS_OUT_XL)
u8Buf[1]=lps22hb._read_byte(LPS_PRESS_OUT_L)
u8Buf[2]=lps22hb._read_byte(LPS_PRESS_OUT_H)
PRESS_DATA=((u8Buf[2]<<16)+(u8Buf[1]<<8)+u8Buf[0])/4096.0
if (lps22hb._read_byte(LPS_STATUS)&0x02)==0x02: # a new pressure data is generated
u8Buf[0]=lps22hb._read_byte(LPS_TEMP_OUT_L)
u8Buf[1]=lps22hb._read_byte(LPS_TEMP_OUT_H)
TEMP_DATA=((u8Buf[1]<<8)+u8Buf[0])/100.0
print('Pressure = %6.2f hPa , Temperature = %6.2f °C\r\n'%(PRESS_DATA,TEMP_DATA))
return { 'Pressure' : '%6.2f hPa'%(PRESS_DATA) , 'Temperature': '%6.2f °C'%(TEMP_DATA) }