1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 @brief Driver for the I2C-based MCP4725 12-Bit DAC.
11 #include "drivers/dac/mcp4725/mcp4725.h"
16 // Set the voltage to 50% of vref and don't save the value in EEPROM
17 mcp4725SetVoltage(2048, false);
19 // Request the current value from the DAC
22 mcp4725ReadConfig(&status, &value);
28 Software License Agreement (BSD License)
30 Copyright (c) 2010, microBuilder SARL
33 Redistribution and use in source and binary forms, with or without
34 modification, are permitted provided that the following conditions are met:
35 1. Redistributions of source code must retain the above copyright
36 notice, this list of conditions and the following disclaimer.
37 2. Redistributions in binary form must reproduce the above copyright
38 notice, this list of conditions and the following disclaimer in the
39 documentation and/or other materials provided with the distribution.
40 3. Neither the name of the copyright holders nor the
41 names of its contributors may be used to endorse or promote products
42 derived from this software without specific prior written permission.
44 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
45 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
48 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 /**************************************************************************/
57 #include "core/i2c/i2c.h"
59 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
60 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
61 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
63 static bool _mcp4725Initialised
= false;
65 /**************************************************************************/
67 @brief Initialises I2C for the MCP4725.
69 /**************************************************************************/
73 if (i2cInit(I2CMASTER
) == false)
79 /* Set initialisation flag */
80 _mcp4725Initialised
= true;
85 /**************************************************************************/
87 @brief Sets the output voltage to a fraction of source vref. (Value
91 The 12-bit value representing the relationship between
92 the DAC's input voltage and its output voltage.
93 @param[in] writeEEPROM
94 If this value is true, 'output' will also be written
95 to the MCP4725's internal non-volatile memory, meaning
96 that the DAC will retain the current voltage output
97 after power-down or reset.
99 /**************************************************************************/
100 void mcp4725SetVoltage( uint16_t output
, bool writeEEPROM
)
102 if (!_mcp4725Initialised
) mcp4725Init();
104 // Clear write buffers
106 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
108 I2CMasterBuffer
[i
] = 0x00;
113 I2CMasterBuffer
[0] = MCP4725_ADDRESS
; // I2C device address
114 if (writeEEPROM
) // command and config bits (C2.C1.C0.x.x.PD1.PD0.x)
116 I2CMasterBuffer
[1] = (MCP4726_CMD_WRITEDACEEPROM
);
120 I2CMasterBuffer
[1] = (MCP4726_CMD_WRITEDAC
);
122 I2CMasterBuffer
[2] = (output
/ 16); // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4)
123 I2CMasterBuffer
[3] = (output
% 16) << 4; // Lower data bits (D3.D2.D1.D0.x.x.x.x)
127 /**************************************************************************/
129 @brief Reads the current configuration and output settings for the
133 Pointer to hold the contents of the status register
135 Pointer to hold the output value of the 12-bit DAC
137 /**************************************************************************/
138 void mcp4725ReadConfig( uint8_t *status
, uint16_t *value
)
140 if (!_mcp4725Initialised
) mcp4725Init();
142 // Clear write buffers
144 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
146 I2CMasterBuffer
[i
] = 0x00;
151 I2CMasterBuffer
[0] = MCP4725_ADDRESS
| MCP4725_READ
;
154 // Shift values to create properly formed integers
155 *status
= I2CSlaveBuffer
[0];
156 *value
= ((I2CSlaveBuffer
[1] << 4) | (I2CSlaveBuffer
[2] >> 4));