-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADCDevice.py
44 lines (36 loc) · 1.48 KB
/
ADCDevice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import smbus
class ADCDevice(object):
def __init__(self):
self.cmd = 0
self.address = 0
self.bus=smbus.SMBus(1)
# print("ADCDevice init")
def detectI2C(self,addr):
try:
self.bus.write_byte(addr,0)
print("Found device in address 0x%x"%(addr))
return True
except:
print("Not found device in address 0x%x"%(addr))
return False
def close(self):
self.bus.close()
class PCF8591(ADCDevice):
def __init__(self):
super(PCF8591, self).__init__()
self.cmd = 0x40 # The default command for PCF8591 is 0x40.
self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module.
def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3
value = self.bus.read_byte_data(self.address, self.cmd+chn)
value = self.bus.read_byte_data(self.address, self.cmd+chn)
return value
def analogWrite(self,value): # write DAC value
self.bus.write_byte_data(address,cmd,value)
class ADS7830(ADCDevice):
def __init__(self):
super(ADS7830, self).__init__()
self.cmd = 0x84
self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module.
def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7
value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4))
return value