Neil Thiessen / MCP4922

Dependents:   MCP4922_HelloWorld NuMidi401 NuFM401 NuFM402

Embed: (wiki syntax)

« Back to documentation index

MCP4922 Class Reference

MCP4922 Class Reference

MCP4922 class. More...

#include <MCP4922.h>

Public Types

enum  DAC { DAC_A, DAC_B }
 

Represents the different DACs in the MCP4922.

More...
enum  ReferenceMode { REF_UNBUFFERED, REF_BUFFERED }
 

Represents the reference buffer mode of DAC A or B in the MCP4922.

More...
enum  GainMode { GAIN_2X, GAIN_1X }
 

Represents the gain mode of DAC A or B in the MCP4922.

More...
enum  PowerMode { POWER_SHUTDOWN, POWER_NORMAL }
 

Represents the power mode of DAC A or B in the MCP4922.

More...

Public Member Functions

 MCP4922 (PinName mosi, PinName sclk, PinName cs, int hz=20000000)
 Create an MCP4922 object connected to the specified SPI pins.
MCP4922::ReferenceMode referenceMode (DAC dac)
 Get the current reference mode of the specified DAC in the MCP4922.
void referenceMode (DAC dac, ReferenceMode mode)
 Set the reference mode of the specified DAC in the MCP4922.
MCP4922::GainMode gainMode (DAC dac)
 Get the current gain mode of the specified DAC in the MCP4922.
void gainMode (DAC dac, GainMode mode)
 Set the gain mode of the specified DAC in the MCP4922.
MCP4922::PowerMode powerMode (DAC dac)
 Get the current power mode of the specified DAC in the MCP4922.
void powerMode (DAC dac, PowerMode mode)
 Set the power mode of the specified DAC in the MCP4922.
float read (DAC dac)
 Get the current output voltage of the specified DAC in the MCP4922 as a percentage.
void write (DAC dac, float value)
 Set the output voltage of the specified DAC in the MCP4922 from a percentage.
void write_u16 (DAC dac, unsigned short value)
 Set the output voltage of the specified DAC in the MCP4922 from a 16-bit range.

Detailed Description

MCP4922 class.

Used for controlling an MCP4922 DAC connected via SPI.

Example:

 #include "mbed.h"
 #include "MCP4922.h"

 //Create an MCP4922 object
 MCP4922 dac(p11, p13, p14);

 int main()
 {
     //Configure DAC A
     dac.referenceMode(MCP4922::DAC_A, MCP4922::REF_UNBUFFERED);
     dac.gainMode(MCP4922::DAC_A, MCP4922::GAIN_1X);
     dac.powerMode(MCP4922::DAC_A, MCP4922::POWER_NORMAL);

     while (1) {
         //Generate a sine wave on DAC A
         for (float i = 0.0; i < 360.0; i += 0.1)
             dac.write(MCP4922::DAC_A, 0.5 * (sinf(i * 3.14159265 / 180.0) + 1));
     }
 }

Definition at line 48 of file MCP4922.h.


Member Enumeration Documentation

enum DAC

Represents the different DACs in the MCP4922.

Enumerator:
DAC_A 

DAC A.

DAC_B 

DAC B.

Definition at line 53 of file MCP4922.h.

enum GainMode

Represents the gain mode of DAC A or B in the MCP4922.

Enumerator:
GAIN_2X 

DAC output = 2 * VREF * D/4096.

GAIN_1X 

DAC output = VREF * D/4096.

Definition at line 67 of file MCP4922.h.

enum PowerMode

Represents the power mode of DAC A or B in the MCP4922.

Enumerator:
POWER_SHUTDOWN 

DAC output is high impedance.

POWER_NORMAL 

DAC output is enabled.

Definition at line 74 of file MCP4922.h.

Represents the reference buffer mode of DAC A or B in the MCP4922.

Enumerator:
REF_UNBUFFERED 

DAC VREF is unbuffered.

REF_BUFFERED 

DAC VREF is buffered.

Definition at line 60 of file MCP4922.h.


Constructor & Destructor Documentation

MCP4922 ( PinName  mosi,
PinName  sclk,
PinName  cs,
int  hz = 20000000 
)

Create an MCP4922 object connected to the specified SPI pins.

Parameters:
mosiThe SPI data out pin.
sclkThe SPI clock pin.
csThe SPI chip select pin.
hzThe SPI bus frequency (defaults to 20MHz).

Definition at line 19 of file MCP4922.cpp.


Member Function Documentation

MCP4922::GainMode gainMode ( DAC  dac )

Get the current gain mode of the specified DAC in the MCP4922.

Parameters:
dacThe DAC to read from.
Returns:
The current gain mode of the specified DAC as a GainMode enum.

Definition at line 63 of file MCP4922.cpp.

void gainMode ( DAC  dac,
GainMode  mode 
)

Set the gain mode of the specified DAC in the MCP4922.

Parameters:
dacThe DAC to write to.
modeThe new gain mode for the specified DAC as a GainMode enum.

Definition at line 72 of file MCP4922.cpp.

MCP4922::PowerMode powerMode ( DAC  dac )

Get the current power mode of the specified DAC in the MCP4922.

Parameters:
dacThe DAC to read from.
Returns:
The current power mode of the specified DAC as a PowerMode enum.

Definition at line 92 of file MCP4922.cpp.

void powerMode ( DAC  dac,
PowerMode  mode 
)

Set the power mode of the specified DAC in the MCP4922.

Parameters:
dacThe DAC to write to.
modeThe new power mode for the specified DAC as a PowerMode enum.

Definition at line 101 of file MCP4922.cpp.

float read ( DAC  dac )

Get the current output voltage of the specified DAC in the MCP4922 as a percentage.

Parameters:
dacThe DAC to read from.
Returns:
The current output voltage of the specified DAC as a percentage (0.0 to 1.0 * VDD).

Definition at line 121 of file MCP4922.cpp.

void referenceMode ( DAC  dac,
ReferenceMode  mode 
)

Set the reference mode of the specified DAC in the MCP4922.

Parameters:
dacThe DAC to write to.
modeThe new reference mode for the specified DAC as a ReferenceMode enum.

Definition at line 43 of file MCP4922.cpp.

MCP4922::ReferenceMode referenceMode ( DAC  dac )

Get the current reference mode of the specified DAC in the MCP4922.

Parameters:
dacThe DAC to read from.
Returns:
The current reference mode of the specified DAC as a ReferenceMode enum.

Definition at line 34 of file MCP4922.cpp.

void write ( DAC  dac,
float  value 
)

Set the output voltage of the specified DAC in the MCP4922 from a percentage.

Parameters:
dacThe DAC to write to.
valueThe new output voltage for the specified DAC as a percentage (0.0 to 1.0 * VDD).

Definition at line 130 of file MCP4922.cpp.

void write_u16 ( DAC  dac,
unsigned short  value 
)

Set the output voltage of the specified DAC in the MCP4922 from a 16-bit range.

Parameters:
dacThe DAC to write to.
valueThe new output voltage for the specified DAC as a 16-bit unsigned short (0x0000 to 0xFFFF).

Definition at line 142 of file MCP4922.cpp.