DD
DD
* Pins used:
* Arduino pin 11 (for Uno; for Mega: 51) to device SDI (pin 4) - fixed pin
* Arduino pin 13 (for Uno; for Mega: 52) to device SCK (pin 3) - fixed pin
* Any digital pin to device LDAC (DAC pin 5) (except with PortWrite, see README)
* Any digital pin to device CS (DAC pin 2) (as above)
*
* Other DAC wirings:
* Pin 1: VDD, to +5 V
* Pin 5: LDAC, either to an Arduino pin, or ground to update vout automatically
* Pin 6: VREF, to +5 V (or some other reference voltage 0 < VREF <= VDD)
* Pin 7: VSS, to ground
* Pin 8: vout
* (Pin 9, for the DFN package only: VSS)
* Only tested on MCP4901 (8-bit) and MCP4922 (dual 12-bit), but it should work on
the others as well.
* Tested on an Arduino Uno R3.
*/
#ifndef _DAC_MCP4X_H
#define _DAC_MCP4X_H
#include <Arduino.h>
#include <inttypes.h>
//
// Params
//
#define MCP4X_PORT_WRITE 1
#define MCP4X_NO_LDAC -1
#ifndef MCP4X_PORT_WRITE
#define MCP4X_PORT_WRITE 0
#endif
#ifndef MCP4X_DEFAULTS
#define MCP4X_DEFAULTS (MCP4X_BUFFERED | MCP4X_GAIN_1X | MCP4X_ACTIVE)
#endif
//
// Constants
//
// Bits
#define MCP4X_WRITE_B (1 << 15)
#define MCP4X_BUFFERED (1 << 14)
#define MCP4X_GAIN_1X (1 << 13)
#define MCP4X_ACTIVE (1 << 12)
// Channels
#define MCP4X_CHAN_A 0
#define MCP4X_CHAN_B 1
// Model identifiers
#define MCP4X_8_BITS 0
#define MCP4X_10_BITS 1
#define MCP4X_12_BITS 2
#define MCP4X_INTREF (1 << 2)
#define MCP4X_DUAL (1 << 3)
class MCP4X {
public:
void configureSPI();
//
// output-like methods that takes a voltage argument
//
void setVoltage(byte chan, float v);
/* Actually change the output, if the LDAC pin isn't shorted to ground */
void latch(void);
private:
unsigned int vrefs[2];
unsigned int regs[2];
boolean dual;
int ss_pin;
int LDAC_pin;
int bitwidth;
boolean autoLatch; /* call latch() automatically after output2() has been
called? */
#endif