其他分享
首页 > 其他分享> > 2021-12-3 ds18x20包装库

2021-12-3 ds18x20包装库

作者:互联网

ds18x20.py

from onewire import OneWire
class DS18X20(object):
  def __init__(self, pin):
    self.ow = OneWire(pin)
    # Scan the 1-wire devices, but only keep those which have the
    # correct # first byte in their rom for a DS18x20 device.
    self.roms = [rom for rom in self.ow.scan() if rom[0] == 0x10 or rom[0] == 0x28]
  def read_temp(self, rom=None):
    """
    Read and return the temperature of one DS18x20 device.
    Pass the 8-byte bytes object with the ROM of the specific device you want to read.
    If only one DS18x20 device is attached to the bus you may omit the rom parameter.
    """
    rom = rom or self.roms[0]
    ow = self.ow
    ow.reset()
    ow.select_rom(rom)
    ow.write_byte(0x44)  # Convert Temp
    while True:
      if ow.read_bit():
        break
    ow.reset()
    ow.select_rom(rom)
    ow.write_byte(0xbe)  # Read scratch
    data = ow.read_bytes(9)
    return self.convert_temp(rom[0], data)
  def read_temps(self):
    """
    Read and return the temperatures of all attached DS18x20 devices.
    """
    temps = []
    for rom in self.roms:
      temps.append(self.read_temp(rom))
    return temps
  def convert_temp(self, rom0, data):
    """
    Convert the raw temperature data into degrees celsius and return as a float.
    """
    temp_lsb = data[0]
    temp_msb = data[1]
    if rom0 == 0x10:
      if temp_msb != 0:
      # convert negative number
        temp_read = temp_lsb >> 1 | 0x80  # truncate bit 0 by shifting, fill high bit with 1.
        temp_read = -((~temp_read + 1) & 0xff) # now convert from two's complement
      else:
        temp_read = temp_lsb >> 1  # truncate bit 0 by shifting
      count_remain = data[6]
      count_per_c = data[7]
      temp = temp_read - 0.25 + (count_per_c - count_remain) / count_per_c
      return temp
    elif rom0 == 0x28:
      return (temp_msb << 8 | temp_lsb) / 16
    else:
      assert False

ds18b20test.py

#Hardware Platform: FireBeetle-ESP32
#Result: measure enviromental temperature.
#Hardware Connection: this test need to connect a 'DS18B20' digital temperature sensor in IO25.
#The info below shows that ds18b20Demo is available for the current version. 
# IO0  IO2  IO4  IO5  IO9  IO10 IO12~19 
# IO21 IO22 IO23 IO25 IO26 IO27 
from machine import Pin
import onewire
import ds18x20
import time
class Ds18b20_test(object):
    def __init__(self,ow):
        self.ow=ow
        self.ow.scan()
        self.ds=ds18x20.DS18X20(self.ow)          #create ds18x20 object     
    def read_data(self):
          roms=self.ds.scan()                #scan ds18x20
          self.ds.convert_temp()             #convert temperature
          for rom in roms:
            rec_data=self.ds.read_temp(rom)    #display
            print(rec_data)
            return rec_data         
if __name__=='__main__':
    ow = onewire.OneWire(Pin(25))   #Init wire
    a=Ds18b20_test(ow)
    a.read_data()
from ds18b20test import Ds18b20_test
import onewire
ow = onewire.OneWire(Pin(25))   #Init wire
a=Ds18b20_test(ow)
a.read_data()

标签:12,rom,temp,read,self,2021,ow,data,ds18x20
来源: https://blog.csdn.net/gaoke11240/article/details/121705255