j0h

spidev

j0h
Dec 30th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Control AD5206 digital potentiometer using SPI
  3. # remind me to set geany from tabs to spaces
  4. import spidev
  5. import time
  6. import RPi.GPIO as GPIO
  7.  
  8. # Setup the SPI connection
  9. spi = spidev.SpiDev()
  10. spi.open(0, 0)  # Bus 0, device 0 (Chip Select 0)
  11. spi.max_speed_hz = 50000  # Set the SPI speed (adjust as needed)
  12.  
  13. # IO Chip Select pins
  14. cs1 = 23
  15. cs2 = 24
  16. cs3 = 25
  17.  
  18. # Constants
  19. MAX = 255
  20. MIN = 0
  21. MID = 127
  22.  
  23. # Setup GPIO for Chip Select (CS)
  24. GPIO.setmode(GPIO.BCM)
  25. GPIO.setwarnings(False)
  26. GPIO.setup(cs1, GPIO.OUT)  # Chip Select for AD5206 #1
  27. GPIO.setup(cs2, GPIO.OUT)  # Chip Select for AD5206 #2
  28. GPIO.setup(cs3, GPIO.OUT)  # Chip Select for AD5206 #3
  29.  
  30. # Function to write data to a specific channel of an AD5206
  31. def set_pot(cs_pin, channel, value):
  32.     if channel < 0 or channel > 5:
  33.         raise ValueError("Channel must be between 0 and 5.")
  34.     if value < 0 or value > 255:
  35.         raise ValueError("Value must be between 0 and 255.")
  36.    
  37.     GPIO.output(cs_pin, GPIO.LOW)  # Enable the chip by pulling CS low
  38.     # Command byte: Bits 2:0 for channel (0-5), others 0
  39.     command_byte = channel & 0x07  # Ensure only the last 3 bits are used
  40.     data = [command_byte, value]  # Command byte followed by value
  41.     spi.xfer2(data)  # Send the data to the AD5206
  42.     GPIO.output(cs_pin, GPIO.HIGH)  # Disable the chip by pulling CS high
  43.  
  44. print("AD5206 control program running. Press Ctrl+C to exit.")
  45. print("I forget is 0  min brightness or max?")
  46.  
  47. # Enable all the devices for write
  48. GPIO.output(cs1, GPIO.LOW)  # Enable the chip by pulling CS low
  49. GPIO.output(cs2, GPIO.LOW)  # Enable the chip by pulling CS low
  50. GPIO.output(cs3, GPIO.LOW)  # Enable the chip by pulling CS low
  51. # Initialize all channels on all chips to minimum resistance
  52. for channel in range(6):
  53.     set_pot(cs1, channel, MIN)
  54.     set_pot(cs2, channel, MIN)
  55.     set_pot(cs3, channel, MIN)
  56. # disable all the devices
  57. GPIO.output(cs1, GPIO.HIGH)  # disable the chip by pulling CS high 
  58. GPIO.output(cs2, GPIO.HIGH)  # disable the chip by pulling CS high 
  59. GPIO.output(cs3, GPIO.HIGH)  # disable the chip by pulling CS high
  60.  
  61.  
  62. # Test: Set channel 3 on each chip to MID resistance
  63. #set_pot(cs1, 3, MID)
  64. #set_pot(cs2, 3, MID)
  65. #set_pot(cs3, 3, MID)
  66.  
  67. # Cleanup GPIO on exit
  68. def cleanup():
  69.     GPIO.cleanup()
  70.     spi.close()
  71.  
  72. # Ensure GPIO cleanup on exit
  73. try:
  74.    
  75.     while True:      
  76.         # Example: Gradually increase channel 0 resistance on cs1
  77.         for value in range(MIN, MAX + 1, 10):
  78.             for n in range(6):
  79.                 set_pot(cs1, n, value)
  80.                # print(f{value})
  81.             time.sleep(0.1)
  82.         for value in range(MIN, MAX + 1, 10):
  83.             for n in range(6):
  84.                 set_pot(cs2, n, value)
  85.                 #print(f{value})
  86.             time.sleep(0.1)
  87.         for value in range(MIN, MAX + 1, 10):
  88.             for n in range(6):
  89.                 set_pot(cs3, n, value)
  90.                 #print(f{value})
  91.             time.sleep(0.1)                  
  92.            
  93. except KeyboardInterrupt:
  94.     print("\nExiting program.")
  95. finally:
  96.     cleanup()
  97.  
Add Comment
Please, Sign In to add comment