折腾了一个星期了,读出的数据没有变化,麻烦帮忙看看!
代码如下:
module(...,package.seeall)
require"utils"
require"ntp"
require"misc"
require"pins"
local i2cId = 2
local slen = string.len
local BMP280_ADDR = 0x76
local BMP280_TEMP_ADDR = 0xFA
local BMP280_PRESS_ADDR = 0xF7
local dig_t1
local dig_t2
local dig_t3
local dig_p1
local dig_p2
local dig_p3
local dig_p4
local dig_p5
local dig_p6
local dig_p7
local dig_p8
local dig_p9
local function sourcesInfo()
print("mem:",_G.collectgarbage("count")) -- 打印占用的RAM
print("flash:",rtos.get_fs_free_size()) -- 打印剩余FALSH,单位Byte
end
---i2c初始化
---i2c.FAST (400KHz), i2c.SLOW (100KHz)
---Max Frequency: 400kHz
local function i2cInit()
if i2c.setup(i2cId,i2c.SLOW) ~= i2c.SLOW then
log.info("I2C","I2c.init fail.")
return
end
end
---i2c写数据
local function i2cWrite(i2caddr,regaddr,data)
---写寄存器
i2c.send(i2cId,i2caddr,regaddr)
---写数据
i2c.send(i2cId,i2caddr,data)
end
---i2c读数据
local function i2cRead(i2caddr,regaddr,size)
---读寄存器
i2c.send(i2cId,i2caddr,regaddr)
---读数据
return string.toHex(i2c.recv(i2cId,i2caddr,size))
end
----------------------------------------
---2个数据和3个数据的高低位顺序不一样---
----------------------------------------
---i2c读连续地址3个数据
local function i2cReadThreeData(i2caddr,regaddr)
log.info("i2cReadThreeData","addr:"..i2caddr.." reg:"..regaddr)
local msb = i2cRead(i2caddr,regaddr,1)
local lsb = i2cRead(i2caddr,regaddr+1,1)
local xlsb = i2cRead(i2caddr,regaddr+2,1)
if msb == "" or lsb == "" or xlsb == "" or msb == nil or lsb == nil or xlsb == nil then
log.info("BMP280 three data","the data is null")
else
--20位精度
log.info("BMP280 three data","msb:"..msb.." lsb:"..lsb.." xlsb:"..xlsb)
return bit.lshift(tonumber(msb,16),12) + bit.lshift(tonumber(lsb,16),4) + bit.rshift(tonumber(xlsb,16),4)
end
end
---i2c读连续地址2个数据
local function i2cReadTwoData(i2caddr,regaddr)
local lsb = i2cRead(i2caddr,regaddr,1)
local msb = i2cRead(i2caddr,regaddr+1,1)
if lsb == "" or msb == "" or lsb == nil or msb == nil then
log.info("BMP280 two data","the data is null")
else
log.info("BMP280 two data","msb:"..msb.." lsb:"..lsb)
return bit.lshift(tonumber(msb,16),8) + tonumber(lsb,16)
end
end
---bmp280初始化
local id
local function bmp280Init()
id = i2cRead(BMP280_ADDR,0xd0,1)
log.info("bmp280 id:",id)
---读取压力补偿值
dig_t1 = i2cReadTwoData(BMP280_ADDR,0x88)
dig_t2 = i2cReadTwoData(BMP280_ADDR,0x8a)
dig_t3 = i2cReadTwoData(BMP280_ADDR,0x8c)
dig_p1 = i2cReadTwoData(BMP280_ADDR,0x8e)
dig_p2 = i2cReadTwoData(BMP280_ADDR,0x90)
dig_p3 = i2cReadTwoData(BMP280_ADDR,0x92)
dig_p4 = i2cReadTwoData(BMP280_ADDR,0x94)
dig_p5 = i2cReadTwoData(BMP280_ADDR,0x96)
dig_p6 = i2cReadTwoData(BMP280_ADDR,0x98)
dig_p7 = i2cReadTwoData(BMP280_ADDR,0x9a)
dig_p8 = i2cReadTwoData(BMP280_ADDR,0x9c)
dig_p9 = i2cReadTwoData(BMP280_ADDR,0x9e)
i2cWrite(BMP280_ADDR,0xe0,0xb6)
sys.wait(1000)
i2cWrite(BMP280_ADDR,0xf4,0xff)
i2cWrite(BMP280_ADDR,0xf5,0x00)
end
local function readbmp280Press()
local adc_T = i2cReadThreeData(BMP280_ADDR,BMP280_TEMP_ADDR)
local adc_P = i2cReadThreeData(BMP280_ADDR,BMP280_PRESS_ADDR)
if adc_P == nil then
return 0
end
local var1 = (adc_T/16384.0-dig_t1/1024.0) * dig_t2
local var2 = (adc_T/131072.0 - dig_t1/8192.0) * (adc_T/131072.0 - dig_t1/8192.0) * dig_t3
local t_fine = var1 + var2
T = (var1+var2)/5120.0
var1 = t_fine/2.0 - 64000.0
var2 = var1 * var1 * dig_p6 / 32768.0
var2 = var2 + var1 * dig_p5 * 2.0
var2 = (var2/4.0) + dig_p4
var1 = dig_p3*var1*var1/524288.0 + dig_p2*var1/524288.0
var1 = (1.0+var1/32768.0) * dig_p1
local p = 1048576.0 - adc_P
p = (p - var2/4096.0) * 6250.0/var1
var1 = dig_p9*p*p/2147483648.0
var2 = p*dig_p8/32768.0
p = p + (var1 + var2 + dig_p7)/16.0
return p
end
local function task()
---i2c初始化
i2cInit()
---bmp280初始化
bmp280Init()
while true do
---打印数据
log.info("bmp280气压值:"..readbmp280Press().."Pa".." id:"..id)
sys.wait(1000)
end
end
sys.taskInit(task)