Skip to content

Commit ee8fca1

Browse files
committed
Update
Update ADC.py and Datasheet
1 parent dad7d3d commit ee8fca1

File tree

2 files changed

+79
-36
lines changed

2 files changed

+79
-36
lines changed

Code/Server/ADC.py

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,107 @@
1-
#!/usr/bin/env python3
2-
#############################################################################
3-
# Filename : ADC.py
4-
# Description : ADC and DAC
5-
# Author : freenove
6-
# modification: 2018/09/15
7-
########################################################################
81
import smbus
92
import time
103
class Adc:
114
def __init__(self):
12-
self.address = 0x48 #default address of PCF8591
13-
self.bus=smbus.SMBus(1)
14-
self.cmd=0x40 #command
15-
16-
def analogRead(self,chn):#read ADC value,chn:0,1,2,3
5+
# Get I2C bus
6+
self.bus = smbus.SMBus(1)
7+
# I2C address of the device
8+
self.ADDRESS = 0x48
9+
# PCF8591 Command
10+
self.PCF8591_CMD =0x40 #Command
11+
# ADS7830 Command Set
12+
self.ADS7830_CMD_SD_SINGLE = 0x84 # Single-Ended Inputs
13+
self.ADS7830_CMD_SNGL_CHANNEL_0 = 0x00 # +IN = CH0, -IN = GND
14+
self.ADS7830_CMD_SNGL_CHANNEL_1 = 0x40 # +IN = CH1, -IN = GND
15+
self.ADS7830_CMD_SNGL_CHANNEL_2 = 0x10 # +IN = CH2, -IN = GND
16+
self.ADS7830_CMD_SNGL_CHANNEL_3 = 0x50 # +IN = CH3, -IN = GND
17+
self.ADS7830_CMD_SNGL_CHANNEL_4 = 0x20 # +IN = CH4, -IN = GND
18+
self.ADS7830_CMD_SNGL_CHANNEL_5 = 0x60 # +IN = CH5, -IN = GND
19+
self.ADS7830_CMD_SNGL_CHANNEL_6 = 0x30 # +IN = CH6, -IN = GND
20+
self.ADS7830_CMD_SNGL_CHANNEL_7 = 0x70 # +IN = CH7, -IN = GND
21+
self.ADS7830_CMD_PD_POWER_DOWN = 0x00 # Power Down Between A/D Converter Conversions
22+
self.ADS7830_CMD_PD_REF_OFF = 0x04 # Internal Reference OFF and A/D Converter ON
23+
self.ADS7830_CMD_PD_REF_ON = 0x08 # Internal Reference ON and A/D Converter OFF
24+
self.ADS7830_CMD_PD_REF_ON_AD_ON = 0x0C # Internal Reference ON and A/D Converter ON
25+
for i in range(3):
26+
aa=self.bus.read_byte_data(self.ADDRESS,0xf4)
27+
if aa < 150:
28+
self.Index="PCF8591"
29+
else:
30+
self.Index="ADS7830"
31+
def analogReadPCF8591(self,chn):#PCF8591 read ADC value,chn:0,1,2,3
1732
value=[0,0,0,0,0,0,0,0,0]
1833
for i in range(9):
19-
value[i] = self.bus.read_byte_data(self.address,self.cmd+chn)
34+
value[i] = self.bus.read_byte_data(self.ADDRESS,self.PCF8591_CMD+chn)
2035
value=sorted(value)
21-
return value[4]
22-
def analogWrite(self,value):#write DAC value
23-
self.bus.write_byte_data(address,cmd,value)
24-
def loop(self):
25-
while True:
26-
self.value = self.analogRead(2) #read the ADC value of channel 0,1,2,3
27-
#analogWrite(value) #write the DAC value
28-
self.voltage = self.value / 256.0 * 3.3 #calculate the voltage value
29-
print ('ADC Value : %d, Voltage : %.2f'%(self.value,self.voltage))
30-
time.sleep(0.01)
31-
def recvADC(self,channel):
36+
return value[4]
37+
38+
def analogWritePCF8591(self,value):#PCF8591 write DAC value
39+
self.bus.write_byte_data(self.ADDRESS,cmd,value)
40+
41+
def recvPCF8591(self,channel):#PCF8591 write DAC value
42+
while(1):
43+
value1 = self.analogReadPCF8591(channel) #read the ADC value of channel 0,1,2,
44+
value2 = self.analogReadPCF8591(channel)
45+
if value1==value2:
46+
break;
47+
voltage = value1 / 256.0 * 3.3 #calculate the voltage value
48+
voltage = round(voltage,2)
49+
return voltage
50+
def recvADS7830(self,channel):
51+
"""Select the Command data from the given provided value above"""
52+
if channel == 0:
53+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_0)
54+
elif channel == 1:
55+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_1)
56+
elif channel == 2:
57+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_2)
58+
elif channel == 3:
59+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_3)
60+
elif channel == 4:
61+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_4)
62+
elif channel == 5:
63+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_5)
64+
elif channel == 6:
65+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_6)
66+
elif channel == 7:
67+
COMMAND_SET = (self.ADS7830_CMD_SD_SINGLE | self.ADS7830_CMD_SNGL_CHANNEL_7)
68+
69+
self.bus.write_byte(self.ADDRESS, COMMAND_SET)
3270
while(1):
33-
self.value = self.analogRead(channel) #read the ADC value of channel 0,1,2,
34-
self.value1 = self.analogRead(channel)
35-
if self.value==self.value1:
71+
value1 = self.bus.read_byte(self.ADDRESS)
72+
value2 = self.bus.read_byte(self.ADDRESS)
73+
if value1==value2:
3674
break;
37-
self.voltage = self.value / 256.0 * 3.3 #calculate the voltage value
38-
self.voltage = round(self.voltage,2)
39-
return self.voltage
40-
def destroy():
75+
voltage = value1 / 256.0 * 3.3 #calculate the voltage value
76+
voltage = round(voltage,2)
77+
return voltage
78+
79+
def recvADC(self,channel):
80+
if self.Index=="PCF8591":
81+
data=self.recvPCF8591(channel)
82+
elif self.Index=="ADS7830":
83+
data=self.recvADS7830(channel)
84+
return data
85+
def i2cClose(self):
4186
self.bus.close()
4287

4388
def loop():
89+
adc=Adc()
4490
while True:
45-
adc=Adc()
4691
Left_IDR=adc.recvADC(0)
4792
print (Left_IDR)
4893
Right_IDR=adc.recvADC(1)
4994
print (Right_IDR)
5095
Power=adc.recvADC(2)*3
5196
print (Power)
5297
time.sleep(1)
53-
#print '----'
54-
98+
print ('----')
5599
def destroy():
56100
pass
57-
58101
# Main program logic follows:
59102
if __name__ == '__main__':
60103
print ('Program is starting ... ')
61104
try:
62105
loop()
63106
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
64-
destroy()
107+
destroy()

Datasheet/ADS7830.pdf

249 KB
Binary file not shown.

0 commit comments

Comments
 (0)