Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 0c82bd7

Browse files
author
K. Townsend
committed
First commit
1 parent 618a13a commit 0c82bd7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Adafruit_MCP4725/Adafruit_MCP4725.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/python
2+
3+
from Adafruit_I2C import Adafruit_I2C
4+
5+
# ============================================================================
6+
# Adafruit MCP4725 12-Bit DAC
7+
# ============================================================================
8+
9+
class MCP4725 :
10+
i2c = None
11+
12+
# Registers
13+
__REG_WRITEDAC = 0x40
14+
__REG_WRITEDACEEPROM = 0x60
15+
16+
# Constructor
17+
def __init__(self, address=0x62, debug=False):
18+
self.i2c = Adafruit_I2C(address)
19+
self.address = address
20+
self.debug = debug
21+
22+
def setVoltage(self, voltage, persist=False):
23+
"Sets the output voltage to the specified value"
24+
if (voltage > 4095):
25+
voltage = 4095
26+
if (self.debug):
27+
print "Setting voltage to %04d" % voltage
28+
# Value needs to be left-shifted four bytes for the MCP4725
29+
voltage <<= 4
30+
if (persist==True):
31+
self.i2c.write16(self.__REG_WRITEDACEEPROM, self.i2c.reverseByteOrder(voltage))
32+
else:
33+
self.i2c.write16(self.__REG_WRITEDAC, self.i2c.reverseByteOrder(voltage))
34+
35+
# Initialise a new instance of MCP4725 using the default address (0x62)
36+
dac = MCP4725(0x62)
37+
# Set output voltage to 50% VCC
38+
dac.setVoltage(2048)

0 commit comments

Comments
 (0)