The Best / MCP4725

Dependents:   dacc

Fork of MCP4725 by Neil Thiessen

Embed: (wiki syntax)

« Back to documentation index

MCP4725 Class Reference

MCP4725 Class Reference

MCP4725 class. More...

#include <MCP4725.h>

Public Types

enum  Address {
  ADDRESS_0 = (0x60 << 1), ADDRESS_1 = (0x61 << 1), ADDRESS_2 = (0x62 << 1), ADDRESS_3 = (0x63 << 1),
  ADDRESS_4 = (0x64 << 1), ADDRESS_5 = (0x65 << 1), ADDRESS_6 = (0x67 << 1), ADDRESS_7 = (0x68 << 1)
}
 

Represents the different I2C address possibilities for the MCP4725.

More...
enum  PowerMode { POWER_NORMAL, POWER_SHUTDOWN_1K, POWER_SHUTDOWN_100K, POWER_SHUTDOWN_500K }
 

Represents the power mode of the MCP4725.

More...

Public Member Functions

 MCP4725 (PinName sda, PinName scl, Address addr=ADDRESS_0, int hz=400000)
 Create an MCP4725 object connected to the specified I2C pins with the specified I2C slave address.
bool open ()
 Probe for the MCP4725 and indicate if it's present on the bus.
void reset ()
 Issue a General Call Reset command to reset all MCP4725 devices on the bus.
void wakeup ()
 Issue a General Call Wake-up command to power-up all MCP4725 devices on the bus.
MCP4725::PowerMode powerMode ()
 Get the current power mode of the MCP4725.
void powerMode (PowerMode mode)
 Set the power mode of the MCP4725.
float read ()
 Get the current output voltage of the MCP4725 as a percentage.
void write (float value)
 Set the output voltage of the MCP4725 from a percentage.
void write_u12 (unsigned short value)
 Set the output voltage of the MCP4725 from a 16-bit range.
void readEeprom (PowerMode *mode, unsigned short *value)
 Get the current DAC settings in EEPROM.
void writeEeprom (PowerMode mode, unsigned short value)
 Set the DAC settings in EEPROM.
 operator float ()
 A shorthand for read()
MCP4725operator= (float value)
 A shorthand for write()

Detailed Description

MCP4725 class.

Used for controlling an MCP4725 DAC connected via I2C.

Example:

 #include "mbed.h"
 #include "MCP4725.h"

 //Create an MCP4725 object at the default address (ADDRESS_0)
 MCP4725 dac(p28, p27);

 int main()
 {
     //Try to open the MCP4725
     if (dac.open()) {
         printf("Device detected!\n");

         //Wake up the DAC
         //NOTE: This might wake up other I2C devices as well!
         dac.wakeup();

         while (1) {
             //Generate a sine wave on the DAC
             for (float i = 0.0; i < 360.0; i += 0.1)
                 dac = 0.5 * (sinf(i * 3.14159265 / 180.0) + 1);
         }
     } else {
         error("Device not detected!\n");
     }
 }

Definition at line 54 of file MCP4725.h.


Member Enumeration Documentation

enum Address

Represents the different I2C address possibilities for the MCP4725.

Enumerator:
ADDRESS_0 

A[2:0] bits = 000.

ADDRESS_1 

A[2:0] bits = 001.

ADDRESS_2 

A[2:0] bits = 010.

ADDRESS_3 

A[2:0] bits = 011.

ADDRESS_4 

A[2:0] bits = 100.

ADDRESS_5 

A[2:0] bits = 101.

ADDRESS_6 

A[2:0] bits = 110.

ADDRESS_7 

A[2:0] bits = 111.

Definition at line 59 of file MCP4725.h.

enum PowerMode

Represents the power mode of the MCP4725.

Enumerator:
POWER_NORMAL 

Chip is enabled, and the output is active.

POWER_SHUTDOWN_1K 

Chip is shutdown, and the output is grounded with a 1kΩ resistor.

POWER_SHUTDOWN_100K 

Chip is shutdown, and the output is grounded with a 100kΩ resistor.

POWER_SHUTDOWN_500K 

Chip is shutdown, and the output is grounded with a 500kΩ resistor.

Definition at line 72 of file MCP4725.h.


Constructor & Destructor Documentation

MCP4725 ( PinName  sda,
PinName  scl,
Address  addr = ADDRESS_0,
int  hz = 400000 
)

Create an MCP4725 object connected to the specified I2C pins with the specified I2C slave address.

Parameters:
sdaThe I2C data pin.
sclThe I2C clock pin.
addrThe I2C slave address (defaults to ADDRESS_0).
hzThe I2C bus frequency (defaults to 400kHz).

Definition at line 19 of file MCP4725.cpp.


Member Function Documentation

bool open (  )

Probe for the MCP4725 and indicate if it's present on the bus.

Returns:
'true' if the device exists on the bus, 'false' if the device doesn't exist on the bus.

Definition at line 25 of file MCP4725.cpp.

operator float (  )

A shorthand for read()

Returns:
The current output voltage as a percentage (0.0 to 1.0 * VDD).

Definition at line 141 of file MCP4725.cpp.

MCP4725 & operator= ( float  value )

A shorthand for write()

Parameters:
valueThe new output voltage as a percentage (0.0 to 1.0 * VDD).

Definition at line 147 of file MCP4725.cpp.

MCP4725::PowerMode powerMode (  )

Get the current power mode of the MCP4725.

Returns:
The current power mode as a PowerMode enum.

Definition at line 55 of file MCP4725.cpp.

void powerMode ( PowerMode  mode )

Set the power mode of the MCP4725.

Parameters:
modeThe new power mode as a PowerMode enum.

Definition at line 64 of file MCP4725.cpp.

float read (  )

Get the current output voltage of the MCP4725 as a percentage.

Returns:
The current output voltage as a percentage (0.0 to 1.0 * VDD).

Definition at line 73 of file MCP4725.cpp.

void readEeprom ( PowerMode mode,
unsigned short *  value 
)

Get the current DAC settings in EEPROM.

Parameters:
modePointer to a PowerMode enum for the power mode in EEPROM.
valuePointer to an unsigned short for the 12-bit DAC value in EEPROM (0x0000 to 0x0FFF).

Definition at line 106 of file MCP4725.cpp.

void reset (  )

Issue a General Call Reset command to reset all MCP4725 devices on the bus.

Warning:
This might reset other I2C devices as well

Definition at line 37 of file MCP4725.cpp.

void wakeup (  )

Issue a General Call Wake-up command to power-up all MCP4725 devices on the bus.

Warning:
This might wake up other I2C devices as well

Definition at line 46 of file MCP4725.cpp.

void write ( float  value )

Set the output voltage of the MCP4725 from a percentage.

Parameters:
valueThe new output voltage as a percentage (0.0 to 1.0 * VDD).

Definition at line 82 of file MCP4725.cpp.

void write_u12 ( unsigned short  value )

Set the output voltage of the MCP4725 from a 16-bit range.

Parameters:
valueThe new output voltage as a 16-bit unsigned short (0x0000 to 0xFFFF).

Definition at line 97 of file MCP4725.cpp.

void writeEeprom ( PowerMode  mode,
unsigned short  value 
)

Set the DAC settings in EEPROM.

Parameters:
modeThe new EEPROM power mode as a PowerMode enum.
valueThe new EEPROM DAC value as a 12-bit unsigned short (0x0000 to 0x0FFF).

Definition at line 121 of file MCP4725.cpp.