SHARE
TWEET

Untitled

a guest Mar 26th, 2020 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pylint: skip-file
  2. from RPi import GPIO
  3. # from mcp import Mcp
  4. import time
  5. import spidev
  6.  
  7. spi = spidev.SpiDev()
  8. spi.open(0, 0)  # Bus SPI0, slave op CE 0
  9. # Bus = het nummer van de SPI-bus
  10. # Device = het nummer van de SS/CE/CS-lijn, standaard heb je op de RPi pins 8 en 9 (BCM) ter beschikking als CE0 en CE1
  11. spi.max_speed_hz = 10 ** 5 # 100 kHz
  12.  
  13. pwm_pin = 21
  14. pwm_waarde = 20  # 50hz 20ms periode
  15.  
  16. GPIO.setmode(GPIO.BCM)
  17. GPIO.setwarnings(False)
  18. GPIO.setup(pwm_pin, GPIO.OUT)
  19. p = GPIO.PWM(pwm_pin, 50)
  20. p.start(6)
  21.  
  22. # byte1 = 0b00000000
  23. # byte2 = 0b00000000
  24. # byte3 = 0b00000000
  25.  
  26. # bytes_out = [byte1, byte2, byte3]
  27.  
  28. def writebit(byte):
  29.     pass
  30.  
  31. def ack():
  32.     pass
  33.  
  34. def draai_motor():
  35.     # Servo motor draaien
  36.     while True:
  37.         # 0 graden (neutraal)
  38.         p.ChangeDutyCycle(6)
  39.         time.sleep(1)
  40.    
  41.         # -90 graden (rechts)
  42.         p.ChangeDutyCycle(2.5)
  43.         time.sleep(1)
  44.    
  45.         # 0 graden (neutraal)
  46.         p.ChangeDutyCycle(6)
  47.         time.sleep(1)
  48.    
  49.         # 90 graden (links)
  50.         p.ChangeDutyCycle(11)
  51.         time.sleep(1)
  52.  
  53. def read_pot():
  54.  
  55.     while True:
  56.        
  57.  
  58.         channeldata = read_spi(0)
  59.         print("Waarde = {}".format(channeldata))
  60.         time.sleep(.1)
  61.    
  62. def read_spi(channel):
  63.   spidata = spi.xfer2([1,(8+channel)<<4,0])
  64.   return ((spidata[1] & 3) << 8) + spidata[2]
  65.  
  66. def setup():
  67.  
  68.    
  69.     bytes_out = []
  70.     counter = 0
  71.     while counter < 4:
  72.         counter += 1
  73.         bytes_in = spi.readbytes(counter)  # leest n bytes en geeft die terug in een `list`
  74.         bytes_out.append(bytes_in)
  75.  
  76.     spi.writebytes(bytes_out[0])  # verstuurt bytes uit de `list` bytes_out
  77.     # verstuurt de bytes in `bytes_out` en ontvangt er evenveel in `bytes_in`.
  78.     bytes_in = spi.xfer(bytes_out[1])
  79.     # tussen elke byte wordt SS/CE/CS kort gedeactiveerd.
  80.     # idem, maar tussen de bytes in blijft SS/CE/CS actief
  81.     bytes_in = spi.xfer2(bytes_out[2])
  82.  
  83. if __name__ == "__main__":
  84.     try:
  85.         setup()
  86.        
  87.         read_pot()
  88.        
  89.  
  90.     except KeyboardInterrupt as e:
  91.         print(e)
  92.  
  93.     finally:
  94.         spi.close()
  95.         p.stop()
  96.         GPIO.cleanup()
  97.         print("Finish")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top