1. 程式人生 > 實用技巧 >怎樣才是刷面試題的正確姿勢?Android400道面試題+通關知識寶典助你進大廠,查漏補缺!

怎樣才是刷面試題的正確姿勢?Android400道面試題+通關知識寶典助你進大廠,查漏補缺!

參考程式碼https://github.com/CRImier/python-MLX90614

需要下載iic的庫

sudo apt install i2c-tools

sudo apt install python-smbus

開啟樹梅派的iic功能

sudo raspi-config

選擇5.Interfacing Options 然後選擇開啟iic

檢視模組的地址

sudo i2cdetect -y -a 1

這裡的預設地址為0x5a

 1 import smbus
 2 from time import sleep
 3 
 4 class MLX90614():
 5 
 6
MLX90614_RAWIR1=0x04 7 MLX90614_RAWIR2=0x05 8 MLX90614_TA=0x06 9 MLX90614_TOBJ1=0x07 10 MLX90614_TOBJ2=0x08 11 12 MLX90614_TOMAX=0x20 13 MLX90614_TOMIN=0x21 14 MLX90614_PWMCTRL=0x22 15 MLX90614_TARANGE=0x23 16 MLX90614_EMISS=0x24 17 MLX90614_CONFIG=0x25 18 MLX90614_ADDR=0x0E 19 MLX90614_ID1=0x3C
20 MLX90614_ID2=0x3D 21 MLX90614_ID3=0x3E 22 MLX90614_ID4=0x3F 23 24 comm_retries = 5 25 comm_sleep_amount = 0.1 26 27 def __init__(self, address=0x5a, bus_num=1): 28 self.bus_num = bus_num 29 self.address = address 30 self.bus = smbus.SMBus(bus=bus_num) 31 32
def read_reg(self, reg_addr): 33 err = None 34 for i in range(self.comm_retries): 35 try: 36 return self.bus.read_word_data(self.address, reg_addr) 37 except IOError as e: 38 err = e 39 #"Rate limiting" - sleeping to prevent problems with sensor 40 #when requesting data too quickly 41 sleep(self.comm_sleep_amount) 42 #By this time, we made a couple requests and the sensor didn't respond 43 #(judging by the fact we haven't returned from this function yet) 44 #So let's just re-raise the last IOError we got 45 raise err 46 47 def data_to_temp(self, data): 48 temp = (data*0.02) - 273.15 49 return temp 50 51 def get_amb_temp(self): 52 data = self.read_reg(self.MLX90614_TA) 53 return self.data_to_temp(data) 54 55 def get_obj_temp(self): 56 data = self.read_reg(self.MLX90614_TOBJ1) 57 return self.data_to_temp(data) 58 59 60 if __name__ == "__main__": 61 sensor = MLX90614() 62 print(sensor.get_amb_temp()) 63 print(sensor.get_obj_temp())

 1 from mlx90614 import MLX90614
 2 import time
 3 
 4 thermometer_address = 0x5a
 5 
 6 thermometer = MLX90614(thermometer_address)
 7 while (1):
 8     print thermometer.get_amb_temp()
 9     print thermometer.get_obj_temp() 
10     time.sleep(1)