差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
barometersensors [2023/07/24 11:06] huangyuan |
barometersensors [2023/07/25 12:07] (当前版本) huangyuan |
||
---|---|---|---|
行 87: | 行 87: | ||
### 5. 参考案例 | ### 5. 参考案例 | ||
- | [[https:// www.murata.com/en-us/products/productdetail?partno=ZPA4756-0311A-R]] | + | from machine import I2C, Pin |
- | + | import time | |
- | [[https://en.wikipedia.org/wiki/Barometer]] | + | i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=400_000) |
+ | ms5611_c = [0, 0, 0, 0, 0, 0, 0, 0] | ||
+ | GY63_ADDRESS = 0x77 | ||
+ | print(i2c.scan()) | ||
+ | def reset(): | ||
+ | i2c.writeto(GY63_ADDRESS, bytearray([0x1E])) | ||
+ | time.sleep(0.01) | ||
+ | def init(): | ||
+ | reset() | ||
+ | for i in range(8): | ||
+ | ms5611_c[i] = prom(i) | ||
+ | def prom(coef_num): | ||
+ | rxbuff = i2c.readfrom_mem(GY63_ADDRESS, 0XA0+coef_num*2, 3) | ||
+ | return rxbuff[0] << 8 | rxbuff[1] | ||
+ | def read_pressure(): | ||
+ | i2c.writeto(GY63_ADDRESS, bytearray([0x48])) | ||
+ | time.sleep(0.02) | ||
+ | data = i2c.readfrom_mem(GY63_ADDRESS, 0, 3) | ||
+ | pressure = (data[0] << 16) + (data[1] << 8) + data[2] | ||
+ | return pressure | ||
+ | def read_temperature(): | ||
+ | i2c.writeto(GY63_ADDRESS, bytearray([0x58])) | ||
+ | time.sleep(0.02) | ||
+ | data = i2c.readfrom_mem(GY63_ADDRESS, 0, 3) | ||
+ | temperature = (data[0] << 16) + (data[1] << 8) + data[2] | ||
+ | return temperature | ||
+ | def calculate(ut, up): | ||
+ | dT = ut - (ms5611_c[5] << 8) | ||
+ | off = (ms5611_c[2] << 16) + ((ms5611_c[4]*dT) >> 7) | ||
+ | sens = (ms5611_c[1] << 15) + ((ms5611_c[3]*dT) >> 8) | ||
+ | temp = 2000 + ((dT*ms5611_c[6]) >> 23) | ||
+ | if (temp < 2000): | ||
+ | delt = temp - 2000 | ||
+ | delt = 5 * delt *delt | ||
+ | off = off - (delt >> 1) | ||
+ | sens = sens - (delt >> 2) | ||
+ | if (temp < -1500): | ||
+ | delt = temp + 1500 | ||
+ | delt = delt * delt | ||
+ | off = off - (7 * delt) | ||
+ | sens = sens - ((11 * delt) >> 1) | ||
+ | temp = temp - ((dT*dT) >> 31) | ||
+ | press = (((int(up)*sens) >> 21) - off) >> 15 | ||
+ | return press, temp | ||
+ | init() | ||
+ | while True: | ||
+ | up = read_pressure() | ||
+ | ut = read_temperature() | ||
+ | pressure, temperature = calculate(ut, up) | ||
+ | print("Pressure:", pressure) | ||
+ | print("Temperature:", temperature) | ||
+ | time.sleep(1) | ||
+ | |||
+ | 结果显示: | ||
+ | |||
+ | {{ ::qiya.png?500 |}} | ||