1 from time import sleep,sleep_ms 2 from machine import SPI,Pin,SoftSPI 3 4 class MAX31865: 5 def init(self, 6 sck, 7 mosi, 8 miso, 9 cs, 10 baudrate=, 11 polarity=0, 12 phase=1, 13 firstbit=SPI.MSB, 14 debug=False, 15 compensation=0): 16 ”’ 17 默认 18 baudrate= 10M 19 polarity=0 20 phase =1 21 MSB 22 compensation=0 23 ”’ 24 25 self.cs = Pin(cs,Pin.OUT) 26 self.max31865 = SoftSPI(baudrate=baudrate, 27 polarity=polarity,phase=phase, 28 firstbit=SPI.MSB, 29 sck=Pin(sck), 30 mosi=Pin(mosi), 31 miso=Pin(miso)) 32 self.compensation = compensation 33 self.max31865.init() 34 self.DEBUG = debug 35 self.config() 36 37 def config(self,value=b‘x80xd3’): 38 ”’ 39 默认: 40 连续转换 bias on 41 ”’ 42 self.cs.off() 43 self.max31865.write(value) 44 self.cs.on() 45 pass 46 47 def readConfig(self): 48 ”’ 49 读配置寄存器 50 ”’ 51 self.cs.off() 52 self.max31865.write(b‘x00’) 53 config = self.max31865.read(1) 54 self.cs.on() 55 if self.DEBUG: 56 print(config) 57 return(config) 58 59 def readADC(self): 60 ”’ 61 读ADC值 62 ”’ 63 self.cs.off() 64 self.max31865.write(b‘x01’) 65 msb = self.max31865.read(1) 66 lsb = self.max31865.read(1) 67 self.cs.on() 68 if self.DEBUG: 69 print(“ ->msb:”+str(msb)+“;lsb:”+str(lsb)) 70 return(msb[0],lsb[0]) 71 72 def readHFault(self): 73 ”’ 74 读上界限寄存器 75 ”’
76 self.cs.off() 77 self.max31865.write(b‘x03’) 78 msb = self.max31865.read(1) 79 lsb = self.max31865.read(1) 80 self.cs.on() 81 if self.DEBUG: 82 print(“ ->hfmsb:”+str(msb)+“;hflsb:”+str(lsb)) 83 return(msb[0],lsb[0]) 84 85 def readLFault(self): 86 ”’ 87 读下界限寄存器 88 ”’
89 self.cs.off() 90 self.max31865.write(b‘x05’) 91 msb = self.max31865.read(1) 92 lsb = self.max31865.read(1) 93 self.cs.on() 94 if self.DEBUG: 95 print(“ ->hfmsb:”+str(msb)+“;hflsb:”+str(lsb)) 96 return(msb[0],lsb[0]) 97 98 def readFault(self): 99 ”’ 100 读错误 101 ”’
102 self.cs.off() 103 self.max31865.write(b‘x07’) 104 config = self.max31865.read(1) 105 self.cs.on() 106 if self.DEBUG: 107 print(“ fault:”,config) 108 return(config) 109 110 def readTemperature(self): 111 ”’ 112 读温度值 113 ”’ 114 m,l = self.readADC() 115 adc_code = (((m*256)+l)>>1) 116 temp = ((adc_code/32) -256) 117 r_pt = ((adc_code*430)/32768) 118 temperature = (2.57*r_pt-257+self.compensation) 119 return(temperature)
讯享网


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/175041.html