Raspberry Pi ® rb-LK-SET Mini PC ARM1176JZFS (1x 700 MHz) 512 MB rb-LK-SET 用户手册
产品代码
rb-LK-SET
sudo apt-get install python-imaging python-imaging-tk python-pip python-dev
git
git
Next, we will install spidev using pip:
sudo pip install spidev
Then we will install WiringPi (the driver for IOs on Raspberry pi, that can be used in C, shell
script or Python, etc):
script or Python, etc):
sudo pip install wiringpi
The interface python code for MCP3004 is as following:
Import spidev
Import time
spi = spidev.SpiDev()
spi.open(0,0)
def readadc(adcnum):
# read SPI data from MCP3004 chip, 4 possible adc's (0 thru 3)
ifadcnum >3oradcnum <0:
return-1
r = spi.xfer2([1,8+adcnum <<4,0])
adcout = ((r[1] &3) <<8)+r[2]
Returnadcout
whileTrue:
value=readadc(0)
volts=(value*3.3)/1024
print("%4d/1023 => %5.3f V" % (value, volts))
time.sleep(0.5)
Import time
spi = spidev.SpiDev()
spi.open(0,0)
def readadc(adcnum):
# read SPI data from MCP3004 chip, 4 possible adc's (0 thru 3)
ifadcnum >3oradcnum <0:
return-1
r = spi.xfer2([1,8+adcnum <<4,0])
adcout = ((r[1] &3) <<8)+r[2]
Returnadcout
whileTrue:
value=readadc(0)
volts=(value*3.3)/1024
print("%4d/1023 => %5.3f V" % (value, volts))
time.sleep(0.5)
Save the above code to a file named linker_adc.py, and enter into the directory to execute:
$python ./linker_adc.py
The following are the results:
91/1023 =>0.293V
93/1023 =>0.300V
94/1023 =>0.303V
93/1023 =>0.300V
94/1023 =>0.303V