当前位置:网站首页>[luatos sensor] 2 air pressure bmp180
[luatos sensor] 2 air pressure bmp180
2022-07-03 04:39:00 【Birds hate fish】
1 Preface
Try to get BMP180 Parameters of .
2 Official source code
BMP180 Air pressure sensor :https://doc.openluat.com/wiki/21?wiki_page_id=2729
Reference material :BMP180 Detailed explanation and examples of air pressure sensor (STM32 With source code )
This article is particularly well written .
In addition, you can also learn from arduino In order to get bmp180 drive , Change in contrast to .
3 Hardware
i2c The equipment is similar , The point is addr,BMP180 by 0x77.
BMP180 Only four pins , The picture above shows BH1750. Don't even addr
4 Official code problem
4.1 i2c.send The change of return value causes printing error log
My development board is esp32 Of , Should be bsp Changed , Therefore, the previous source code is not applicable .
The phenomenon :log Tips i2c.send Returns an error
reason : In the official example i2c,send() The return value is the number of data , Now the return value is true/false
4.2 math.pow Abandoning
math.pow stay lua5.3 Has been deprecated . Use the operator ^
-- local altitude = 44330 * (1-math.pow(((p) / 101325.0),(1.0/5.255)))
local altitude = 44330 * (1-((p) / 101325.0)^(1.0/5.255))
-- math.pow stay lua5.3 Has been deprecated . Use the operator ^
4.3 Common unit conversion
In the source code, multiply the temperature by 0.1, pressure /1000 Conversion for kPa, At an altitude of /1000 Conversion for m
5 Code
BMP180.lua
--- Module function :I2C BMP180 A functional test .
-- @author denghai,youkai
-- @module i2c.BMP180
-- @license MIT
-- @copyright openLuat
-- @release 2021.09.22
--[[ BMP180_REG_ID = 0xD0, //contains 0x55 after power on BMP180_REG_RESET = 0xE0, //write 0xB6 to reset BMP180_REG_STATUS = 0xF3, //bit 0: im_update, bit 3: measuring BMP180_REG_CTRL_MEAS = 0xF4, //sets data acquisition options of device BMP180_REG_CONFIG = 0xF5, //sets the rate, filter and interface options of the device. BMP180_REG_OUT = 0xF6, //raw conversion results BMP180_REG_CAL_AC1 = 0xAA, //2 bytes each. can never be 0x0000 or oxFFFF BMP180_REG_CAL_AC2 = 0xAC, BMP180_REG_CAL_AC3 = 0xAE, BMP180_REG_CAL_AC4 = 0xB0, BMP180_REG_CAL_AC5 = 0xB2, BMP180_REG_CAL_AC6 = 0xB4, BMP180_REG_CAL_B1 = 0xB6, BMP180_REG_CAL_B2 = 0xB8, BMP180_REG_CAL_MB = 0xBA, BMP180_REG_CAL_MC = 0xBC, BMP180_REG_CAL_MD = 0xBE, ]]
BMP180_REG_ID = 0xD0 --contains 0x55 after power on
BMP180_REG_RESET = 0xE0 --write 0xB6 to reset
BMP180_REG_STATUS = 0xF3 --bit 0: im_update bit 3: measuring
BMP180_REG_CTRL_MEAS = 0xF4 --sets data acquisition options of device
BMP180_REG_CONFIG = 0xF5 --sets the rate filter and interface options of the device.
BMP180_REG_OUT = 0xF6 --raw conversion results
BMP180_REG_CAL_AC1 = 0xAA --2 bytes each. can never be 0x0000 or oxFFFF
BMP180_REG_CAL_AC2 = 0xAC
BMP180_REG_CAL_AC3 = 0xAE
BMP180_REG_CAL_AC4 = 0xB0
BMP180_REG_CAL_AC5 = 0xB2
BMP180_REG_CAL_AC6 = 0xB4
BMP180_REG_CAL_B1 = 0xB6
BMP180_REG_CAL_B2 = 0xB8
BMP180_REG_CAL_MB = 0xBA
BMP180_REG_CAL_MC = 0xBC
BMP180_REG_CAL_MD = 0xBE
BMP180_CMD_RESET = 0xB6
--register 0xD0
BMP180_MASK_ID = 0xFF
--register 0xE0
BMP180_MASK_RESET = 0xFF
--register 0xF4
BMP180_MASK_OSS = 0xC0
BMP180_MASK_SCO = 0x20
BMP180_MASK_MCTRL = 0x1F
BMP180_CMD_TEMP = 0x2E --start temperature conversion
BMP180_CMD_PRESS = 0x34 --start pressure conversion
-- i2c ID
i2cid = 0
-- i2c rate
speed = 100000
BMP180_i2c_addr = 0x77
-- initialization
function BMP180_init_i2c(address)
if i2c.setup(i2cid, speed, -1, 1) ~= speed then
log.error("i2c", "setup fail", addr)
return
end
addr = address
-- print("BMP180 addr:",addr)
end
-- Reading data
function BMP180_send(...)
sys.wait(10)
-- print("addr",addr)
if not addr then
log.info("i2c", "addr err")
return
end
local t = {
...}
-- print("t",t[1],t[2])
temp_t = i2c.send(i2cid, addr, t)
-- print("#t",#t)
-- print("temp_t",temp_t)
-- if temp_t ~= #t then -- Now? i2c.BMP180_send() The return value is true 了 , It's not a number
if temp_t ~= true then
log.error("i2c", "BMP180_send fail", #t)
return
end
return true
end
-- send data
function BMP180_read(n)
sys.wait(10)
if not addr then
log.info("i2c", "addr err")
return "\x00"
end
val = i2c.recv(i2cid, addr, n)
-- log.info("BMP180_read", val:toHex())
if val and #val > 0 then return val end
return "\x00"
end
-- Read short value
function BMP180_short(addr, n)
-- print("addr",addr)
BMP180_send(addr) -- Just address ?
-- print("BMP180_send addr")
-- print("n",n)
if n then
f, val = pack.unpack(BMP180_read(2), ">H")
else
f, val = pack.unpack(BMP180_read(2), ">h")
end
-- log.info("val", f, val)
return f and val or 0
end
function BMP180_init()
BMP180_init_i2c(BMP180_i2c_addr) -- Pass in i2c Address 0x77 = 119
BMP180_send(BMP180_REG_ID) -- Incoming address 0xD0, register id
local id = BMP180_read(1)
-- print("bmp180 reg id ",id)
if "U"~=id then
print("error id",id)
sys.restart("error i2c id!")
return
end
-- Reset
BMP180_send(BMP180_REG_RESET,BMP180_CMD_RESET) -- 0xE0,0XB6
-- https://blog.csdn.net/weixin_50622833/article/details/118611152
AC1 = BMP180_short(BMP180_REG_CAL_AC1) -- 0xAA --2 bytes each. can never be 0x0000 or oxFFFF
AC2 = BMP180_short(BMP180_REG_CAL_AC2) -- 0xAC
AC3 = BMP180_short(BMP180_REG_CAL_AC3) -- 0xAE
AC4 = BMP180_short(BMP180_REG_CAL_AC4,true) -- 0xB0
AC5 = BMP180_short(BMP180_REG_CAL_AC5,true) -- 0xB2
AC6 = BMP180_short(BMP180_REG_CAL_AC6,true) -- 0xB4
B1 = BMP180_short(BMP180_REG_CAL_B1) -- 0xB6
B2 = BMP180_short(BMP180_REG_CAL_B2) -- 0xB8
MB = BMP180_short(BMP180_REG_CAL_MB) -- 0xBA
MC = BMP180_short(BMP180_REG_CAL_MC) -- 0xBC
MD = BMP180_short(BMP180_REG_CAL_MD) -- 0xBE
end
function BMP180_get_temp_press()
-- temperature ℃
BMP180_send(BMP180_REG_CTRL_MEAS,BMP180_CMD_TEMP) --0xF4 Represent measurement ,0x2E Indicating temperature
sys.wait(1000)
log.info(" temperature raw", BMP180_short(BMP180_REG_OUT)) --0xF6 Data output
UT = BMP180_short(BMP180_REG_OUT) -- Temperature value
-- print("UT",UT)
-- pressure Pa
BMP180_send(BMP180_REG_CTRL_MEAS,BMP180_CMD_PRESS) --0xF4,0x34
sys.wait(1000)
BMP180_send(BMP180_REG_OUT)
_, UP = pack.unpack(BMP180_read(2), "<H") -- Barometric value
-- print("UP",UP)
log.info(" pressure raw", UP)
return BMP_UncompemstatedToTrue(UT,UP)
end
function BMP180_test()
-- sys.wait(8000)
BMP180_init()
while true do
sys.wait(2000)
BMP180_get_temp_press()
end
end
--https://blog.csdn.net/weixin_50622833/article/details/118611152
--// Use the obtained parameters to correct the temperature and atmospheric pressure , And calculate the altitude
function BMP_UncompemstatedToTrue(UT,UP)
local Press = 0
local X1 = (UT - AC6) * AC5/32768 -- 2^15, Data shift right 15 position
-- log.info("X1 ",X1,"UT ",UT,"AC6 ",AC6)
-- local X2 = bit.lshift(MC,11) / (X1 + MD)
local X2 = MC*2048 / (X1 + MD) -- 2^11, Move left 11 position
-- log.info("X2",MC,"MC",X2,"MD",MD)
local B5 = X1 + X2
-- log.info("B5",B5)
-- local Temp = bit.rshift((B5 + 8) ,4)
local Temp = (B5 + 8)/16
Temp = Temp * 0.1
log.info(" temperature : ",Temp.." °C")
local B6 = B5 - 4000
-- log.info("B6",B6)
X1 = (B2 *((B6 * B6)/4096))/2048
-- X1 = bit.rshift(B2 * bit.rshift(B6 * B6,12) ,11)
-- log.info("X1",X1,"B2",B2,"B6",B6)
X2 = (AC2 * B6)/2048
-- X2 = bit.rshift(AC2 * B6,11)
-- log.info("X2",X2,"AC2",AC2)
local X3 = X1 + X2
-- log.info("X3",X3)
local B3 = ((AC1 * 4 + X3) + 2) /4
-- log.info("B3",B3,"AC1",AC1)
X1 = (AC3 * B6)/8192
-- X1 = bit.rshift(AC3 * B6 ,13)
-- log.info("X1",X1,"AC3",AC3)
X2 = (B1 *((B6*B6)/4096)) /65536
-- X2 = bit.rshift((B1 *bit.rshift(B6*B6 ,12)) ,16)
-- log.info("X2",X2,"B1",B1)
-- X3 = bit.rshift(X1 + X2 + 2, 2)
X3 = (X1 + X2 + 2)/4
-- log.info("X3",X3)
local B4 = (AC4 * (X3 + 32768))/32768
-- local B4 = bit.rshift(AC4 * (X3 + 32768) ,15)
-- log.info("B4",B4,"AC4",AC4)
local B7 = (UP - B3) * 50000
-- log.info("B7",B7,"UP",UP)
if(B7 < 0x80000000) then
Press = (B7 * 2) / B4
else
Press = (B7 / B4) * 2
end
-- log.info("Press",Press)
X1 = (Press/256) * (Press/256)
-- X1 = bit.rshift(Press ,8) * bit.rshift(Press,8)
-- log.info("X1",X1)
X1 = (X1 * 3038)/65536
-- X1 = bit.rshift(X1 * 3038,16)
-- log.info("X1",X1)
X2 = (-7357 * Press)/65536
-- X2 = bit.rshift(-7357 * Press, 16)
-- log.info("X2",X2)
Press = Press + (X1 + X2 + 3791)/16
-- Press = Press + bit.rshift(X1 + X2 + 3791,4)
-- log.info(" Air pressure correction ", Press)
-- Press = Press * 0.001
log.info(" pressure : ", Press * 0.001 .." kPa")
local altitude = 44330 * (1-((Press) / 101325.0)^1.0/5.255)
altitude = altitude * 0.001
log.info(" At an altitude of : ", altitude.." m")
return Temp,Press * 0.001,altitude
end
main.lua
PROJECT = "BMP180"
VERSION = "1.0.0"
_G.sys = require("sys")
-- load I²C Function test module
require ("BMP180")
sys.taskInit(function()
-- ps: Yes wait You can't put it outside
BMP180_test()
-- sys.wait(1500)
while 1 do
sys.wait(100)
end
end)
sys.run()
6 result
Personally, I feel that the accuracy is not high .
边栏推荐
- Handling record of electric skateboard detained by traffic police
- 雇佣收银员(差分约束)
- [dynamic programming] subsequence problem
- 2022-02-13 (347. Top k high frequency elements)
- SSM based campus part-time platform for College Students
- After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
- Leetcode simple question: check whether the array is sorted and rotated
- A outsourcing boy's mid-2022 summary
- 带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
- Leetcode simple problem delete an element to strictly increment the array
猜你喜欢
2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
Introduction to message queuing (MQ)
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Introduction to JVM principle
C language series - Section 3 - functions
FuncS sh file not found when using the benchmarksql tool to test kingbases
《牛客刷verilog》Part II Verilog进阶挑战
[luatos sensor] 1 light sensing bh1750
stm32逆向入门
随机推荐
C primre plus Chapter 10 question 6 inverted array
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Prefix and (continuously updated)
Summary of training competition (Lao Li's collection of questions)
vulnhub HA: Natraj
Leetcode simple question: the key with the longest key duration
Small program animation realizes the running lantern and animation object
MediaTek 2023 IC written examination approved in advance (topic)
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
[BMZCTF-pwn] 18-RCTF-2017-Recho
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
7. Integrated learning
金仓数据库KingbaseES 插件kdb_exists_expand
第十九届浙江省 I. Barbecue
AWS VPC
arthas watch 抓取入参的某个字段/属性
[BMZCTF-pwn] 20-secret_ file
[software testing-6] & Test Management
金仓数据库KingbaseES 插件kdb_date_function
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing