K210代码:
from machine import I2C
import time
i2c = I2C(I2C.I2C3,freq=100000, scl=28, sda=29)
time.sleep_ms(100)
MLX90614_IIC_ADDR = const(0x00)
MLX90614_TA = const(0x06)
MLX90614_TOBJ1 = const(0x07)
temp=0
class MLX90614:
def __init__(self,i2c,addr=MLX90614_IIC_ADDR):
self.addr=addr
self.i2c=i2c
def getObjCelsius(self):
return self.getTemp(MLX90614_TOBJ1) #Get celsius temperature of the object
def getEnvCelsius(self):
return self.getTemp(MLX90614_TA) #Get celsius temperature of the ambient
def getObjFahrenheit(self):
return (self.getTemp(MLX90614_TOBJ1) * 9 / 5) + 32 #Get fahrenheit temperature of the object
def getEnvFahrenheit(self):
return (self.getTemp(MLX90614_TA) * 9 / 5) + 32 #Get fahrenheit temperature of the ambient
def getTemp(self,reg):
global temp
temp = self.getReg(reg)*0.02-273.15 #Temperature conversion
def getReg(self,reg):
data = self.i2c.readfrom_mem(self.addr,reg,3) #Receive DATA
time.sleep_ms(100)
result = (data[1]<<8) | data[0]
return result
ir = MLX90614(i2c)
while (1):
ir.getObjCelsius()
print("temp %s *C"% (temp))
如果出现如下报错,是因为IIC出问题了:
OSError: [Errno 5] EIO
先检查红外测温传感器与单片机的连接,若还是报错,则修改下面这句代码中的“I2C.I2C3”和“scl=28, sda=29”的引脚。
i2c = I2C(I2C.I2C3,freq=100000, scl=28, sda=29)
I2C.I2C3可修改为:I2C.I2C0,I2C.I2C1,I2C.I2C2