Update
Update ADC.py and Datasheet
This commit is contained in:
parent
dad7d3d8de
commit
ee8fca1e23
@ -1,48 +1,93 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
#############################################################################
|
|
||||||
# Filename : ADC.py
|
|
||||||
# Description : ADC and DAC
|
|
||||||
# Author : freenove
|
|
||||||
# modification: 2018/09/15
|
|
||||||
########################################################################
|
|
||||||
import smbus
|
import smbus
|
||||||
import time
|
import time
|
||||||
class Adc:
|
class Adc:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.address = 0x48 #default address of PCF8591
|
# Get I2C bus
|
||||||
self.bus=smbus.SMBus(1)
|
self.bus = smbus.SMBus(1)
|
||||||
self.cmd=0x40 #command
|
# I2C address of the device
|
||||||
|
self.ADDRESS = 0x48
|
||||||
def analogRead(self,chn):#read ADC value,chn:0,1,2,3
|
# PCF8591 Command
|
||||||
|
self.PCF8591_CMD =0x40 #Command
|
||||||
|
# ADS7830 Command Set
|
||||||
|
self.ADS7830_CMD_SD_SINGLE = 0x84 # Single-Ended Inputs
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_0 = 0x00 # +IN = CH0, -IN = GND
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_1 = 0x40 # +IN = CH1, -IN = GND
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_2 = 0x10 # +IN = CH2, -IN = GND
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_3 = 0x50 # +IN = CH3, -IN = GND
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_4 = 0x20 # +IN = CH4, -IN = GND
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_5 = 0x60 # +IN = CH5, -IN = GND
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_6 = 0x30 # +IN = CH6, -IN = GND
|
||||||
|
self.ADS7830_CMD_SNGL_CHANNEL_7 = 0x70 # +IN = CH7, -IN = GND
|
||||||
|
self.ADS7830_CMD_PD_POWER_DOWN = 0x00 # Power Down Between A/D Converter Conversions
|
||||||
|
self.ADS7830_CMD_PD_REF_OFF = 0x04 # Internal Reference OFF and A/D Converter ON
|
||||||
|
self.ADS7830_CMD_PD_REF_ON = 0x08 # Internal Reference ON and A/D Converter OFF
|
||||||
|
self.ADS7830_CMD_PD_REF_ON_AD_ON = 0x0C # Internal Reference ON and A/D Converter ON
|
||||||
|
for i in range(3):
|
||||||
|
aa=self.bus.read_byte_data(self.ADDRESS,0xf4)
|
||||||
|
if aa < 150:
|
||||||
|
self.Index="PCF8591"
|
||||||
|
else:
|
||||||
|
self.Index="ADS7830"
|
||||||
|
def analogReadPCF8591(self,chn):#PCF8591 read ADC value,chn:0,1,2,3
|
||||||
value=[0,0,0,0,0,0,0,0,0]
|
value=[0,0,0,0,0,0,0,0,0]
|
||||||
for i in range(9):
|
for i in range(9):
|
||||||
value[i] = self.bus.read_byte_data(self.address,self.cmd+chn)
|
value[i] = self.bus.read_byte_data(self.ADDRESS,self.PCF8591_CMD+chn)
|
||||||
value=sorted(value)
|
value=sorted(value)
|
||||||
return value[4]
|
return value[4]
|
||||||
def analogWrite(self,value):#write DAC value
|
|
||||||
self.bus.write_byte_data(address,cmd,value)
|
def analogWritePCF8591(self,value):#PCF8591 write DAC value
|
||||||
def loop(self):
|
self.bus.write_byte_data(self.ADDRESS,cmd,value)
|
||||||
while True:
|
|
||||||
self.value = self.analogRead(2) #read the ADC value of channel 0,1,2,3
|
def recvPCF8591(self,channel):#PCF8591 write DAC value
|
||||||
#analogWrite(value) #write the DAC value
|
|
||||||
self.voltage = self.value / 256.0 * 3.3 #calculate the voltage value
|
|
||||||
print ('ADC Value : %d, Voltage : %.2f'%(self.value,self.voltage))
|
|
||||||
time.sleep(0.01)
|
|
||||||
def recvADC(self,channel):
|
|
||||||
while(1):
|
while(1):
|
||||||
self.value = self.analogRead(channel) #read the ADC value of channel 0,1,2,
|
value1 = self.analogReadPCF8591(channel) #read the ADC value of channel 0,1,2,
|
||||||
self.value1 = self.analogRead(channel)
|
value2 = self.analogReadPCF8591(channel)
|
||||||
if self.value==self.value1:
|
if value1==value2:
|
||||||
break;
|
break;
|
||||||
self.voltage = self.value / 256.0 * 3.3 #calculate the voltage value
|
voltage = value1 / 256.0 * 3.3 #calculate the voltage value
|
||||||
self.voltage = round(self.voltage,2)
|
voltage = round(voltage,2)
|
||||||
return self.voltage
|
return voltage
|
||||||
def destroy():
|
def recvADS7830(self,channel):
|
||||||
|
"""Select the Command data from the given provided value above"""
|
||||||
|
if channel == 0:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_0)
|
||||||
|
elif channel == 1:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_1)
|
||||||
|
elif channel == 2:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_2)
|
||||||
|
elif channel == 3:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_3)
|
||||||
|
elif channel == 4:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_4)
|
||||||
|
elif channel == 5:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_5)
|
||||||
|
elif channel == 6:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_6)
|
||||||
|
elif channel == 7:
|
||||||
|
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_7)
|
||||||
|
|
||||||
|
self.bus.write_byte(self.ADDRESS, COMMAND_SET)
|
||||||
|
while(1):
|
||||||
|
value1 = self.bus.read_byte(self.ADDRESS)
|
||||||
|
value2 = self.bus.read_byte(self.ADDRESS)
|
||||||
|
if value1==value2:
|
||||||
|
break;
|
||||||
|
voltage = value1 / 256.0 * 3.3 #calculate the voltage value
|
||||||
|
voltage = round(voltage,2)
|
||||||
|
return voltage
|
||||||
|
|
||||||
|
def recvADC(self,channel):
|
||||||
|
if self.Index=="PCF8591":
|
||||||
|
data=self.recvPCF8591(channel)
|
||||||
|
elif self.Index=="ADS7830":
|
||||||
|
data=self.recvADS7830(channel)
|
||||||
|
return data
|
||||||
|
def i2cClose(self):
|
||||||
self.bus.close()
|
self.bus.close()
|
||||||
|
|
||||||
def loop():
|
def loop():
|
||||||
while True:
|
|
||||||
adc=Adc()
|
adc=Adc()
|
||||||
|
while True:
|
||||||
Left_IDR=adc.recvADC(0)
|
Left_IDR=adc.recvADC(0)
|
||||||
print (Left_IDR)
|
print (Left_IDR)
|
||||||
Right_IDR=adc.recvADC(1)
|
Right_IDR=adc.recvADC(1)
|
||||||
@ -50,11 +95,9 @@ def loop():
|
|||||||
Power=adc.recvADC(2)*3
|
Power=adc.recvADC(2)*3
|
||||||
print (Power)
|
print (Power)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
#print '----'
|
print ('----')
|
||||||
|
|
||||||
def destroy():
|
def destroy():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Main program logic follows:
|
# Main program logic follows:
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print ('Program is starting ... ')
|
print ('Program is starting ... ')
|
||||||
|
BIN
Datasheet/ADS7830.pdf
Normal file
BIN
Datasheet/ADS7830.pdf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user