-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADC3008.py
executable file
·39 lines (33 loc) · 1.33 KB
/
ADC3008.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.SPI as SPI
import spidev
import time
import os
# Open SPI bus : questa parte va nel main per dichiarare la spi che poi
# l'adc ha come ingresso
# spi = spidev.SpiDev()
# spi.open(0,0)
# spi.max_speed_hz=1000000
class MCP3008 :
def __init__ (self, clk=None, cs=None, miso=None, mosi=None, spi=None, gpio=None):
self._spi = None
#Handle hardware SPI
if spi is not None:
self._spi = spi
elif clk is not None and cs is not None and miso is not None and mosi is not None:
# Default to platform GPIO if not provided.
if gpio is None:
gpio = GPIO.get_platform_gpio()
self._spi = SPI.BitBang(gpio, clk, mosi, miso, cs)
else:
raise ValueError('Must specify either spi for for hardware SPI or clk, cs, miso, and mosi for software SPI!')
self._spi.set_clock_hz(1000000)
self._spi.set_mode(0)
self._spi.set_bit_order(SPI.MSBFIRST)
#questa inizializzazione è necessaria nel caso in cui nel main non vengano passate le informazioni necessarie
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(self, channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data