# i2cDAC.cpp

This sketch talks to a MCP4725 digital-to-analog converter, as sold in the Sparkfun breakout board at http://sparkfun.com/products/8736

    /*                                             -*- mode:C++ -*-
     *
     * Sketch description: Demonstration of driving an MCP4725 DAC via
     * I2C, using full I2C transactions.  This sketch generates
     * sawtooth(ish) waves at about 400Hz.
     *
     * Schematic:
     *  U1: IXM
     *  U2: MCP4725
     *
     *      +--------------+       +--------------+
     *  VSS-|GND   N    U1 |    D3-|VDD     ANALOG|-> To some kind of amp
     *  VDD-|D3            |   GND-|VSS   U2      |
     *  SDA-|Tx   LED      |    RX-|SCL           |
     *      |12V  FACE    E|    TX-|SDA           |
     *  SCL-|Rx            |       +--------------+
     *      |D2            |
     *      |GND   S       |
     *      +--------------+
     *
     * Sketch author: Dave
     *
     */

    #define I2C_ADDR 0x60  // MCP4725

    SFBHWI2C i2c(I2C_1);   // Create object 'i2c', set up for I2C controller 1

    void setup() {
      pinMode(WEST_D3_PIN,OUTPUT);         // The DAC draws little power,
      digitalWrite(WEST_D3_PIN,HIGH);      // so we feed it off an output pin..
      i2c.init(I2C1_PINS_WEST_TX_RX,400);  // Range is officially 100..400KHz
    }

    u32 nextLevel = 0;     // DAC level we're currently sending
    u8 buf[2];             // I2C command buffer
    const u32 step = 150;  // How much we increase the level per i2c command

    void loop() {
      if (!i2c.status()) {             // Whenever the bus is available..
        buf[0] = (nextLevel>>8)&0xf;   // Get high four bits to send
        buf[1] = nextLevel&0xff;       // Get low eight bits to send
        i2c.transmit(I2C_ADDR,2,buf);  // Start an I2C transmission
        nextLevel =(nextLevel+step)&0xfff; // Move on to next level
      }
      ledSet(BODY_RGB_BLUE_PIN,seconds()&1);
    }

    #define SFB_SKETCH_CREATOR_ID B36_4(d,a,v,e)
    #define SFB_SKETCH_PROGRAM_ID B36_5(i,2,d,a,c)
    #define SFB_SKETCH_COPYRIGHT_NOTICE "2011 Dave Ackley Placed in the public domain"