Permalink
Browse files

Added ability to change addr - works with various addrs

  • Loading branch information...
1 parent ee77a80 commit 200ec3d3d764bbf3f80e95f0803b686311621a34 @ladyada ladyada committed Jul 5, 2012
Showing with 15 additions and 7 deletions.
  1. +7 −2 Adafruit_MCP4725.cpp
  2. +2 −2 Adafruit_MCP4725.h
  3. +6 −3 examples/trianglewave/trianglewave.pde
View
9 Adafruit_MCP4725.cpp
@@ -41,8 +41,10 @@ Adafruit_MCP4725::Adafruit_MCP4725() {
@brief Setups the HW
*/
/**************************************************************************/
-void Adafruit_MCP4725::begin() {
+void Adafruit_MCP4725::begin(uint8_t addr) {
+ _i2caddr = addr;
Wire.begin();
+
}
/**************************************************************************/
@@ -62,7 +64,9 @@ void Adafruit_MCP4725::begin() {
/**************************************************************************/
void Adafruit_MCP4725::setVoltage( uint16_t output, bool writeEEPROM )
{
- Wire.beginTransmission(MCP4725_ADDRESS);
+ uint8_t twbrback = TWBR;
+ TWBR = 12; // 400 khz
+ Wire.beginTransmission(_i2caddr);
if (writeEEPROM)
{
Wire.write(MCP4726_CMD_WRITEDACEEPROM);
@@ -74,4 +78,5 @@ void Adafruit_MCP4725::setVoltage( uint16_t output, bool writeEEPROM )
Wire.write(output / 16); // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4)
Wire.write(output % 16) << 4; // Lower data bits (D3.D2.D1.D0.x.x.x.x)
Wire.endTransmission();
+ TWBR = twbrback;
}
View
4 Adafruit_MCP4725.h
@@ -25,15 +25,15 @@
#include <Wire.h>
-#define MCP4725_ADDRESS (0x62) // 1100010 - Assumes A0 is GND and A2,A1 are 01 (MCP4725A1T-E/CH)
#define MCP4726_CMD_WRITEDAC (0x40) // Writes data to the DAC
#define MCP4726_CMD_WRITEDACEEPROM (0x60) // Writes data to the DAC and the EEPROM (persisting the assigned value after reset)
class Adafruit_MCP4725{
public:
Adafruit_MCP4725();
- void begin(void);
+ void begin(uint8_t a);
void setVoltage( uint16_t output, bool writeEEPROM );
private:
+ uint8_t _i2caddr;
};
View
9 examples/trianglewave/trianglewave.pde
@@ -20,10 +20,13 @@
Adafruit_MCP4725 dac;
void setup(void) {
- Serial.begin(115200);
+ Serial.begin(9600);
Serial.println("Hello!");
- dac.begin();
+ // For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC)
+ // For MCP4725A0 the address is 0x60 or 0x61
+ // For MCP4725A2 the address is 0x64 or 0x65
+ dac.begin(0x62);
Serial.println("Generating a triangle wave");
}
@@ -39,4 +42,4 @@ void loop(void) {
{
dac.setVoltage(counter, false);
}
-}
+}

0 comments on commit 200ec3d

Please sign in to comment.