Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MCP4725.h
00001 /** 00002 * @brief MCP4725.h 00003 * @details 12-Bit Digital-to-Analog Converter with EEPROM Memory. 00004 * Header file. 00005 * 00006 * 00007 * @return NA 00008 * 00009 * @author Manuel Caballero 00010 * @date 7/September/2017 00011 * @version 7/September/2017 The ORIGIN 00012 * @pre NaN. 00013 * @warning NaN 00014 * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ). 00015 */ 00016 #ifndef MCP4725_H 00017 #define MCP4725_H 00018 00019 #include "mbed.h" 00020 00021 00022 /** 00023 Example: 00024 00025 #include "mbed.h" 00026 #include "MCP4725.h" 00027 00028 MCP4725 myDACSensor ( I2C_SDA, I2C_SCL, MCP4725::MCP4725_ADDRESS_LOW, 400000 ); 00029 00030 00031 Ticker newDACOutput; 00032 DigitalOut myled(LED1); 00033 00034 MCP4725::MCP4725_status_t aux; 00035 uint8_t myState = 0; 00036 00037 00038 void changeDATA ( void ) 00039 { 00040 MCP4725::Vector_new_dac_value_t myNewDAC_Value; 00041 00042 myled = 1; 00043 00044 switch ( myState ) { 00045 default: 00046 case 0: 00047 // Vout ~ 0V 00048 myNewDAC_Value.DAC_New_Value = 0; 00049 aux = myDACSensor.MCP4725_SetNewValue ( MCP4725::FAST_MODE, myNewDAC_Value ); 00050 00051 myState = 1; 00052 break; 00053 00054 case 1: 00055 // Vout = ~ ( Vref * 0.5 ) 00056 myNewDAC_Value.DAC_New_Value = 2048; 00057 aux = myDACSensor.MCP4725_SetNewValue ( MCP4725::WRITE_DAC_AND_EEPROM_REGISTER_MODE, myNewDAC_Value ); 00058 00059 myState = 2; 00060 break; 00061 00062 case 2: 00063 // Vout ~ Vref 00064 myNewDAC_Value.DAC_New_Value = 4095; 00065 aux = myDACSensor.MCP4725_SetNewValue ( MCP4725::WRITE_DAC_REGISTER_MODE, myNewDAC_Value ); 00066 00067 myState = 0; 00068 break; 00069 } 00070 00071 myled = 0; 00072 } 00073 00074 00075 int main() 00076 { 00077 MCP4725::Vector_data_t myDefaultData; 00078 00079 00080 // Reset and wake the device up 00081 aux = myDACSensor.MCP4725_Reset (); 00082 aux = myDACSensor.MCP4725_WakeUp (); 00083 00084 // Read the default data in both EEPROM and DAC 00085 aux = myDACSensor.MCP4725_GetDAC_Data ( &myDefaultData ); 00086 aux = myDACSensor.MCP4725_GetEEPROM_Data ( &myDefaultData ); 00087 00088 00089 newDACOutput.attach( &changeDATA, 0.5 ); // the address of the function to be attached ( changeDATA ) and the interval ( 0.5s ) 00090 00091 // Let the callbacks take care of everything 00092 while(1) sleep(); 00093 } 00094 */ 00095 00096 00097 /*! 00098 Library for the MCP4725 12-Bit Digital-to-Analog Converter with EEPROM Memory. 00099 */ 00100 class MCP4725 00101 { 00102 public: 00103 /** 00104 * @brief DEFAULT ADDRESSES ( NOTE1: The A2 and A1 are programmed to '00' (default), if not requested by customer. 00105 * NOTE2: On my board, the A2 and A1 are programmed to '01'. ) 00106 */ 00107 typedef enum { 00108 MCP4725_ADDRESS_LOW = ( 0x62 << 1 ), /*!< A0 pin ties to GND */ 00109 MCP4725_ADDRESS_HIGH = ( 0x63 << 1 ) /*!< A0 pin ties to VDD */ 00110 } MCP4725_address_t; 00111 00112 00113 /** 00114 * @brief COMMANDS 00115 */ 00116 #define MCP4725_GENERAL_CALL 0x00 /*!< The MCP4725 device acknowledges the general call address */ 00117 00118 /* General Call Commands */ 00119 /** 00120 * @brief GENERAL CALL COMMANDS 00121 */ 00122 #define MCP4725_GENERAL_CALL_RESET 0x06 /*!< Perform an internal reset similar to a power-on-reset (POR). */ 00123 #define MCP4725_GENERAL_CALL_WAKE_UP 0x09 /*!< The power-down bits of the DAC register are set to a normal operation. */ 00124 00125 00126 00127 /* Commands Registers */ 00128 /** 00129 * @brief WRITE COMMAND TYPE 00130 */ 00131 typedef enum { 00132 FAST_MODE = 0, /*!< This command is used to change the DAC register. EEPROM is not affected. */ 00133 WRITE_DAC_REGISTER_MODE = 1, /*!< Load configuration bits and data code to the DAC Register. */ 00134 WRITE_DAC_AND_EEPROM_REGISTER_MODE = 2 /*!< Load configuration bits and data code to the DAC Register and also write the EEPROM. */ 00135 } MCP4725_write_command_type_t; 00136 00137 00138 00139 /** 00140 * @brief POWER-DOWN MODE 00141 */ 00142 typedef enum { 00143 NORMAL_MODE = 0, /*!< Normal Mode. */ 00144 POWER_DOWN_1KOHM_RESISTIVE_LOAD_MODE = 1, /*!< Power-Down Mode. 1 kΩ resistor to ground. */ 00145 POWER_DOWN_100KOHM_RESISTIVE_LOAD_MODE = 2, /*!< Power-Down Mode. 100 kΩ resistor to ground. */ 00146 POWER_DOWN_500KOHM_RESISTIVE_LOAD_MODE = 3 /*!< Power-Down Mode. 500 kΩ resistor to ground. */ 00147 } MCP4725_operation_mode_t; 00148 00149 00150 00151 /** 00152 * @brief READY/#BUSY BIT 00153 */ 00154 typedef enum { 00155 EEPROM_BUSY = 0, /*!< EEPROM write is not completed. */ 00156 EEPROM_READY = 1 /*!< EEPROM write is complete. */ 00157 } MCP4725_eeprom_status_t; 00158 00159 00160 00161 00162 #ifndef VECTOR_STRUCT_H 00163 #define VECTOR_STRUCT_H 00164 typedef struct { 00165 uint16_t EEPROM_Data; 00166 uint16_t DAC_Data; 00167 } Vector_data_t; 00168 00169 typedef struct { 00170 uint32_t DAC_New_Value; 00171 } Vector_new_dac_value_t; 00172 #endif 00173 00174 00175 00176 00177 00178 /** 00179 * @brief INTERNAL CONSTANTS 00180 */ 00181 typedef enum { 00182 MCP4725_SUCCESS = 0, 00183 MCP4725_FAILURE = 1, 00184 I2C_SUCCESS = 0 /*!< I2C communication was fine */ 00185 } MCP4725_status_t; 00186 00187 00188 00189 00190 /** Create an MCP4725 object connected to the specified I2C pins. 00191 * 00192 * @param sda I2C data pin 00193 * @param scl I2C clock pin 00194 * @param addr I2C slave address 00195 * @param freq I2C frequency in Hz. 00196 */ 00197 MCP4725 ( PinName sda, PinName scl, uint32_t addr, uint32_t freq ); 00198 00199 /** Delete MCP4725 object. 00200 */ 00201 ~MCP4725(); 00202 00203 /** It performs an internal reset similar to a power-on-reset ( POR ). 00204 */ 00205 MCP4725_status_t MCP4725_Reset ( void ); 00206 00207 /** The power-down bits of the DAC register are set to a normal operation. 00208 */ 00209 MCP4725_status_t MCP4725_WakeUp ( void ); 00210 00211 /** It configures the power mode of the device. 00212 */ 00213 MCP4725_status_t MCP4725_PowerMode ( MCP4725_write_command_type_t myWriteCMD, MCP4725_operation_mode_t myPowerMode ); 00214 00215 /** It sets a new output value. 00216 */ 00217 MCP4725_status_t MCP4725_SetNewValue ( MCP4725_write_command_type_t myWriteCMD, Vector_new_dac_value_t myDACNewValue ); 00218 00219 /** It gets the eeprom status. 00220 */ 00221 MCP4725_status_t MCP4725_EEPROM_Status ( MCP4725_eeprom_status_t* myEEPROM_Status ); 00222 00223 /** It gets the last eeprom stored value. 00224 */ 00225 MCP4725_status_t MCP4725_GetEEPROM_Data ( Vector_data_t* myEEPROMData ); 00226 00227 /** It gets the last dac stored value. 00228 */ 00229 MCP4725_status_t MCP4725_GetDAC_Data ( Vector_data_t* myDACData ); 00230 00231 00232 00233 00234 private: 00235 I2C i2c; 00236 uint32_t MCP4725_Addr; 00237 }; 00238 00239 #endif
Generated on Wed Jul 20 2022 10:49:43 by
1.7.2