A collection of Analog Devices drivers for the mbed platform

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CN0398.h Source File

CN0398.h

00001 #ifndef _CN0398_H_
00002 #define _CN0398_H_
00003 #include "AD7124.h"
00004 
00005 /**
00006  * @brief Calibration solutions enum
00007  */
00008 enum {
00009     ACETATE,
00010     BORATE,
00011     CAOH2,
00012     CARBONATE,
00013     CITRATE,
00014     HCL,
00015     OXALATE,
00016     PHOSPHATE0,
00017     PHOSPHATE1,
00018     PHOSPHATE2,
00019     PHTHALATE,
00020     TARTRATE,
00021     TRIS,
00022     PH4,
00023     PH10,
00024     NUMBER_OF_SOLUTIONS
00025 };
00026 
00027 /**
00028  * @brief Calibration solutions strings
00029  */
00030 const char solutions[NUMBER_OF_SOLUTIONS][20] = {
00031     "ACETATE",
00032     "BORATE",
00033     "CAOH2",
00034     "CARBONATE",
00035     "CITRATE",
00036     "HCL",
00037     "OXALATE",
00038     "PHOSPHATE0",
00039     "PHOSPHATE1",
00040     "PHOSPHATE2",
00041     "PHTHALATE",
00042     "TARTRATE",
00043     "TRIS",
00044     "PH4",
00045     "PH10"
00046 };
00047 #define NUMBER_OF_TEMPERATURE_ENTRIES 31
00048 
00049 // *INDENT-OFF*
00050 /**
00051  * @brief Calibration temperatures
00052  */
00053 const uint8_t ph_temperatures[NUMBER_OF_TEMPERATURE_ENTRIES] =
00054 {
00055         0 ,     5 ,     10,     15,     18,     19,     20,     21,     22,     23,     24,     25,
00056         26,     27,     28,     29,     30,     35,     37,     40,     45,     50,     55,     60,
00057         65,     70,     75,     80,     85,     90,     95,
00058 };
00059 
00060 /**
00061  * @brief Calibration solutions temperature to ph look-up tables
00062  */
00063 const float ph_temp_lut[NUMBER_OF_SOLUTIONS][NUMBER_OF_TEMPERATURE_ENTRIES]
00064 {
00065 /* ACETATE    */ {4.667, 4.66, 4.655, 4.652, 4.651, 4.651, 4.65, 4.65, 4.65, 4.65, 4.65, 4.65, 4.65, 4.651, 4.651, 4.651, 4.652, 4.655, 4.656, 4.659, 4.666, 4.673, 4.683, 4.694, 4.706, 4.72, 4.736, 4.753, 4.772, 4.793, 4.815},
00066 /* BORATE     */ {9.464, 9.395, 9.332, 9.276, 9.245, 9.235, 9.225, 9.216, 9.207, 9.197, 9.189, 9.18, 9.171, 9.163, 9.155, 9.147, 9.139, 9.102, 9.088, 9.068, 9.038, 9.01, 8.985, 8.962, 8.941, 8.921, 8.902, 8.884, 8.867, 8.85, 8.833},
00067 /* CAOH2      */ {13.424, 13.207, 13.003, 12.81, 12.699, 12.663, 12.627, 12.592, 12.557, 12.522, 12.488, 12.454, 12.42, 12.387, 12.354, 12.322, 12.289, 12.133, 12.072, 11.984, 11.841, 11.705, 11.574, 11.449  },
00068 /* CARBONATE  */ {10.317, 10.245, 10.179, 10.118, 10.084, 10.073, 10.062, 10.052, 10.042, 10.032, 10.022, 10.012, 10.002, 9.993, 9.984, 9.975, 9.966, 9.925, 9.91, 9.889, 9.857, 9.828},
00069 /* CITRATE    */ {3.863, 3.84, 3.82, 3.803, 3.793, 3.791, 3.788, 3.785, 3.783, 3.78, 3.778, 3.776, 3.774, 3.772, 3.77, 3.768, 3.766, 3.759, 3.756, 3.754, 3.75, 3.749},
00070 /* HCL        */ {1.082, 1.085, 1.087, 1.089, 1.09, 1.091, 1.091, 1.092, 1.092, 1.093, 1.093, 1.094, 1.094, 1.094, 1.095, 1.095, 1.096, 1.098, 1.099, 1.101, 1.103, 1.106, 1.108, 1.111, 1.113, 1.116, 1.119, 1.121, 1.124, 1.127, 1.13},
00071 /* OXALATE    */ {1.666, 1.668, 1.67, 1.672, 1.674, 1.675, 1.675, 1.676, 1.677, 1.678, 1.678, 1.679, 1.68, 1.681, 1.681, 1.682, 1.683, 1.688, 1.69, 1.694, 1.7, 1.707, 1.715, 1.723, 1.732, 1.743, 1.754, 1.765, 1.778, 1.792, 1.806},
00072 /* PHOSPHATE0 */ {6.984, 6.951, 6.923, 6.9, 6.888, 6.884, 6.881, 6.877, 6.874, 6.871, 6.868, 6.865, 6.862, 6.86, 6.857, 6.855, 6.853, 6.844, 6.841, 6.838, 6.834, 6.833, 6.833, 6.836, 6.84, 6.845, 6.852, 6.859, 6.867, 6.876, 6.886},
00073 /* PHOSPHATE1 */ {7.118, 7.087, 7.059, 7.036, 7.024, 7.02, 7.016, 7.013, 7.009, 7.006, 7.003, 7, 6.997, 6.994, 6.992, 6.989, 6.987, 6.977, 6.974, 6.97, 6.965, 6.964, 6.965, 6.968, 6.974, 6.982, 6.992, 7.004, 7.018, 7.034, 7.052},
00074 /* PHOSPHATE2 */ {7.534, 7.5, 7.472, 7.448, 7.436, 7.432, 7.429, 7.425, 7.422, 7.419, 7.416, 7.413, 7.41, 7.407, 7.405, 7.402, 7.4, 7.389, 7.386, 7.38, 7.373, 7.367},
00075 /* PHTHALATE  */ {4, 3.998, 3.997, 3.998, 3.999, 4, 4.001, 4.001, 4.002, 4.003, 4.004, 4.005, 4.006, 4.007, 4.008, 4.009, 4.011, 4.018, 4.022, 4.027, 4.038, 4.05, 4.064, 4.08, 4.097, 4.116, 4.137, 4.159, 4.183, 4.208, 4.235},
00076 /* TARTRATE   */ {3.557, 3.557, 3.557, 3.557, 3.557, 3.557, 3.557, 3.557, 3.557, 3.557, 3.557, 3.557, 3.556, 3.555, 3.554, 3.553, 3.552, 3.549, 3.548, 3.547, 3.547, 3.549, 3.554, 3.56, 3.569, 3.58, 3.593, 3.61, 3.628, 3.65, 3.675},
00077 /* TRIS       */ {8.471, 8.303, 8.142, 7.988, 7.899, 7.869, 7.84, 7.812, 7.783, 7.755, 7.727, 7.699, 7.671, 7.644, 7.617, 7.59, 7.563, 7.433, 7.382, 7.307, 7.186, 7.07},
00078 /* PH4        */ {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
00079 /* PH10       */ {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
00080 };
00081 // *INDENT-ON*
00082 
00083 #define TEMPERATURE_SENSOR_PRESENT
00084 #define MOISTURE_SENSOR_PRESENT
00085 #define PH_SENSOR_PRESENT
00086 
00087 //#define USE_LINEAR_TEMP_EQ
00088 //#define USE_MANUFACTURER_MOISTURE_EQ
00089 
00090 
00091 #define ZERO_POINT_TOLERANCE (0.003)
00092 #define PH_ISO (7)
00093 #define AVOGADRO (8.314)
00094 #define FARADAY_CONSTANT (96485.0)
00095 #define KELVIN_OFFSET (273.1)
00096 
00097 
00098 
00099 
00100 /**
00101  * @brief the CN0398 shield class
00102  */
00103 class CN0398
00104 {
00105 private:
00106 public:
00107     /**
00108      * @brief CN0398 constructor
00109      * @param cs - CN0398 external ADC chip select pin
00110      * @param swctrl0 - CN0398 ADP7118 enable pin
00111      */
00112     CN0398(PinName cs, PinName swctrl0);
00113 
00114     /**
00115      * @brief reads the temperature sensor
00116      * @return temperature
00117      */
00118     float read_rtd();
00119     /**
00120      * @brief reads the pH sensor
00121      * @param temperature(optional) - environment temperature
00122      * @return reading of the pH sensor corrected with temperature(if provided)
00123      */
00124     float read_ph(float temperature = 25.0);
00125 
00126     /**
00127      * @brief reads the moisture sensor
00128      * @return reading of the moisture sensor
00129      */
00130     float read_moist();
00131 
00132     typedef enum {
00133         P1 = 0,
00134         P2 = 1,
00135         P3 = 2,
00136         P4 = 3
00137     } ad_digital_output_t;
00138 
00139     /**
00140      * @brief reads the ADC channel
00141      * @param ch channel to be read
00142      * @return ADC reading in counts
00143      */
00144     int32_t read_channel(uint8_t ch);
00145 
00146     /**
00147      * @brief converts counts to voltage - unipolar conversion
00148      * @param data in counts
00149      * @param gain(optional) - default 1
00150      * @param VREF(optional) - default 2.5 - reference voltage
00151      * @return voltage
00152      */
00153     float data_to_voltage(uint32_t data, uint8_t gain = 1, float VREF = 2.5);
00154 
00155     /**
00156     * @brief converts counts to voltage - bipolar conversion
00157     * @param data in counts
00158     * @param gain(optional) - default 1
00159     * @param VREF(optional) - default 2.5 - reference voltage
00160     * @return voltage
00161     */
00162     float data_to_voltage_bipolar(uint32_t data, uint8_t gain = 1, float VREF = 2.5);
00163 
00164     /**
00165      *  @brief enables an ADC channel
00166      *  @param channel - channel to be enabled
00167      */
00168     void enable_channel(int channel);
00169     /**
00170       * @brief disables an ADC channel
00171       * @param channel - channel to be disabled
00172       */
00173     void disable_channel(int channel);
00174 
00175     /**
00176      * @brief performs pt 0 calibration. ph probe should be in calibration solution before calling this method
00177      * @param temp - environment temperature
00178      */
00179     void calibrate_ph_pt0(float temperature = 25.0);
00180 
00181     /**
00182     * @brief performs pt 1 calibration. ph probe should be in calibration solution before calling this method
00183     * @param temp - environment temperature
00184     */
00185     void calibrate_ph_pt1(float temperature = 25.0);
00186 
00187     /**
00188      * @brief performs offset calibration. pH probes should be shorted before calling this method
00189      *
00190      */
00191     void calibrate_ph_offset();
00192 
00193     /* void enable_current_source0(int current_source_channel);
00194      void enable_current_source1(int current_source_channel);*/
00195 
00196     /**
00197      * @brief toggles the digital outputs on or off
00198      * @param p - digital output
00199      * @param state - state of the output
00200      */
00201     void set_digital_output(ad_digital_output_t p, bool state);
00202 
00203     /**
00204      * @brief triggers ADC start of single conversion
00205      */
00206     void start_single_conversion();
00207 
00208     /**
00209      * @brief resets the digital interface of the ADC
00210      */
00211     void reset();
00212 
00213     /**
00214      * @brief initializes the ADC
00215      */
00216     void setup();
00217 
00218     /**
00219      *  @brief configures the ADC for the CN0398 application
00220      */
00221     void init();
00222 
00223     AD7124 ad7124;
00224     DigitalOut ADP7118Enable;
00225 
00226     bool use_nernst = false;
00227     const float default_offset_voltage = 0;
00228     const uint16_t SENSOR_SETTLING_TIME = 400; /*in ms*/
00229     float offset_voltage;
00230     float default_calibration_ph[2][2] = {{4, 0.169534}, {10,  -0.134135}};
00231     float calibration_ph[2][2];
00232     uint8_t solution0, solution1;
00233 
00234 };
00235 #endif