ugnen

git clone https://git.tarina.org/ugnen
Log | Files | Refs

max6675.py (1301B)


      1 import RPi.GPIO as GPIO
      2 import time
      3 GPIO.setmode(GPIO.BCM)
      4 GPIO.setwarnings(False)
      5 
      6 # set pin number for communicate with MAX6675
      7 def set_pin (CS, SCK, SO, UNIT):
      8     global sck
      9     sck= SCK
     10     global so
     11     so = SO
     12     global unit
     13     unit = UNIT
     14     
     15     GPIO.setup(CS, GPIO.OUT, initial = GPIO.HIGH)
     16     GPIO.setup(SCK, GPIO.OUT, initial = GPIO.LOW)
     17     GPIO.setup(SO, GPIO.IN)
     18 
     19 def read_temp(cs_no):
     20     
     21     GPIO.output(cs_no, GPIO.LOW)
     22     time.sleep(0.002)
     23     GPIO.output(cs_no, GPIO.HIGH)
     24     time.sleep(0.22)
     25 
     26     GPIO.output(cs_no, GPIO.LOW)
     27     GPIO.output(sck, GPIO.HIGH)
     28     time.sleep(0.001)
     29     GPIO.output(sck, GPIO.LOW)
     30     Value = 0
     31     for i in range(11, -1, -1):
     32         GPIO.output(sck, GPIO.HIGH)
     33         Value = Value + (GPIO.input(so) * (2 ** i))
     34         GPIO.output(sck, GPIO.LOW)
     35 
     36     GPIO.output(sck, GPIO.HIGH)
     37     error_tc = GPIO.input(so)
     38     GPIO.output(sck, GPIO.LOW)
     39 
     40     for i in range(2):
     41         GPIO.output(sck, GPIO.HIGH)
     42         time.sleep(0.001)
     43         GPIO.output(sck, GPIO.LOW)
     44 
     45     GPIO.output(cs_no, GPIO.HIGH)
     46 
     47     if unit == 0:
     48         temp = Value
     49     if unit == 1:
     50         temp = Value * 0.25
     51     if unit == 2:
     52         temp = Value * 0.25 * 9.0 / 5.0 + 32.0
     53 
     54     if error_tc != 0:
     55         return -cs_no
     56     else:
     57         return temp
     58 
     59 GPIO.cleanup()
     60