forked from adafruit/Adafruit-Raspberry-Pi-Python-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_MCP4725.py
executable file
·35 lines (29 loc) · 1.01 KB
/
Adafruit_MCP4725.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
#!/usr/bin/python
from Adafruit_I2C import Adafruit_I2C
# ============================================================================
# Adafruit MCP4725 12-Bit DAC
# ============================================================================
class MCP4725 :
i2c = None
# Registers
__REG_WRITEDAC = 0x40
__REG_WRITEDACEEPROM = 0x60
# Constructor
def __init__(self, address=0x62, debug=False):
self.i2c = Adafruit_I2C(address)
self.address = address
self.debug = debug
def setVoltage(self, voltage, persist=False):
"Sets the output voltage to the specified value"
if (voltage > 4095):
voltage = 4095
if (voltage < 0):
voltage = 0
if (self.debug):
print "Setting voltage to %04d" % voltage
# Value needs to be left-shifted four bytes for the MCP4725
bytes = [(voltage >> 4) & 0xFF, (voltage << 4) & 0xFF]
if (persist):
self.i2c.writeList(self.__REG_WRITEDACEEPROM, bytes)
else:
self.i2c.writeList(self.__REG_WRITEDAC, bytes)