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.cpp
00001 /** 00002 * @brief MCP4725.c 00003 * @details 12-Bit Digital-to-Analog Converter with EEPROM Memory. 00004 * Functions 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 00017 #include "MCP4725.h" 00018 00019 00020 MCP4725::MCP4725 ( PinName sda, PinName scl, uint32_t addr, uint32_t freq ) 00021 : i2c ( sda, scl ) 00022 , MCP4725_Addr ( addr ) 00023 { 00024 i2c.frequency( freq ); 00025 } 00026 00027 00028 MCP4725::~MCP4725() 00029 { 00030 } 00031 00032 00033 00034 /** 00035 * @brief MCP4725_Reset ( void ) 00036 * 00037 * @details It performs an internal reset similar to a power-on-reset ( POR ). 00038 * 00039 * @param[in] NaN. 00040 * 00041 * @param[out] NaN. 00042 * 00043 * 00044 * @return Status of MCP4725_Reset. 00045 * 00046 * 00047 * @author Manuel Caballero 00048 * @date 7/September/2017 00049 * @version 7/September/2017 The ORIGIN 00050 * @pre NaN 00051 * @warning The user MUST respect the time it takes this instruction to be 00052 * performed ( max. 50ms ). 00053 */ 00054 MCP4725::MCP4725_status_t MCP4725::MCP4725_Reset ( void ) 00055 { 00056 char cmd = MCP4725_GENERAL_CALL_RESET; 00057 uint32_t aux = 0; 00058 00059 00060 aux = i2c.write ( MCP4725_GENERAL_CALL, &cmd, 1 ); 00061 00062 00063 00064 if ( aux == I2C_SUCCESS ) 00065 return MCP4725_SUCCESS; 00066 else 00067 return MCP4725_FAILURE; 00068 } 00069 00070 00071 00072 /** 00073 * @brief MCP4725_WakeUp ( void ) 00074 * 00075 * @details The power-down bits of the DAC register are set to a normal operation. 00076 * 00077 * @param[in] NaN. 00078 * 00079 * @param[out] NaN. 00080 * 00081 * 00082 * @return Status of MCP4725_WakeUp. 00083 * 00084 * 00085 * @author Manuel Caballero 00086 * @date 7/September/2017 00087 * @version 7/September/2017 The ORIGIN 00088 * @pre NaN 00089 * @warning NaN. 00090 */ 00091 MCP4725::MCP4725_status_t MCP4725::MCP4725_WakeUp ( void ) 00092 { 00093 char cmd = MCP4725_GENERAL_CALL_WAKE_UP; 00094 uint32_t aux = 0; 00095 00096 00097 aux = i2c.write ( MCP4725_GENERAL_CALL, &cmd, 1 ); 00098 00099 00100 00101 if ( aux == I2C_SUCCESS ) 00102 return MCP4725_SUCCESS; 00103 else 00104 return MCP4725_FAILURE; 00105 } 00106 00107 00108 /** 00109 * @brief MCP4725_PowerMode ( MCP4725_write_command_type_t , MCP4725_operation_mode_t ) 00110 * 00111 * @details It configures the power mode of the device. 00112 * 00113 * @param[in] myWriteCMD: It writes the command into the DAC or EEPROM/DAC. 00114 * @param[in] myPowerMode: Normal Mode or one of the Power-Down available modes. 00115 * 00116 * @param[out] NaN. 00117 * 00118 * 00119 * @return Status of MCP4725_PowerMode. 00120 * 00121 * 00122 * @author Manuel Caballero 00123 * @date 7/September/2017 00124 * @version 7/September/2017 The ORIGIN 00125 * @pre NaN 00126 * @warning NaN. 00127 */ 00128 MCP4725::MCP4725_status_t MCP4725::MCP4725_PowerMode ( MCP4725_write_command_type_t myWriteCMD, MCP4725_operation_mode_t myPowerMode ) 00129 { 00130 char cmd[] = { 0, 0, 0, 0, 0, 0 }; 00131 uint32_t aux = 0; 00132 uint32_t dataTX = 3; 00133 00134 00135 // Read the device to mask the default value 00136 aux = i2c.read ( MCP4725_Addr, &cmd[0], 5 ); 00137 00138 00139 00140 // Choose the power mode 00141 switch ( myPowerMode ){ 00142 default: 00143 case NORMAL_MODE : 00144 cmd[0] = 0; 00145 break; 00146 00147 case POWER_DOWN_1KOHM_RESISTIVE_LOAD_MODE : 00148 cmd[0] = 0x01; 00149 break; 00150 00151 case POWER_DOWN_100KOHM_RESISTIVE_LOAD_MODE : 00152 cmd[0] = 0x02; 00153 break; 00154 00155 case POWER_DOWN_500KOHM_RESISTIVE_LOAD_MODE : 00156 cmd[0] = 0x03; 00157 break; 00158 } 00159 00160 00161 // Prepare the data according to the write mode 00162 switch ( myWriteCMD ){ 00163 default: 00164 case FAST_MODE : 00165 cmd[0] <<= 4; 00166 cmd[0] |= ( ( cmd[1] & 0xF0 ) >> 4 ); 00167 cmd[1] = ( ( cmd[1] & 0x0F ) << 4 ); 00168 cmd[1] |= ( ( cmd[2] & 0xF0 ) >> 4 ); 00169 00170 dataTX = 2; 00171 break; 00172 00173 case WRITE_DAC_REGISTER_MODE : 00174 cmd[0] <<= 1; 00175 cmd[0] |= 0x40; 00176 break; 00177 00178 case WRITE_DAC_AND_EEPROM_REGISTER_MODE : 00179 cmd[0] <<= 1; 00180 cmd[0] |= 0x60; 00181 break; 00182 } 00183 00184 00185 00186 aux = i2c.write ( MCP4725_Addr, &cmd[0], dataTX ); 00187 00188 00189 00190 if ( aux == I2C_SUCCESS ) 00191 return MCP4725_SUCCESS; 00192 else 00193 return MCP4725_FAILURE; 00194 } 00195 00196 00197 00198 /** 00199 * @brief MCP4725_SetNewValue ( MCP4725_write_command_type_t , uint32_t ) 00200 * 00201 * @details It sends a new output value. 00202 * 00203 * @param[in] myWriteCMD: It writes the command into the DAC or EEPROM/DAC. 00204 * @param[in] myDACNewValue: New output value. 00205 * 00206 * @param[out] NaN. 00207 * 00208 * 00209 * @return Status of MCP4725_SetNewValue. 00210 * 00211 * 00212 * @author Manuel Caballero 00213 * @date 7/September/2017 00214 * @version 7/September/2017 The ORIGIN 00215 * @pre NaN 00216 * @warning NaN. 00217 */ 00218 MCP4725::MCP4725_status_t MCP4725::MCP4725_SetNewValue ( MCP4725_write_command_type_t myWriteCMD, Vector_new_dac_value_t myDACNewValue ) 00219 { 00220 char cmd[] = { 0, 0, 0 }; 00221 uint32_t aux = 0; 00222 uint32_t dataTX = 3; 00223 00224 00225 // 12-Bit of resolution ONLY! 00226 if ( myDACNewValue.DAC_New_Value > 4095 ) 00227 return MCP4725_FAILURE; 00228 00229 00230 // Prepare the data according to the write mode 00231 cmd[1] |= ( ( myDACNewValue.DAC_New_Value & 0xFF0 ) >> 4 ); 00232 cmd[2] |= ( ( myDACNewValue.DAC_New_Value & 0x00F ) << 4 ); 00233 00234 switch ( myWriteCMD ){ 00235 default: 00236 case FAST_MODE : 00237 cmd[0] |= ( ( myDACNewValue.DAC_New_Value & 0xF00 ) >> 8 ); 00238 cmd[1] = ( myDACNewValue.DAC_New_Value & 0x0FF ); 00239 00240 dataTX = 2; 00241 break; 00242 00243 case WRITE_DAC_REGISTER_MODE : 00244 cmd[0] |= 0x40; 00245 break; 00246 00247 case WRITE_DAC_AND_EEPROM_REGISTER_MODE : 00248 cmd[0] |= 0x60; 00249 break; 00250 } 00251 00252 00253 00254 aux = i2c.write ( MCP4725_Addr, &cmd[0], dataTX ); 00255 00256 00257 00258 if ( aux == I2C_SUCCESS ) 00259 return MCP4725_SUCCESS; 00260 else 00261 return MCP4725_FAILURE; 00262 } 00263 00264 00265 00266 /** 00267 * @brief MCP4725_EEPROM_Status ( MCP4725_eeprom_status_t ) 00268 * 00269 * @details It gets the eeprom status. 00270 * 00271 * @param[in] myEEPROM_Status: EEPROM status. 00272 * 00273 * @param[out] NaN. 00274 * 00275 * 00276 * @return Status of MCP4725_EEPROM_Status. 00277 * 00278 * 00279 * @author Manuel Caballero 00280 * @date 7/September/2017 00281 * @version 7/September/2017 The ORIGIN 00282 * @pre NaN 00283 * @warning NaN. 00284 */ 00285 MCP4725::MCP4725_status_t MCP4725::MCP4725_EEPROM_Status ( MCP4725_eeprom_status_t* myEEPROM_Status ) 00286 { 00287 char cmd[] = { 0, 0, 0, 0, 0 }; 00288 uint32_t aux = 0; 00289 00290 // Read command 00291 aux = i2c.read ( MCP4725_Addr, &cmd[0], 5 ); 00292 00293 // Update EEPROM status 00294 *myEEPROM_Status = ( MCP4725_eeprom_status_t )( ( cmd[0] & 0x80 ) >> 7 ); 00295 00296 00297 00298 if ( aux == I2C_SUCCESS ) 00299 return MCP4725_SUCCESS; 00300 else 00301 return MCP4725_FAILURE; 00302 } 00303 00304 00305 00306 /** 00307 * @brief MCP4725_GetEEPROM_Data ( Vector_data_t* ) 00308 * 00309 * @details It gets the eeprom value. 00310 * 00311 * @param[in] myEEPROMData: EEPROM value. 00312 * 00313 * @param[out] NaN. 00314 * 00315 * 00316 * @return Status of MCP4725_GetEEPROM_Data. 00317 * 00318 * 00319 * @author Manuel Caballero 00320 * @date 8/September/2017 00321 * @version 8/September/2017 The ORIGIN 00322 * @pre NaN 00323 * @warning NaN. 00324 */ 00325 MCP4725::MCP4725_status_t MCP4725::MCP4725_GetEEPROM_Data ( Vector_data_t* myEEPROMData ) 00326 { 00327 char cmd[] = { 0, 0, 0, 0, 0 }; 00328 uint32_t aux = 0; 00329 00330 // Read command 00331 aux = i2c.read ( MCP4725_Addr, &cmd[0], 5 ); 00332 00333 // Read EEPROM value 00334 myEEPROMData->EEPROM_Data = ( ( cmd[3] & 0x0F ) << 8 ) | ( cmd[4] ); 00335 00336 00337 00338 if ( aux == I2C_SUCCESS ) 00339 return MCP4725_SUCCESS; 00340 else 00341 return MCP4725_FAILURE; 00342 } 00343 00344 00345 00346 /** 00347 * @brief MCP4725_GetDAC_Data ( Vector_data_t* ) 00348 * 00349 * @details It gets the DAC value. 00350 * 00351 * @param[in] myDACData: DAC value. 00352 * 00353 * @param[out] NaN. 00354 * 00355 * 00356 * @return Status of MCP4725_GetDAC_Data. 00357 * 00358 * 00359 * @author Manuel Caballero 00360 * @date 8/September/2017 00361 * @version 8/September/2017 The ORIGIN 00362 * @pre NaN 00363 * @warning NaN. 00364 */ 00365 MCP4725::MCP4725_status_t MCP4725::MCP4725_GetDAC_Data ( Vector_data_t* myDACData ) 00366 { 00367 char cmd[] = { 0, 0, 0, 0, 0 }; 00368 uint32_t aux = 0; 00369 00370 // Read command 00371 aux = i2c.read ( MCP4725_Addr, &cmd[0], 5 ); 00372 00373 // Read DAC value 00374 myDACData->DAC_Data = ( ( cmd[1] & 0xF0 ) << 4 ) | ( ( ( ( cmd[1] & 0x0F ) >> 4 ) | ( cmd[2] & 0xF0 ) >> 4 ) ); 00375 00376 00377 00378 if ( aux == I2C_SUCCESS ) 00379 return MCP4725_SUCCESS; 00380 else 00381 return MCP4725_FAILURE; 00382 }
Generated on Wed Jul 20 2022 10:49:43 by
1.7.2