#! /usr/bin/env python3 import spidev import time import RPi.GPIO as GPIO from decimal import * tnom = 25 rnom = 10000.0 corr = 4800 temp_gpio = { 1: { "BCM": 26, "Rk": 9770.0 #9770, }, 0: { "BCM": 20, "Rk": 10000.0 } } GPIO.setmode(GPIO.BCM) for k in temp_gpio: GPIO.setup(temp_gpio[k]["BCM"], GPIO.OUT) GPIO.output(temp_gpio[k]["BCM"], False) def read_spiADC(spi, channel): if channel > 7 or channel < 0: return [] GPIO.output(temp_gpio[channel]["BCM"], True) hchbit=channel >> 2 lchbits = channel & 0x03 to_send = [ hchbit | 0x06, lchbits << 6, 0x00 ] spi.cshigh=True # time.sleep(0.01) spi.cshigh = False r=spi.xfer(to_send) spi.cshigh = True GPIO.output(temp_gpio[channel]["BCM"], False) # time.sleep(0.01) highbyte = r[1] lowbyte = r[2] value = (highbyte << 8) | lowbyte return value def calc_temp(adc_reading, channel = 0): rser = temp_gpio[channel]["Rk"] r = (rser/(4095/adc_reading - 1)) r = r - corr t = r/rnom if t < 0: t = t * -1 t = float(Decimal(t).ln()) / 4050 + 1.0/(tnom + 273.15) t = 1/t t = t - 273.15 return t #GPIO.output(temp_gpio[1], True) #while True: # pass spi = spidev.SpiDev() spi.open(0,0) spi.mode = 0 spi.max_speed_hz = 1000000 while True: chan0 = read_spiADC(spi, 0) chan1 = read_spiADC(spi, 1) r0 = 0 r1 = 0 t0 = 0 t1 = 0 if chan0 != 0: r0 = rnom/(4095/ chan0 - 1) t0 = calc_temp(chan0, 0) if chan1 != 0: r1 = rnom/(4095/ chan1 - 1) t1 = calc_temp(chan1, 1) print("{:d} R={:d} T={:.2f} |\t| {:d} R={:d} T={:.2f}".format(chan0,int(r0), t0, chan1, int(r1), t1), end="\r") time.sleep(0.25) # print(str(chan0) +" R=" + str(int(r0)) + " T=" + str(t0) +" |\t| "+str(chan1)+" R="+str(int(r1)) + " T=" +str(t1), end="\r") spi.close()