ADC0832.py
#!/usr/bin/env python
#
# This is a program for all ADC Module. It
# convert analog singnal to digital signal.
#
# This program is most analog signal modules'
# dependency. Use it like this:
# `import ADC0832`
# `sig = ADC0832.getResult(chn)`
#
# *'chn' should be 0 or 1 represent for ch0 or ch1
# on ADC0832
#
# ACD1302 Pi
# CS ---------------- Pin 11
# CLK --------------- Pin 12
# DI ---------------- Pin 13
# VCC ----------------- 3.3V
# GND ------------------ GND
#
import RPi.GPIO as GPIO
import wiringpi
import time
ADC_CS = 17 #11
ADC_CLK = 18 #12
ADC_DIO = 27 #13
# using default pins for backwards compatibility
def setup(cs=17,clk=18,dio=27): #11,12,13
global ADC_CS, ADC_CLK, ADC_DIO
ADC_CS=cs
ADC_CLK=clk
ADC_DIO=dio
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Number GPIOs by its physical location
GPIO.setup(ADC_CS, GPIO.OUT) # Set pins' mode is output
GPIO.setup(ADC_CLK, GPIO.OUT) # Set pins' mode is output
def destroy():
GPIO.cleanup()
# using channel = 0 as default for backwards compatibility
def getResult(channel=0): # Get ADC result, input channal
GPIO.setup(ADC_DIO, GPIO.OUT)
GPIO.output(ADC_CS, 0)
GPIO.output(ADC_CLK, 0)
GPIO.output(ADC_DIO, 1); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 1); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 0)
GPIO.output(ADC_DIO, 1); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 1); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 0)
GPIO.output(ADC_DIO, channel); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 1)
GPIO.output(ADC_DIO, 1); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 0)
GPIO.output(ADC_DIO, 1); wiringpi.delayMicroseconds(2)
dat1 = 0
for i in range(0, 8):
GPIO.output(ADC_CLK, 1); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 0); wiringpi.delayMicroseconds(2)
GPIO.setup(ADC_DIO, GPIO.IN)
dat1 = dat1 << 1 | GPIO.input(ADC_DIO)
dat2 = 0
for i in range(0, 8):
dat2 = dat2 | GPIO.input(ADC_DIO) << i
GPIO.output(ADC_CLK, 1); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CLK, 0); wiringpi.delayMicroseconds(2)
GPIO.output(ADC_CS, 1)
GPIO.setup(ADC_DIO, GPIO.OUT)
if dat1 == dat2:
return dat1
else:
return 0
def getResult1():
return getResult(1)
def loop():
while True:
res0 = getResult(0)
res1 = getResult(1)
print ('res0 = %d, res1 = %d' % (res0,res1))
time.sleep(0.4)
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
measure_plt.py
import ADC0832
import time
import numpy as np
import matplotlib.pyplot as plt
def init():
ADC0832.setup()
def loop():
n=0
i=0
y=[]
x=[]
t=time.process_time()
while n<10:
digitalVal=ADC0832.getResult()
y.append(3.3*float(digitalVal)/255)
x.append(time.process_time())
n=n+1
#plt.axis([0.1,0.2,0.4,0.8])
#前后两组参数分别表示x、y轴范围
#等价于plt.xlim(0.1,0.2) plt.ylim(0.4,0.8)
plt.plot(x,y,'-o')
while i<10:
x1="%.3f"%x[i]
y1="%.2f"%y[i]
text='{'+str(x1)+','+str(y1)+'}'
plt.text(x[i],y[i],text)
i=i+1
plt.show()
if __name__=='__main__':
init()
loop()
ADC0832.destroy()
print("The End")
measure_sin.py
import ADC0832
import time
import numpy as np
import matplotlib.pyplot as plt
def init():
ADC0832.setup()
def loop():
n=0
t=0
y=[]
x=[]
while n<1000:
digitalVal=ADC0832.getResult()
y.append(3.3*float(digitalVal)/255)
x.append(time.perf_counter())
# or time.process_time()
n=n+1
t=time.perf_counter()-t
# or time.process_time()
plt.plot(x,y)
plt.show()
if __name__=='__main__':
init()
loop()
ADC0832.destroy()
print("The End")
measure_V.py
import ADC0832
import time
def init():
ADC0832.setup()
#def setup(cs=11,clk=12,dio=13):
# global ADC_CS, ADC_CLK, ADC_DIO
# ADC_CS=cs
# ADC_CLK=clk
# ADC_DIO=dio
# GPIO.setwarnings(False)
# GPIO.setmode(GPIO.BOARD)
# GPIO.setup(ADC_CS, GPIO.OUT)
# GPIO.setup(ADC_CLK, GPIO.OUT)
def loop():
while True:
digitalVal=ADC0832.getResult()
# Get ADC result, input channal
# getResult()函数代码实现ADC0832工作原理
# 得到0~255之间的一个数
# 详细参见ADC0832.py
print(3.3*float(digitalVal)/255)
# 转换为电压量
time.sleep(0.2)
if __name__=='__main__':
init()
try:
loop()
except KeyboardInterrupt:
ADC0832.destroy()#调用GPIO.cleanup
print("The end!")