Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import time
  2. import Adafruit_GPIO.SPI as SPI
  3. import Adafruit_MCP3008
  4.  
  5. CLK = 18
  6. MISO = 23
  7. MOSI = 24
  8. CS = 25
  9. #mcp = Adafruit_MCP3008.MCP3008(clk=CLK,cs=CS,miso=MISO,mosi=MOSI)
  10.  
  11. # Hardware SPI configuration:
  12. SPI_PORT = 0
  13. SPI_DEVICE = 0
  14. mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
  15.  
  16. signal_reads = 16
  17. print('Reading MCP3008 values, press Ctrl-C to quit...')
  18. # Print nice channel column headers.
  19. print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
  20. print('-' * 57)
  21. # Main program loop.
  22.  
  23. def get_read():
  24. values = [0]*8
  25. for j in range(signal_reads):
  26. for i in range(8):
  27. values[i] += mcp.read_adc(i)
  28. for i in range(8):
  29. values[i] = values[i] / signal_reads
  30. return values
  31.  
  32. while True:
  33. # Read all the ADC channel values in a list.
  34. values = [0]*8
  35. #for i in range(8):
  36. # # The read_adc function will get the value of the specified channel (0-7).
  37. # values[i] = mcp.read_adc(i)
  38. values = get_read()
  39. # Print the ADC values.
  40. print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
  41. # Pause for half a second.
  42. time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement