The Best / MCP4725

Dependents:   dacc

Fork of MCP4725 by Neil Thiessen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP4725.cpp Source File

MCP4725.cpp

00001 /* MCP4725 Driver Library
00002  * Copyright (c) 2014 Neil Thiessen
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "MCP4725.h"
00018 
00019 MCP4725::MCP4725(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr)
00020 {
00021     //Set the I2C bus frequency
00022     m_I2C.frequency(hz);
00023 }
00024 
00025 bool MCP4725::open()
00026 {
00027     //Probe for the MCP4725 using a Zero Length Transfer
00028     if (!m_I2C.write(m_ADDR, NULL, 0)) {
00029         //Return success
00030         return true;
00031     } else {
00032         //Return failure
00033         return false;
00034     }
00035 }
00036 
00037 void MCP4725::reset()
00038 {
00039     //The General Call Reset command
00040     char data = 0x06;
00041 
00042     //Issue the command to the General Call address
00043     m_I2C.write(0x00, &data, 1);
00044 }
00045 
00046 void MCP4725::wakeup()
00047 {
00048     //The General Call Wake-up command
00049     char data = 0x09;
00050 
00051     //Issue the command to the General Call address
00052     m_I2C.write(0x00, &data, 1);
00053 }
00054 
00055 MCP4725::PowerMode MCP4725::powerMode()
00056 {
00057     //Read the current settings from the DAC
00058     readDac();
00059 
00060     //Return the current power mode
00061     return m_PowerMode;
00062 }
00063 
00064 void MCP4725::powerMode(PowerMode mode)
00065 {
00066     //Update the power mode
00067     m_PowerMode = mode;
00068 
00069     //Update the DAC
00070     writeDac();
00071 }
00072 
00073 float MCP4725::read()
00074 {
00075     //Read the current settings from the DAC
00076     readDac();
00077 
00078     //Return the current DAC value as a percentage
00079     return m_DacValue / 4095.0;
00080 }
00081 
00082 void MCP4725::write(float value)
00083 {
00084     //Range limit value
00085     if (value < 0.0)
00086         value = 0.0;
00087     else if (value > 1.0)
00088         value = 1.0;
00089 
00090     //Update the DAC value
00091     m_DacValue = (unsigned short)(value * 4095);
00092 
00093     //Update the DAC
00094     writeDac();
00095 }
00096 
00097 void MCP4725::write_u12(unsigned short value)
00098 {
00099     //Update the DAC value
00100     //m_DacValue = value >> 4;
00101     m_DacValue = value;
00102     //Update the DAC
00103     writeDac();
00104 }
00105 
00106 void MCP4725::readEeprom(PowerMode* mode, unsigned short* value)
00107 {
00108     //Create a temporary buffer
00109     char buff[5];
00110 
00111     //Keep reading until the EEPROM is ready
00112     do {
00113         m_I2C.read(m_ADDR, buff, 5);
00114     } while ((buff[0] & 0x80) == 0);
00115 
00116     //Extract the EEPROM power mode, and 12-bit DAC value
00117     *mode = (PowerMode)((buff[3] >> 5) & 0x03);
00118     *value = ((buff[3] << 8) & 0xFFF) | buff[4];
00119 }
00120 
00121 void MCP4725::writeEeprom(PowerMode mode, unsigned short value)
00122 {
00123     //Create a temporary buffer
00124     char buff[5];
00125 
00126     //Block until the EEPROM is ready
00127     do {
00128         m_I2C.read(m_ADDR, buff, 5);
00129     } while ((buff[0] & 0x80) == 0);
00130 
00131     //Load the command, power mode, and 12-bit DAC value
00132     buff[0] = 0x60 | ((int)mode << 1);
00133     buff[1] = value >> 4;
00134     buff[2] = value << 4;
00135 
00136     //Write the data
00137     m_I2C.write(m_ADDR, buff, 3);
00138 }
00139 
00140 #ifdef MBED_OPERATORS
00141 MCP4725::operator float()
00142 {
00143     //Return the current output voltage
00144     return read();
00145 }
00146 
00147 MCP4725& MCP4725::operator=(float value)
00148 {
00149     //Set the output voltage
00150     write(value);
00151     return *this;
00152 }
00153 #endif
00154 
00155 void MCP4725::readDac()
00156 {
00157     //Create a temporary buffer
00158     char buff[5];
00159 
00160     //Read the current DAC settings
00161     m_I2C.read(m_ADDR, buff, 5);
00162 
00163     //Extract the current power mode, and 12-bit DAC value
00164     m_PowerMode = (PowerMode)((buff[0] >> 1) & 0x03);
00165     m_DacValue = (buff[1] << 4) | (buff[2] >> 4);
00166 }
00167 
00168 void MCP4725::writeDac()
00169 {
00170     //Create a temporary buffer
00171     char buff[2];
00172 
00173     //Load the power mode, and 12-bit DAC value
00174     buff[0] = ((int)m_PowerMode << 4) | (m_DacValue >> 8);
00175     buff[1] = m_DacValue;
00176 
00177     //Write the data
00178     m_I2C.write(m_ADDR, buff, 2);
00179 }