本文主要是介绍python 驱动 歌儿声学气压传感器 SPL06系列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、IIC驱动
2、SPL06-001/SPL06-007
3、驱动部分
class SPL06():I2C=I2C(I2c_CLK_PIN,I2c_SDA_PIN)SPL06_SlaveAddr = 0x76# 写单个寄存器def setReg(self, reg, dat):self.I2C.write(self.SPL06_SlaveAddr,reg, dat)# 读取单个寄存器封装def getReg(self, reg):self.rb[0]=0self.rb[0]=self.I2C.read(self.SPL06_SlaveAddr,reg)return self.rb[0]# 读取双寄存器数据def get2Reg(self, reg):return self.getReg(reg) + self.getReg(reg+1) * 256# 传感器初始化def __init__(self):self.rb = bytearray(1)self.C0 = (self.getReg(0x10)<<4)+((self.getReg(0x11)&0xF0)>>4)self.C0 = (0-((0xFFF^self.C0)+1)) if (self.C0 & (1<<11)) else self.C0self.C1 = ((self.getReg(0x11)&0x0F)<<8)+self.getReg(0x12)self.C1 = (0-((0xFFF^self.C1)+1)) if (self.C1 & (1<<11)) else self.C1self.C00 = (self.getReg(0x13)<<12)+(self.getReg(0x14)<<4)+(self.getReg(0x15)>>4)self.C00 = (0-((0xFFFFF^self.C00)+1)) if (self.C00 & (1<<19)) else self.C00self.C10 = ((self.getReg(0x15)&0x0F)<<16)+(self.getReg(0x16)<<8)+self.getReg(0x17)self.C10 = (0-((0xFFFFF^self.C10)+1)) if (self.C10 & (1<<19)) else self.C10self.C01 = (self.getReg(0x18)<<8)+self.getReg(0x19)self.C01 = (0-((0xFFFF^self.C01)+1)) if (self.C01 & (1<<15)) else self.C01self.C11 = (self.getReg(0x1A)<<8)+self.getReg(0x1B)self.C11 = (0-((0xFFFF^self.C11)+1)) if (self.C11 & (1<<15)) else self.C11self.C20 = (self.getReg(0x1C)<<8)+self.getReg(0x1D)self.C20 = (0-((0xFFFF^self.C20)+1)) if (self.C20 & (1<<15)) else self.C20self.C21 = (self.getReg(0x1E)<<8)+self.getReg(0x1F)self.C21 = (0-((0xFFFF^self.C21)+1)) if (self.C21 & (1<<15)) else self.C21self.C30 = (self.getReg(0x20)<<8)+self.getReg(0x21)self.C30 = (0-((0xFFFF^self.C30)+1)) if (self.C30 & (1<<15)) else self.C30self.setReg(0x06,0x25)#气压测量配置 4 measurements 32 timestemp = self.getReg(0x09) #oversampling times>8时必须使用self.setReg(0x09,temp|0x04)self.setReg(0X07,0xa3) #温度测量配置 4 measurements 8 timesself.setReg(0x08,0x07)#测量模式配置 连续测量模式self.T = 0self.P = 0self.Altitude = 0# 读取数据并计算def get(self):adc_P = (self.getReg(0x00)<<16) + (self.getReg(0X01)<<8) + (self.getReg(0X02))adc_P = (0-((adc_P^0xFFFFFF)+1)) if (adc_P & (1<<23))else adc_Padc_T = (self.getReg(0x03)<<16) + (self.getReg(0X04)<<8) + (self.getReg(0X05))adc_T= (0-((adc_T^0xFFFFFF)+1)) if (adc_T & (1<<23))else adc_TTraw_src = adc_T/7864320.0 # 8timesPraw_src = adc_P/516096.0 # 32timesself.T = (0.5*self.C0+Traw_src*self.C1)-4.5 #板子有热源做简单的偏差处理self.P = self.C00+Praw_src*(self.C10+Praw_src*(self.C20+Praw_src*self.C30))+Traw_src*self.C01+Traw_src*Praw_src*(self.C11+Praw_src*self.C21)self.Altitude = 44330*(1-(self.P/101325)**(1/5.255))return [self.T,self.P,self.Altitude]# 获取温度数值:单位℃def getTemp(self):self.get()return self.T# 获取大气压强:单位padef getPress(self):self.get()return self.P# 获取海拔高度:单位米def getAltitude(self):self.get()return self.Altitude#soft Reset def reset(self):self.setReg(0x0C, 0x09)def SPL06Chack(self):#检查SPL06连接正常print("ID:",self.getReg(0x0D))if self.getReg(0x0D)==0x10:return 1else:return 0
4、测试部分:
if __name__ == "__main__" :SPL06_obj=SPL06()if SPL06_obj.SPL06Chack()==1:print('SPL06检测OK')for i in range(1,500):print('Temp:%f ℃ Press:%f Pa Altitude:%f m'%(SPL06_obj.getTemp(),SPL06_obj.getPress(),SPL06_obj.getAltitude()))time.sleep(1)
5、测试结果:
6、完整驱动代码请点击:https://download.csdn.net/download/DD_Boy_liang/85721891
7、模拟IIC代码请点击:https://download.csdn.net/download/DD_Boy_liang/85721934
这篇关于python 驱动 歌儿声学气压传感器 SPL06系列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!