Advertisement
Prof_Proton

automatischer Kaminofen

May 12th, 2015
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.49 KB | None | 0 0
  1. """
  2. Connections:
  3.  
  4. LED Bar to JP5
  5. LED Display to JP7
  6. Temperature Sensor to JP1
  7. Servo to Pin 35 (GPIO 19)
  8. """
  9.  
  10. import spidev
  11. import time
  12. import sys
  13. import os
  14. import RPi.GPIO as IO
  15.  
  16.  
  17. t_soll = 25
  18.  
  19. ##======================================================================
  20. ##======================================================================
  21.  
  22.  
  23. IO.setwarnings(False)
  24. IO.setmode(IO.BCM)
  25.  
  26. HexDigits = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71]
  27.  
  28. ADDR_AUTO = 0x40
  29. ADDR_FIXED = 0x44
  30. STARTADDR = 0xC0
  31. BRIGHT_DARKEST = 0
  32. BRIGHT_TYPICAL = 2
  33. BRIGHT_HIGHEST = 7
  34. OUTPUT = IO.OUT
  35. INPUT = IO.IN
  36. LOW = IO.LOW
  37. HIGH = IO.HIGH
  38.  
  39. class TM1637:
  40.     __doublePoint = False
  41.     __Clkpin = 0
  42.     __Datapin = 0
  43.     __brightnes = BRIGHT_TYPICAL;
  44.     __currentData = [0,0,0,0];
  45.    
  46.     def __init__( self, pinClock, pinData, brightnes ):
  47.         self.__Clkpin = pinClock
  48.         self.__Datapin = pinData
  49.         self.__brightnes = brightnes;
  50.         IO.setup(self.__Clkpin,OUTPUT)
  51.         IO.setup(self.__Datapin,OUTPUT)
  52.     # end  __init__
  53.  
  54.     def Clear(self):
  55.         b = self.__brightnes;
  56.         point = self.__doublePoint;
  57.         self.__brightnes = 0;
  58.         self.__doublePoint = False;
  59.         data = [0x7F,0x7F,0x7F,0x7F];
  60.         self.Show(data);
  61.         self.__brightnes = b;               # restore saved brightnes
  62.         self.__doublePoint = point;
  63.     # end  Clear
  64.  
  65.     def Show( self, data ):
  66.         for i in range(0,4):
  67.             self.__currentData[i] = data[i];
  68.        
  69.         self.start();
  70.         self.writeByte(ADDR_AUTO);
  71.         self.stop();
  72.         self.start();
  73.         self.writeByte(STARTADDR);
  74.         for i in range(0,4):
  75.             self.writeByte(self.coding(data[i]));
  76.         self.stop();
  77.         self.start();
  78.         self.writeByte(0x88 + self.__brightnes);
  79.         self.stop();
  80.     # end  Show
  81.  
  82.     def Show1(self, DigitNumber, data): # show one Digit (number 0...3)
  83.         if( DigitNumber < 0 or DigitNumber > 3):
  84.             return; # error
  85.    
  86.         self.__currentData[DigitNumber] = data;
  87.        
  88.         self.start();
  89.         self.writeByte(ADDR_FIXED);
  90.         self.stop();
  91.         self.start();
  92.         self.writeByte(STARTADDR | DigitNumber);
  93.         self.writeByte(self.coding(data));
  94.         self.stop();
  95.         self.start();
  96.         self.writeByte(0x88 + self.__brightnes);
  97.         self.stop();
  98.     # end  Show1
  99.        
  100.     def SetBrightnes(self, brightnes):      # brightnes 0...7
  101.         if( brightnes > 7 ):
  102.             brightnes = 7;
  103.         elif( brightnes < 0 ):
  104.             brightnes = 0;
  105.  
  106.         if( self.__brightnes != brightnes):
  107.             self.__brightnes = brightnes;
  108.             self.Show(self.__currentData);
  109.         # end if
  110.     # end  SetBrightnes
  111.  
  112.     def ShowDoublepoint(self, on):          # shows or hides the doublepoint
  113.         if( self.__doublePoint != on):
  114.             self.__doublePoint = on;
  115.             self.Show(self.__currentData);
  116.         # end if
  117.     # end  ShowDoublepoint
  118.            
  119.     def writeByte( self, data ):
  120.         for i in range(0,8):
  121.             IO.output( self.__Clkpin, LOW)
  122.             if(data & 0x01):
  123.                 IO.output( self.__Datapin, HIGH)
  124.             else:
  125.                 IO.output( self.__Datapin, LOW)
  126.             data = data >> 1
  127.             IO.output( self.__Clkpin, HIGH)
  128.         #endfor
  129.  
  130.         # wait for ACK
  131.         IO.output( self.__Clkpin, LOW)
  132.         IO.output( self.__Datapin, HIGH)
  133.         IO.output( self.__Clkpin, HIGH)
  134.         IO.setup(self.__Datapin, INPUT)
  135.        
  136.         while(IO.input(self.__Datapin)):
  137.             time.sleep(0.001)
  138.             if( IO.input(self.__Datapin)):
  139.                 IO.setup(self.__Datapin, OUTPUT)
  140.                 IO.output( self.__Datapin, LOW)
  141.                 IO.setup(self.__Datapin, INPUT)
  142.             #endif
  143.         # endwhile            
  144.         IO.setup(self.__Datapin, OUTPUT)
  145.     # end writeByte
  146.    
  147.     def start(self):
  148.         IO.output( self.__Clkpin, HIGH) # send start signal to TM1637
  149.         IO.output( self.__Datapin, HIGH)
  150.         IO.output( self.__Datapin, LOW)
  151.         IO.output( self.__Clkpin, LOW)
  152.     # end start
  153.    
  154.     def stop(self):
  155.         IO.output( self.__Clkpin, LOW)
  156.         IO.output( self.__Datapin, LOW)
  157.         IO.output( self.__Clkpin, HIGH)
  158.         IO.output( self.__Datapin, HIGH)
  159.     # end stop
  160.    
  161.     def coding(self, data):
  162.         if( self.__doublePoint ):
  163.             pointData = 0x80
  164.         else:
  165.             pointData = 0;
  166.        
  167.         if(data == 0x7F):
  168.             data = 0
  169.         else:
  170.             data = HexDigits[data] + pointData;
  171.         return data
  172.     # end coding
  173.    
  174. # end class TM1637
  175.        
  176. Display = TM1637(23,24,BRIGHT_TYPICAL)
  177.  
  178. ## =============================================================
  179. ## =============================================================
  180.  
  181. d_pin_d = 17
  182. clk_pin_d = 18
  183. IO.setup([d_pin_d,clk_pin_d], IO.OUT, initial = 0)
  184. clk = False #Initialisieren der Clock-Variable
  185. brightness = 0x001f
  186.  
  187. def send16bitData(data): #Control und LED-Block Daten
  188.     global clk
  189.     bit = 0x8000
  190.     for b in range(16):
  191.         if (data&bit):
  192.             IO.output(d_pin_d,1)
  193.         else:
  194.             IO.output(d_pin_d,0)
  195.         bit = (bit>>1)
  196.         clk = not clk
  197.         IO.output(clk_pin_d, clk)
  198.  
  199. def sendleds(on, half = 0):
  200.     send16bitData(0x0000)
  201.     for n in range(on):             #Full on
  202.         send16bitData(brightness)
  203.     for n in range(on, on+half):    #between full on and half
  204.         send16bitData(brightness >> 2) #Half brightness
  205.     for n in range(on+half, 12):
  206.         send16bitData(0x000)
  207.     latch()
  208.  
  209. def latch():
  210.     toggle = False
  211.     time.sleep(0.0005)
  212.     for i in range(8):
  213.         IO.output(d_pin_d,toggle)
  214.         toggle = not toggle
  215.     time.sleep(0.0005)
  216.  
  217. def ledbar(nr_on_leds = 0, bar_mode = 0, max_brightness=0x001f):
  218. #nr_on_leds = How many LED's switched on
  219. #bar_mode   = -1 = bar decreasing from max to half brightness
  220. #           = 0  = steady on
  221. #           = 1  = bar increasing from half to max brightness
  222. #   send16bitData(0x0000)
  223.     if (bar_mode == 0):
  224.         sendleds(nr_on_leds)
  225.     elif (bar_mode == 1):
  226.         for i in range(nr_on_leds+1):
  227.             sendleds(i, nr_on_leds-i)
  228.             time.sleep(0.1)
  229.     elif (bar_mode == -1):
  230.         for i in range(nr_on_leds+1):
  231.             sendleds(nr_on_leds-i, i)
  232.             time.sleep(0.1)
  233.  
  234. ledbar()
  235.  
  236. ## =============================================================
  237. ## =============================================================
  238.  
  239. # begin ADC
  240.  
  241. spi = spidev.SpiDev()
  242. spi.open(0,0)
  243.  
  244. def readadc(adcnum):
  245. # read SPI data from MCP3004 chip, 4 possible adc's (0 thru 3)
  246.     if adcnum >3 or adcnum <0:
  247.        return-1
  248.     r = spi.xfer2([1,8+adcnum <<4,0])
  249.     adcout = ((r[1] &3) <<8)+r[2]
  250.     return adcout
  251.  
  252. ## =============================================================
  253. ## =============================================================
  254. def read_temp():
  255.         value_=readadc(0)
  256.         volts_=(value_*3.3)/1024
  257.         temp_ = (volts_-0.5)*100
  258.         return temp_
  259.  
  260. IO.setup(19, IO.OUT)
  261.  
  262.  
  263. try:
  264.         while True:
  265.                 t1 = read_temp()
  266.                 time.sleep(0.01)
  267.                 t2 = read_temp()
  268.                 time.sleep(0.01)
  269.                 t3 = read_temp()
  270.                 temp = (t1 + t2 + t3) / 3
  271.                 t_list = []
  272.                 t_list += str(temp)
  273.                 del t_list[2]
  274.                 del t_list[4:]
  275.                 t_list = [int(i) for i in t_list]
  276.                 Display.Show(t_list)
  277.                 bars = int((temp-18)/(27-18)*10)
  278.                 ledbar(bars, 0)
  279.  
  280.                 Servo = IO.PWM(19, 50)
  281.                 Servo.start(7.5)
  282.                
  283.                 diff_t = temp - t_soll
  284.                 if diff_t > 1.5:
  285.                         Servo.ChangeDutyCycle(2.5)
  286.                 if diff_t == t_soll:
  287.                         Servo.ChangeDutyCycle(7.5)
  288.                 if diff_t < 1.5:
  289.                         Servo.ChangeDutyCycle(12.5)
  290.                 print(temp)
  291.                
  292.                 time.sleep(5)
  293.                
  294. except:
  295.         ledbar(0,0)
  296.         Display.Clear()
  297.         spi.close()
  298.         IO.cleanup()
  299.         Servo.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement