1.15.1. Max31865
MAX31865是一款集成的的单芯片RTD数字转换器,用于替代多个分立元件来降低成本。它提供简单而准确的温度测量,非常适合用于测量和过程控制,是在工业领域最常用的测量方案。
MAX31865完全集成了RTD数字转换器,单芯片方案降低了系统成本(CD约50%)和系统的复杂度,尤其适用于工业中最常用的铂RTD (如Pt100或Pt1000),解决了电阻->数字转换问题。
典型特征:
u 简单地解决了铂RTD电阻数字化问题
u 适用PT100、PT1000
u 适用于2线、3线、4线接法
u 15bits ADC精度
u 全局精度≤0.5℃
u 输入保护±50V
u 查分输入
u 检测机制
u SPI总线
u 20-PIN TQFN封装
TQFN封装手工焊有难度,故实验中采用某宝采购的模块和3线PT100,如上图所示。
1.15.2. SPI总线
ESP32的micropython固件,内置硬件spi模块SPI(spi1、spi2)和软件spi模块SoftSPI。SPI模块/SoftSPI模块作为machine的子模块,与Pin/I2C/Uart等处于同级目录。
下图摘自micropython官网:
实际使用时,硬件spi/软件spi均可满足使用要求,本实验中使用软件spi。
1.15.3. 接线
Pin |
MAX31865 |
ESP32-WROOM-32 |
CS |
CS |
D4 |
SDI |
Sdi |
D23 |
SDO |
sdo |
D19 |
SCK |
sck |
D18 |
1.15.4. 读取温度
代码实现如下:
1 from time import sleep,sleep_ms 2 from machine import SPI,Pin,SoftSPI 3 4 cs = Pin(4,Pin.OUT) 5 max31865 = SoftSPI(baudrate=1000000,\ 6 polarity=0,phase=1,\ 7 firstbit=SPI.MSB,\ 8 sck=Pin(18),\ 9 mosi=Pin(23),\ 10 miso=Pin(19)) 11 12 max31865.init() 13 14 DEBUG = False 15 16 def config(value=b'\x80\xd3'): 17 cs.off() 18 max31865.write(value) 19 cs.on() 20 pass 21 22 def readConfig(): 23 cs.off() 24 max31865.write(b'\x00') 25 config = max31865.read(1) 26 cs.on() 27 if DEBUG: 28 print(config) 29 30 def readADC(): 31 cs.off() 32 max31865.write(b'\x01') 33 msb = max31865.read(1) 34 lsb = max31865.read(1) 35 cs.on() 36 if DEBUG: 37 print("\t->msb:"+str(msb)+";lsb:"+str(lsb)) 38 return(msb[0],lsb[0]) 39 40 def readHFault(): 41 cs.off() 42 max31865.write(b'\x03') 43 msb = max31865.read(1) 44 lsb = max31865.read(1) 45 cs.on() 46 if DEBUG: 47 print("\t->hfmsb:"+str(msb)+";hflsb:"+str(lsb)) 48 49 def readLFault(): 50 cs.off() 51 max31865.write(b'\x05') 52 msb = max31865.read(1) 53 lsb = max31865.read(1) 54 cs.on() 55 if DEBUG: 56 print("\t->lfmsb:"+str(msb)+";lflsb:"+str(lsb)) 57 58 def readFault(): 59 cs.off() 60 max31865.write(b'\x07') 61 config = max31865.read(1) 62 cs.on() 63 if DEBUG: 64 print("\tfault:",config) 65 66 Config() 67 68 while True: 69 ''' 70 max31865手册p19,r_pt=100Ω对应0℃, 71 r_pt=111.67Ω对应30℃,带入公式 72 t = r*a+b可得a = 2.57,b=-257 73 ∴temperature = 2.57*r_pt - 257 74 ''' 75 76 m,l = readADC() 77 adc_code = (((m*256)+l)>>1) 78 temp = ((adc_code/32) -256) 79 r_pt = ((adc_code*430)/32768) 80 print("R:",r_pt) 81 print("temperature:%3.2f ℃" %(2.57*r_pt-257)) 82 sleep(1)
注意:max31865 datasheet说明
a. 连续转换时不要改变config寄存器D0值;
b. Max31865支持spi模式1/3,所以phase必须为1;
c. 高字节在前,MSB模式;
1.15.5. 抽象为模块
Max31865驱动模块源码:
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=1000000,\ 11 polarity=0,\ 12 phase=1,\ 13 firstbit=SPI.MSB,\ 14 debug=False,\ 15 compensation=0): 16 ''' 17 默认 18 baudrate=1000000 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'\x80\xd3'): 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("\t->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("\t->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("\t->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("\tfault:",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)
Main.py文件示例:
1 from max31865 import MAX31865 2 from time import sleep 3 4 sensor = MAX31865(18,23,19,4) 5 6 while True: 7 print("Temperature:%3.2f" %(sensor.readTemperature())) 8 sleep(1)
运行效果如下图:
----*----*----*----*----*----*----*----*----*----*----*----
史久超
email:shijiuchao1983@163.com
tel:15999550975 微信同号
qq:157185173 经常不在线,尽量微信