Lange Level richtig laden.
[hackover2013-badge-firmware.git] / drivers / dac / mcp4725 / mcp4725.c
1 /**************************************************************************/
2 /*!
3 @file mcp4725.c
4 @author K. Townsend (microBuilder.eu)
5
6 @brief Driver for the I2C-based MCP4725 12-Bit DAC.
7
8 @section Example
9
10 @code
11 #include "drivers/dac/mcp4725/mcp4725.h"
12 ...
13
14 mcp4725Init();
15
16 // Set the voltage to 50% of vref and don't save the value in EEPROM
17 mcp4725SetVoltage(2048, false);
18
19 // Request the current value from the DAC
20 uint8_t status = 0;
21 uint16_t value = 0;
22 mcp4725ReadConfig(&status, &value);
23
24 @endcode
25
26 @section LICENSE
27
28 Software License Agreement (BSD License)
29
30 Copyright (c) 2010, microBuilder SARL
31 All rights reserved.
32
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.
43
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.
54 */
55 /**************************************************************************/
56 #include "mcp4725.h"
57 #include "core/i2c/i2c.h"
58
59 extern volatile uint8_t I2CMasterBuffer[I2C_BUFSIZE];
60 extern volatile uint8_t I2CSlaveBuffer[I2C_BUFSIZE];
61 extern volatile uint32_t I2CReadLength, I2CWriteLength;
62
63 static bool _mcp4725Initialised = false;
64
65 /**************************************************************************/
66 /*!
67 @brief Initialises I2C for the MCP4725.
68 */
69 /**************************************************************************/
70 int mcp4725Init()
71 {
72 // Initialise I2C
73 if (i2cInit(I2CMASTER) == false)
74 {
75 /* Fatal error */
76 return -1;
77 }
78
79 /* Set initialisation flag */
80 _mcp4725Initialised = true;
81
82 return 0;
83 }
84
85 /**************************************************************************/
86 /*!
87 @brief Sets the output voltage to a fraction of source vref. (Value
88 can be 0..4095)
89
90 @param[in] output
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.
98 */
99 /**************************************************************************/
100 void mcp4725SetVoltage( uint16_t output, bool writeEEPROM )
101 {
102 if (!_mcp4725Initialised) mcp4725Init();
103
104 // Clear write buffers
105 uint32_t i;
106 for ( i = 0; i < I2C_BUFSIZE; i++ )
107 {
108 I2CMasterBuffer[i] = 0x00;
109 }
110
111 I2CWriteLength = 4;
112 I2CReadLength = 0;
113 I2CMasterBuffer[0] = MCP4725_ADDRESS; // I2C device address
114 if (writeEEPROM) // command and config bits (C2.C1.C0.x.x.PD1.PD0.x)
115 {
116 I2CMasterBuffer[1] = (MCP4726_CMD_WRITEDACEEPROM);
117 }
118 else
119 {
120 I2CMasterBuffer[1] = (MCP4726_CMD_WRITEDAC);
121 }
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)
124 i2cEngine();
125 }
126
127 /**************************************************************************/
128 /*!
129 @brief Reads the current configuration and output settings for the
130 DAC.
131
132 @param[out] status
133 Pointer to hold the contents of the status register
134 @param[out] value
135 Pointer to hold the output value of the 12-bit DAC
136 */
137 /**************************************************************************/
138 void mcp4725ReadConfig( uint8_t *status, uint16_t *value )
139 {
140 if (!_mcp4725Initialised) mcp4725Init();
141
142 // Clear write buffers
143 uint32_t i;
144 for ( i = 0; i < I2C_BUFSIZE; i++ )
145 {
146 I2CMasterBuffer[i] = 0x00;
147 }
148
149 I2CWriteLength = 1;
150 I2CReadLength = 3;
151 I2CMasterBuffer[0] = MCP4725_ADDRESS | MCP4725_READ;
152 i2cEngine();
153
154 // Shift values to create properly formed integers
155 *status = I2CSlaveBuffer[0];
156 *value = ((I2CSlaveBuffer[1] << 4) | (I2CSlaveBuffer[2] >> 4));
157 }
158
This page took 0.059023 seconds and 5 git commands to generate.