Files at this revision

API Documentation at this revision

Comitter:
mcm
Date:
Fri Sep 08 18:25:52 2017 +0000
Parent:
2:e09b7dd7c1dd
Commit message:
The library was completed and tested, it works as expected.

Changed in this revision

MCP4725.cpp Show annotated file Show diff for this revision Revisions of this file
MCP4725.h Show annotated file Show diff for this revision Revisions of this file
--- a/MCP4725.cpp	Thu Sep 07 20:12:56 2017 +0000
+++ b/MCP4725.cpp	Fri Sep 08 18:25:52 2017 +0000
@@ -215,7 +215,7 @@
  * @pre         NaN
  * @warning     NaN.
  */
-MCP4725::MCP4725_status_t  MCP4725::MCP4725_SetNewValue   ( MCP4725_write_command_type_t myWriteCMD, uint32_t myDACNewValue )
+MCP4725::MCP4725_status_t  MCP4725::MCP4725_SetNewValue   ( MCP4725_write_command_type_t myWriteCMD, Vector_new_dac_value_t myDACNewValue )
 {
     char        cmd[]             =    { 0, 0, 0 };
     uint32_t    aux               =    0;
@@ -223,19 +223,19 @@
 
 
     // 12-Bit of resolution ONLY!
-    if ( myDACNewValue > 4095 )
+    if ( myDACNewValue.DAC_New_Value > 4095 )
         return  MCP4725_FAILURE;
 
 
     // Prepare the data according to the write mode
-    cmd[1]  |=  ( ( myDACNewValue & 0xFF0 ) >> 4 );
-    cmd[2]  |=  ( ( myDACNewValue & 0x00F ) << 4 );
+    cmd[1]  |=  ( ( myDACNewValue.DAC_New_Value & 0xFF0 ) >> 4 );
+    cmd[2]  |=  ( ( myDACNewValue.DAC_New_Value & 0x00F ) << 4 );
 
     switch ( myWriteCMD ){
         default:
         case FAST_MODE:
-                cmd[0]  |=  ( ( myDACNewValue & 0xF00 ) >> 8 );
-                cmd[1]   =  ( myDACNewValue & 0x0FF );
+                cmd[0]  |=  ( ( myDACNewValue.DAC_New_Value & 0xF00 ) >> 8 );
+                cmd[1]   =  ( myDACNewValue.DAC_New_Value & 0x0FF );
 
                 dataTX   =   2;
                 break;
@@ -300,3 +300,83 @@
     else
        return   MCP4725_FAILURE;
 }
+
+
+
+/**
+ * @brief       MCP4725_GetEEPROM_Data ( Vector_data_t* )
+ *
+ * @details     It gets the eeprom value.
+ *
+ * @param[in]    myEEPROMData:          EEPROM value.
+ *
+ * @param[out]   NaN.
+ *
+ *
+ * @return       Status of MCP4725_GetEEPROM_Data.
+ *
+ *
+ * @author      Manuel Caballero
+ * @date        8/September/2017
+ * @version     8/September/2017   The ORIGIN
+ * @pre         NaN
+ * @warning     NaN.
+ */
+MCP4725::MCP4725_status_t  MCP4725::MCP4725_GetEEPROM_Data  ( Vector_data_t* myEEPROMData )
+{
+    char        cmd[]             =    { 0, 0, 0, 0, 0 };
+    uint32_t    aux               =    0;
+
+    // Read command
+    aux = i2c.read ( MCP4725_Addr, &cmd[0], 5 );
+
+    // Read EEPROM value
+    myEEPROMData->EEPROM_Data =   ( ( cmd[3] & 0x0F ) << 8 ) | ( cmd[4] );
+
+
+
+    if ( aux == I2C_SUCCESS )
+       return   MCP4725_SUCCESS;
+    else
+       return   MCP4725_FAILURE;
+}
+
+
+
+/**
+ * @brief       MCP4725_GetDAC_Data ( Vector_data_t* )
+ *
+ * @details     It gets the DAC value.
+ *
+ * @param[in]    myDACData:             DAC value.
+ *
+ * @param[out]   NaN.
+ *
+ *
+ * @return       Status of MCP4725_GetDAC_Data.
+ *
+ *
+ * @author      Manuel Caballero
+ * @date        8/September/2017
+ * @version     8/September/2017   The ORIGIN
+ * @pre         NaN
+ * @warning     NaN.
+ */
+MCP4725::MCP4725_status_t  MCP4725::MCP4725_GetDAC_Data    ( Vector_data_t* myDACData )
+{
+    char        cmd[]             =    { 0, 0, 0, 0, 0 };
+    uint32_t    aux               =    0;
+
+    // Read command
+    aux = i2c.read ( MCP4725_Addr, &cmd[0], 5 );
+
+    // Read DAC value
+    myDACData->DAC_Data =   ( ( cmd[1] & 0xF0 ) << 4 ) | ( ( ( ( cmd[1] & 0x0F ) >> 4 ) | ( cmd[2] & 0xF0 ) >> 4 ) );
+
+
+
+    if ( aux == I2C_SUCCESS )
+       return   MCP4725_SUCCESS;
+    else
+       return   MCP4725_FAILURE;
+}
--- a/MCP4725.h	Thu Sep 07 20:12:56 2017 +0000
+++ b/MCP4725.h	Fri Sep 08 18:25:52 2017 +0000
@@ -22,7 +22,75 @@
 /**
     Example:
 
-[TODO]
+#include "mbed.h"
+#include "MCP4725.h"
+
+MCP4725 myDACSensor        ( I2C_SDA, I2C_SCL, MCP4725::MCP4725_ADDRESS_LOW, 400000 );
+
+
+Ticker newDACOutput;
+DigitalOut myled(LED1);
+
+MCP4725::MCP4725_status_t        aux;
+uint8_t                 myState = 0;
+
+
+void changeDATA ( void )
+{
+    MCP4725::Vector_new_dac_value_t myNewDAC_Value;
+
+    myled = 1;
+
+    switch ( myState ) {
+        default:
+        case 0:
+            // Vout ~ 0V
+            myNewDAC_Value.DAC_New_Value   =   0;
+            aux = myDACSensor.MCP4725_SetNewValue ( MCP4725::FAST_MODE, myNewDAC_Value );
+
+            myState                      =   1;
+            break;
+
+        case 1:
+            // Vout = ~ ( Vref * 0.5 )
+            myNewDAC_Value.DAC_New_Value   =   2048;
+            aux = myDACSensor.MCP4725_SetNewValue ( MCP4725::WRITE_DAC_AND_EEPROM_REGISTER_MODE, myNewDAC_Value );
+
+            myState                      =   2;
+            break;
+
+        case 2:
+            // Vout ~ Vref
+            myNewDAC_Value.DAC_New_Value   =   4095;
+            aux = myDACSensor.MCP4725_SetNewValue ( MCP4725::WRITE_DAC_REGISTER_MODE, myNewDAC_Value );
+
+            myState                      =   0;
+            break;
+    }
+
+    myled = 0;
+}
+
+
+int main()
+{
+    MCP4725::Vector_data_t           myDefaultData;
+
+
+    // Reset and wake the device up
+    aux = myDACSensor.MCP4725_Reset  ();
+    aux = myDACSensor.MCP4725_WakeUp ();
+
+    // Read the default data in both EEPROM and DAC
+    aux = myDACSensor.MCP4725_GetDAC_Data    ( &myDefaultData );
+    aux = myDACSensor.MCP4725_GetEEPROM_Data ( &myDefaultData );
+
+
+    newDACOutput.attach( &changeDATA, 0.5 );                      // the address of the function to be attached ( changeDATA ) and the interval ( 0.5s )
+
+    // Let the callbacks take care of everything
+    while(1)  sleep();
+}
 */
 
 
@@ -91,6 +159,21 @@
 
 
 
+#ifndef VECTOR_STRUCT_H
+#define VECTOR_STRUCT_H
+    typedef struct {
+        uint16_t EEPROM_Data;
+        uint16_t DAC_Data;
+    } Vector_data_t;
+
+    typedef struct {
+        uint32_t DAC_New_Value;
+    } Vector_new_dac_value_t;
+#endif
+
+
+
+
 
     /**
       * @brief   INTERNAL CONSTANTS
@@ -131,12 +214,22 @@
 
     /** It sets a new output value.
      */
-    MCP4725_status_t  MCP4725_SetNewValue                   ( MCP4725_write_command_type_t myWriteCMD, uint32_t myDACNewValue );
+    MCP4725_status_t  MCP4725_SetNewValue                   ( MCP4725_write_command_type_t myWriteCMD, Vector_new_dac_value_t myDACNewValue );
 
     /** It gets the eeprom status.
      */
     MCP4725_status_t  MCP4725_EEPROM_Status                 ( MCP4725_eeprom_status_t* myEEPROM_Status );
 
+    /** It gets the last eeprom stored value.
+     */
+    MCP4725_status_t  MCP4725_GetEEPROM_Data                ( Vector_data_t* myEEPROMData );
+
+    /** It gets the last dac stored value.
+     */
+    MCP4725_status_t  MCP4725_GetDAC_Data                   ( Vector_data_t* myDACData );
+
+
+
 
 private:
     I2C      i2c;