Adafruit ADC和Python属性错误

时间:2017-05-19 03:22:35

标签: python raspberry-pi adc

我是Python的新手,目前在我的Pi 3 mod b上开展一个项目。我使用Adafruit ADC1015转换模拟信号。然而,即使我有代码来获得一些电压测量,我得到一个错误" AttributeError:' int'对象没有属性" readADCSingleEnded'"。

为了解释这一点,我试图运行的python脚本如下:

#!/usr/bin/python
import time, signal, sys
from Adafruit_ADS1x15 import ADS1x15

def signal_handler(signal, frame):
    print 'You pressed Ctrl+C!'
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

ADS1015 = 0x00  
ADS1115 = 0x01  

gain = 4096  # +/- 4.096V


sps = 250  # 250 samples per second

# Initialise the ADC using the default mode (use default I2C address)
# Set this to ADS1015 or ADS1115 depending on the ADC you are using!
adc = ADS1015(ic=ADS1015)

# Read channel 0 in single-ended mode using the settings above
volts=adc.readADCSingleEnded(0, gain, sps) / 1000

# To read channel 3 in single-ended mode, +/- 1.024V, 860 sps use:
# volts = adc.readADCSingleEnded(3, 1024, 860)

print "%.6f" % (volts)

" ADS1x15"我们导入的文件包含以下与错误相关的代码:

# Constructor
def __init__(self, address=0x48, ic=__IC_ADS1015, debug=False):
# Depending on if you have an old or a new Raspberry Pi, you
# may need to change the I2C bus.  Older Pis use SMBus 0,
# whereas new Pis use SMBus 1.  If you see an error like:
# 'Error accessing 0x48: Check your I2C address '
# change the SMBus number in the initializer below!
self.i2c = Adafruit_I2C(address)
self.address = address
self.debug = debug
# Make sure the IC specified is valid
if ((ic < self.__IC_ADS1015) | (ic > self.__IC_ADS1115)):
  if (self.debug):
    print "ADS1x15: Invalid IC specfied: %h" % ic
  return -1
else:
  self.ic = ic

# Set pga value, so that getLastConversionResult() can use it,
# any function that accepts a pga value must update this.
self.pga = 6144    


 def readADCSingleEnded(self, channel=0, pga=6144, sps=250):
"Gets a single-ended ADC reading from the specified channel in mV. \
The sample rate for this mode (single-shot) can be used to lower the noise \
(low sps) or to lower the power consumption (high sps) by duty cycling, \
see datasheet page 14 for more info. \
The pga must be given in mV, see page 13 for the supported values."

# With invalid channel return -1
if (channel > 3):
  if (self.debug):
    print "ADS1x15: Invalid channel specified: %d" % channel
  return -1

# Disable comparator, Non-latching, Alert/Rdy active low
# traditional comparator, single-shot mode
config = self.__ADS1015_REG_CONFIG_CQUE_NONE    | \
         self.__ADS1015_REG_CONFIG_CLAT_NONLAT  | \
         self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
         self.__ADS1015_REG_CONFIG_CMODE_TRAD   | \
         self.__ADS1015_REG_CONFIG_MODE_SINGLE    

# Set sample per seconds, defaults to 250sps
# If sps is in the dictionary (defined in init) it returns the value of the constant
# othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
if (self.ic == self.__IC_ADS1015):
  config |= self.spsADS1015.setdefault(sps, self.__ADS1015_REG_CONFIG_DR_1600SPS)
else:
  if ( (sps not in self.spsADS1115) & self.debug):    
print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
  config |= self.spsADS1115.setdefault(sps, self.__ADS1115_REG_CONFIG_DR_250SPS)

# Set PGA/voltage range, defaults to +-6.144V
if ( (pga not in self.pgaADS1x15) & self.debug):      
  print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
config |= self.pgaADS1x15.setdefault(pga, self.__ADS1015_REG_CONFIG_PGA_6_144V)
self.pga = pga

# Set the channel to be converted
if channel == 3:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_3
elif channel == 2:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_2
elif channel == 1:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_1
else:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_0

# Set 'start single-conversion' bit
config |= self.__ADS1015_REG_CONFIG_OS_SINGLE

# Write config register to the ADC
bytes = [(config >> 8) & 0xFF, config & 0xFF]
self.i2c.writeList(self.__ADS1015_REG_POINTER_CONFIG, bytes)

# Wait for the ADC conversion to complete
# The minimum delay depends on the sps: delay >= 1/sps
# We add 0.1ms to be sure
delay = 1.0/sps+0.0001
time.sleep(delay)

# Read the conversion results
result = self.i2c.readList(self.__ADS1015_REG_POINTER_CONVERT, 2)
if (self.ic == self.__IC_ADS1015):
    # Shift right 4 bits for the 12-bit ADS1015 and convert to mV
    return ( ((result[0] << 8) | (result[1] & 0xFF)) >> 4 )*pga/2048.0
else:
# Return a mV value for the ADS1115
# (Take signed values into account as well)
val = (result[0] << 8) | (result[1])
if val > 0x7FFF:
  return (val - 0xFFFF)*pga/32768.0
else:
  return ( (result[0] << 8) | (result[1]) )*pga/32768.0

我相信这会很平常,因为它是与ADC相关的一部分,但即使我经常尝试,我也无法解决这个问题。

1 个答案:

答案 0 :(得分:1)

找到它。线

adc = ADS1015(ic=ADS1015)

应该是

adc = ADS1x15(ic=ADS1015)