A feature complete driver for the MCP4725 DAC from Microchip with an AnalogOut-compatible API.

Dependents:   MCP4725_HelloWorld DAC02_RFID_and_TFT

Committer:
neilt6
Date:
Tue May 06 17:11:35 2014 +0000
Revision:
0:203cdd3b30fc
Child:
1:3b687a9acdf3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:203cdd3b30fc 1 /* MCP4725 Driver Library
neilt6 0:203cdd3b30fc 2 * Copyright (c) 2014 Neil Thiessen
neilt6 0:203cdd3b30fc 3 *
neilt6 0:203cdd3b30fc 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:203cdd3b30fc 5 * you may not use this file except in compliance with the License.
neilt6 0:203cdd3b30fc 6 * You may obtain a copy of the License at
neilt6 0:203cdd3b30fc 7 *
neilt6 0:203cdd3b30fc 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:203cdd3b30fc 9 *
neilt6 0:203cdd3b30fc 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:203cdd3b30fc 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:203cdd3b30fc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:203cdd3b30fc 13 * See the License for the specific language governing permissions and
neilt6 0:203cdd3b30fc 14 * limitations under the License.
neilt6 0:203cdd3b30fc 15 */
neilt6 0:203cdd3b30fc 16
neilt6 0:203cdd3b30fc 17 #include "MCP4725.h"
neilt6 0:203cdd3b30fc 18
neilt6 0:203cdd3b30fc 19 MCP4725::MCP4725(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr)
neilt6 0:203cdd3b30fc 20 {
neilt6 0:203cdd3b30fc 21 //Set the I2C bus frequency
neilt6 0:203cdd3b30fc 22 m_I2C.frequency(hz);
neilt6 0:203cdd3b30fc 23 }
neilt6 0:203cdd3b30fc 24
neilt6 0:203cdd3b30fc 25 bool MCP4725::open()
neilt6 0:203cdd3b30fc 26 {
neilt6 0:203cdd3b30fc 27 //Probe for the MCP4725 using a Zero Length Transfer
neilt6 0:203cdd3b30fc 28 if (!m_I2C.write(m_ADDR, NULL, 0)) {
neilt6 0:203cdd3b30fc 29 //Return success
neilt6 0:203cdd3b30fc 30 return true;
neilt6 0:203cdd3b30fc 31 } else {
neilt6 0:203cdd3b30fc 32 //Return failure
neilt6 0:203cdd3b30fc 33 return false;
neilt6 0:203cdd3b30fc 34 }
neilt6 0:203cdd3b30fc 35 }
neilt6 0:203cdd3b30fc 36
neilt6 0:203cdd3b30fc 37 void MCP4725::reset()
neilt6 0:203cdd3b30fc 38 {
neilt6 0:203cdd3b30fc 39 //The General Call Reset command
neilt6 0:203cdd3b30fc 40 char data = 0x06;
neilt6 0:203cdd3b30fc 41
neilt6 0:203cdd3b30fc 42 //Issue the command to the General Call address
neilt6 0:203cdd3b30fc 43 m_I2C.write(0x00, &data, 1);
neilt6 0:203cdd3b30fc 44 }
neilt6 0:203cdd3b30fc 45
neilt6 0:203cdd3b30fc 46 void MCP4725::wakeup()
neilt6 0:203cdd3b30fc 47 {
neilt6 0:203cdd3b30fc 48 //The General Call Wake-up command
neilt6 0:203cdd3b30fc 49 char data = 0x09;
neilt6 0:203cdd3b30fc 50
neilt6 0:203cdd3b30fc 51 //Issue the command to the General Call address
neilt6 0:203cdd3b30fc 52 m_I2C.write(0x00, &data, 1);
neilt6 0:203cdd3b30fc 53 }
neilt6 0:203cdd3b30fc 54
neilt6 0:203cdd3b30fc 55 MCP4725::PowerMode MCP4725::powerMode()
neilt6 0:203cdd3b30fc 56 {
neilt6 0:203cdd3b30fc 57 //Read the current settings from the DAC
neilt6 0:203cdd3b30fc 58 readDac();
neilt6 0:203cdd3b30fc 59
neilt6 0:203cdd3b30fc 60 //Return the current power mode
neilt6 0:203cdd3b30fc 61 return m_PowerMode;
neilt6 0:203cdd3b30fc 62 }
neilt6 0:203cdd3b30fc 63
neilt6 0:203cdd3b30fc 64 void MCP4725::powerMode(PowerMode mode)
neilt6 0:203cdd3b30fc 65 {
neilt6 0:203cdd3b30fc 66 //Update the power mode
neilt6 0:203cdd3b30fc 67 m_PowerMode = mode;
neilt6 0:203cdd3b30fc 68
neilt6 0:203cdd3b30fc 69 //Update the DAC
neilt6 0:203cdd3b30fc 70 writeDac();
neilt6 0:203cdd3b30fc 71 }
neilt6 0:203cdd3b30fc 72
neilt6 0:203cdd3b30fc 73 float MCP4725::read()
neilt6 0:203cdd3b30fc 74 {
neilt6 0:203cdd3b30fc 75 //Read the current settings from the DAC
neilt6 0:203cdd3b30fc 76 readDac();
neilt6 0:203cdd3b30fc 77
neilt6 0:203cdd3b30fc 78 //Return the current DAC value as a percentage
neilt6 0:203cdd3b30fc 79 return m_DacValue / 4095.0;
neilt6 0:203cdd3b30fc 80 }
neilt6 0:203cdd3b30fc 81
neilt6 0:203cdd3b30fc 82 void MCP4725::write(float value)
neilt6 0:203cdd3b30fc 83 {
neilt6 0:203cdd3b30fc 84 //Range limit value
neilt6 0:203cdd3b30fc 85 if (value < 0.0)
neilt6 0:203cdd3b30fc 86 value = 0.0;
neilt6 0:203cdd3b30fc 87 else if (value > 1.0)
neilt6 0:203cdd3b30fc 88 value = 1.0;
neilt6 0:203cdd3b30fc 89
neilt6 0:203cdd3b30fc 90 //Update the DAC value
neilt6 0:203cdd3b30fc 91 m_DacValue = (unsigned short)(value * 4095);
neilt6 0:203cdd3b30fc 92
neilt6 0:203cdd3b30fc 93 //Update the DAC
neilt6 0:203cdd3b30fc 94 writeDac();
neilt6 0:203cdd3b30fc 95 }
neilt6 0:203cdd3b30fc 96
neilt6 0:203cdd3b30fc 97 void MCP4725::write_u12(unsigned short value)
neilt6 0:203cdd3b30fc 98 {
neilt6 0:203cdd3b30fc 99 //Update the DAC value
neilt6 0:203cdd3b30fc 100 m_DacValue = value & 0x0FFF;
neilt6 0:203cdd3b30fc 101
neilt6 0:203cdd3b30fc 102 //Update the DAC
neilt6 0:203cdd3b30fc 103 writeDac();
neilt6 0:203cdd3b30fc 104 }
neilt6 0:203cdd3b30fc 105
neilt6 0:203cdd3b30fc 106 void MCP4725::write_u16(unsigned short value)
neilt6 0:203cdd3b30fc 107 {
neilt6 0:203cdd3b30fc 108 //Update the DAC value
neilt6 0:203cdd3b30fc 109 m_DacValue = value >> 4;
neilt6 0:203cdd3b30fc 110
neilt6 0:203cdd3b30fc 111 //Update the DAC
neilt6 0:203cdd3b30fc 112 writeDac();
neilt6 0:203cdd3b30fc 113 }
neilt6 0:203cdd3b30fc 114
neilt6 0:203cdd3b30fc 115 void MCP4725::readEeprom(PowerMode* mode, unsigned short* value)
neilt6 0:203cdd3b30fc 116 {
neilt6 0:203cdd3b30fc 117 //Create a temporary buffer
neilt6 0:203cdd3b30fc 118 char buff[5];
neilt6 0:203cdd3b30fc 119
neilt6 0:203cdd3b30fc 120 //Keep reading until the EEPROM is ready
neilt6 0:203cdd3b30fc 121 do {
neilt6 0:203cdd3b30fc 122 m_I2C.read(m_ADDR, buff, 5);
neilt6 0:203cdd3b30fc 123 } while ((buff[0] & 0x80) == 0);
neilt6 0:203cdd3b30fc 124
neilt6 0:203cdd3b30fc 125 //Extract the EEPROM power mode, and 12-bit DAC value
neilt6 0:203cdd3b30fc 126 *mode = (PowerMode)((buff[3] >> 5) & 0x03);
neilt6 0:203cdd3b30fc 127 *value = ((buff[3] << 8) & 0xFFF) | buff[4];
neilt6 0:203cdd3b30fc 128 }
neilt6 0:203cdd3b30fc 129
neilt6 0:203cdd3b30fc 130 void MCP4725::writeEeprom(PowerMode mode, unsigned short value)
neilt6 0:203cdd3b30fc 131 {
neilt6 0:203cdd3b30fc 132 //Create a temporary buffer
neilt6 0:203cdd3b30fc 133 char buff[5];
neilt6 0:203cdd3b30fc 134
neilt6 0:203cdd3b30fc 135 //Block until the EEPROM is ready
neilt6 0:203cdd3b30fc 136 do {
neilt6 0:203cdd3b30fc 137 m_I2C.read(m_ADDR, buff, 5);
neilt6 0:203cdd3b30fc 138 } while ((buff[0] & 0x80) == 0);
neilt6 0:203cdd3b30fc 139
neilt6 0:203cdd3b30fc 140 //Load the command, power mode, and 12-bit DAC value
neilt6 0:203cdd3b30fc 141 buff[0] = 0x60 | ((int)mode << 1);
neilt6 0:203cdd3b30fc 142 buff[1] = value >> 4;
neilt6 0:203cdd3b30fc 143 buff[2] = value << 4;
neilt6 0:203cdd3b30fc 144
neilt6 0:203cdd3b30fc 145 //Write the data
neilt6 0:203cdd3b30fc 146 m_I2C.write(m_ADDR, buff, 3);
neilt6 0:203cdd3b30fc 147 }
neilt6 0:203cdd3b30fc 148
neilt6 0:203cdd3b30fc 149 MCP4725::operator float()
neilt6 0:203cdd3b30fc 150 {
neilt6 0:203cdd3b30fc 151 //Return the current output voltage
neilt6 0:203cdd3b30fc 152 return read();
neilt6 0:203cdd3b30fc 153 }
neilt6 0:203cdd3b30fc 154
neilt6 0:203cdd3b30fc 155 MCP4725& MCP4725::operator=(float value)
neilt6 0:203cdd3b30fc 156 {
neilt6 0:203cdd3b30fc 157 //Set the output voltage
neilt6 0:203cdd3b30fc 158 write(value);
neilt6 0:203cdd3b30fc 159 return *this;
neilt6 0:203cdd3b30fc 160 }
neilt6 0:203cdd3b30fc 161
neilt6 0:203cdd3b30fc 162 void MCP4725::readDac()
neilt6 0:203cdd3b30fc 163 {
neilt6 0:203cdd3b30fc 164 //Create a temporary buffer
neilt6 0:203cdd3b30fc 165 char buff[5];
neilt6 0:203cdd3b30fc 166
neilt6 0:203cdd3b30fc 167 //Read the current DAC settings
neilt6 0:203cdd3b30fc 168 m_I2C.read(m_ADDR, buff, 5);
neilt6 0:203cdd3b30fc 169
neilt6 0:203cdd3b30fc 170 //Extract the current power mode, and 12-bit DAC value
neilt6 0:203cdd3b30fc 171 m_PowerMode = (PowerMode)((buff[0] >> 1) & 0x03);
neilt6 0:203cdd3b30fc 172 m_DacValue = (buff[1] << 4) | (buff[2] >> 4);
neilt6 0:203cdd3b30fc 173 }
neilt6 0:203cdd3b30fc 174
neilt6 0:203cdd3b30fc 175 void MCP4725::writeDac()
neilt6 0:203cdd3b30fc 176 {
neilt6 0:203cdd3b30fc 177 //Create a temporary buffer
neilt6 0:203cdd3b30fc 178 char buff[2];
neilt6 0:203cdd3b30fc 179
neilt6 0:203cdd3b30fc 180 //Load the power mode, and 12-bit DAC value
neilt6 0:203cdd3b30fc 181 buff[0] = ((int)m_PowerMode << 4) | (m_DacValue >> 8);
neilt6 0:203cdd3b30fc 182 buff[1] = m_DacValue;
neilt6 0:203cdd3b30fc 183
neilt6 0:203cdd3b30fc 184 //Write the data
neilt6 0:203cdd3b30fc 185 m_I2C.write(m_ADDR, buff, 2);
neilt6 0:203cdd3b30fc 186 }