A collection of Analog Devices drivers for the mbed platform

For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all

Committer:
Adrian Suciu
Date:
Mon Oct 24 18:15:23 2016 +0300
Revision:
30:990ce210e8c2
Added example and library for the CN0398

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Adrian Suciu 30:990ce210e8c2 1 #ifndef _CN0398_H_
Adrian Suciu 30:990ce210e8c2 2 #define _CN0398_H_
Adrian Suciu 30:990ce210e8c2 3 #include "AD7124.h"
Adrian Suciu 30:990ce210e8c2 4
Adrian Suciu 30:990ce210e8c2 5 /**
Adrian Suciu 30:990ce210e8c2 6 * @brief Calibration solutions enum
Adrian Suciu 30:990ce210e8c2 7 */
Adrian Suciu 30:990ce210e8c2 8 enum {
Adrian Suciu 30:990ce210e8c2 9 ACETATE,
Adrian Suciu 30:990ce210e8c2 10 BORATE,
Adrian Suciu 30:990ce210e8c2 11 CAOH2,
Adrian Suciu 30:990ce210e8c2 12 CARBONATE,
Adrian Suciu 30:990ce210e8c2 13 CITRATE,
Adrian Suciu 30:990ce210e8c2 14 HCL,
Adrian Suciu 30:990ce210e8c2 15 OXALATE,
Adrian Suciu 30:990ce210e8c2 16 PHOSPHATE0,
Adrian Suciu 30:990ce210e8c2 17 PHOSPHATE1,
Adrian Suciu 30:990ce210e8c2 18 PHOSPHATE2,
Adrian Suciu 30:990ce210e8c2 19 PHTHALATE,
Adrian Suciu 30:990ce210e8c2 20 TARTRATE,
Adrian Suciu 30:990ce210e8c2 21 TRIS,
Adrian Suciu 30:990ce210e8c2 22 PH4,
Adrian Suciu 30:990ce210e8c2 23 PH10,
Adrian Suciu 30:990ce210e8c2 24 NUMBER_OF_SOLUTIONS
Adrian Suciu 30:990ce210e8c2 25 };
Adrian Suciu 30:990ce210e8c2 26
Adrian Suciu 30:990ce210e8c2 27 /**
Adrian Suciu 30:990ce210e8c2 28 * @brief Calibration solutions strings
Adrian Suciu 30:990ce210e8c2 29 */
Adrian Suciu 30:990ce210e8c2 30 const char solutions[NUMBER_OF_SOLUTIONS][20] = {
Adrian Suciu 30:990ce210e8c2 31 "ACETATE",
Adrian Suciu 30:990ce210e8c2 32 "BORATE",
Adrian Suciu 30:990ce210e8c2 33 "CAOH2",
Adrian Suciu 30:990ce210e8c2 34 "CARBONATE",
Adrian Suciu 30:990ce210e8c2 35 "CITRATE",
Adrian Suciu 30:990ce210e8c2 36 "HCL",
Adrian Suciu 30:990ce210e8c2 37 "OXALATE",
Adrian Suciu 30:990ce210e8c2 38 "PHOSPHATE0",
Adrian Suciu 30:990ce210e8c2 39 "PHOSPHATE1",
Adrian Suciu 30:990ce210e8c2 40 "PHOSPHATE2",
Adrian Suciu 30:990ce210e8c2 41 "PHTHALATE",
Adrian Suciu 30:990ce210e8c2 42 "TARTRATE",
Adrian Suciu 30:990ce210e8c2 43 "TRIS",
Adrian Suciu 30:990ce210e8c2 44 "PH4",
Adrian Suciu 30:990ce210e8c2 45 "PH10"
Adrian Suciu 30:990ce210e8c2 46 };
Adrian Suciu 30:990ce210e8c2 47 #define NUMBER_OF_TEMPERATURE_ENTRIES 31
Adrian Suciu 30:990ce210e8c2 48
Adrian Suciu 30:990ce210e8c2 49 // *INDENT-OFF*
Adrian Suciu 30:990ce210e8c2 50 /**
Adrian Suciu 30:990ce210e8c2 51 * @brief Calibration temperatures
Adrian Suciu 30:990ce210e8c2 52 */
Adrian Suciu 30:990ce210e8c2 53 const uint8_t ph_temperatures[NUMBER_OF_TEMPERATURE_ENTRIES] =
Adrian Suciu 30:990ce210e8c2 54 {
Adrian Suciu 30:990ce210e8c2 55 0 , 5 , 10, 15, 18, 19, 20, 21, 22, 23, 24, 25,
Adrian Suciu 30:990ce210e8c2 56 26, 27, 28, 29, 30, 35, 37, 40, 45, 50, 55, 60,
Adrian Suciu 30:990ce210e8c2 57 65, 70, 75, 80, 85, 90, 95,
Adrian Suciu 30:990ce210e8c2 58 };
Adrian Suciu 30:990ce210e8c2 59
Adrian Suciu 30:990ce210e8c2 60 /**
Adrian Suciu 30:990ce210e8c2 61 * @brief Calibration solutions temperature to ph look-up tables
Adrian Suciu 30:990ce210e8c2 62 */
Adrian Suciu 30:990ce210e8c2 63 const float ph_temp_lut[NUMBER_OF_SOLUTIONS][NUMBER_OF_TEMPERATURE_ENTRIES]
Adrian Suciu 30:990ce210e8c2 64 {
Adrian Suciu 30:990ce210e8c2 65 /* 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},
Adrian Suciu 30:990ce210e8c2 66 /* 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},
Adrian Suciu 30:990ce210e8c2 67 /* 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 },
Adrian Suciu 30:990ce210e8c2 68 /* 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},
Adrian Suciu 30:990ce210e8c2 69 /* 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},
Adrian Suciu 30:990ce210e8c2 70 /* 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},
Adrian Suciu 30:990ce210e8c2 71 /* 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},
Adrian Suciu 30:990ce210e8c2 72 /* 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},
Adrian Suciu 30:990ce210e8c2 73 /* 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},
Adrian Suciu 30:990ce210e8c2 74 /* 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},
Adrian Suciu 30:990ce210e8c2 75 /* 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},
Adrian Suciu 30:990ce210e8c2 76 /* 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},
Adrian Suciu 30:990ce210e8c2 77 /* 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},
Adrian Suciu 30:990ce210e8c2 78 /* 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 },
Adrian Suciu 30:990ce210e8c2 79 /* 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 },
Adrian Suciu 30:990ce210e8c2 80 };
Adrian Suciu 30:990ce210e8c2 81 // *INDENT-ON*
Adrian Suciu 30:990ce210e8c2 82
Adrian Suciu 30:990ce210e8c2 83 #define TEMPERATURE_SENSOR_PRESENT
Adrian Suciu 30:990ce210e8c2 84 #define MOISTURE_SENSOR_PRESENT
Adrian Suciu 30:990ce210e8c2 85 #define PH_SENSOR_PRESENT
Adrian Suciu 30:990ce210e8c2 86
Adrian Suciu 30:990ce210e8c2 87 //#define USE_LINEAR_TEMP_EQ
Adrian Suciu 30:990ce210e8c2 88 //#define USE_MANUFACTURER_MOISTURE_EQ
Adrian Suciu 30:990ce210e8c2 89
Adrian Suciu 30:990ce210e8c2 90
Adrian Suciu 30:990ce210e8c2 91 #define ZERO_POINT_TOLERANCE (0.003)
Adrian Suciu 30:990ce210e8c2 92 #define PH_ISO (7)
Adrian Suciu 30:990ce210e8c2 93 #define AVOGADRO (8.314)
Adrian Suciu 30:990ce210e8c2 94 #define FARADAY_CONSTANT (96485.0)
Adrian Suciu 30:990ce210e8c2 95 #define KELVIN_OFFSET (273.1)
Adrian Suciu 30:990ce210e8c2 96
Adrian Suciu 30:990ce210e8c2 97
Adrian Suciu 30:990ce210e8c2 98
Adrian Suciu 30:990ce210e8c2 99
Adrian Suciu 30:990ce210e8c2 100 /**
Adrian Suciu 30:990ce210e8c2 101 * @brief the CN0398 shield class
Adrian Suciu 30:990ce210e8c2 102 */
Adrian Suciu 30:990ce210e8c2 103 class CN0398
Adrian Suciu 30:990ce210e8c2 104 {
Adrian Suciu 30:990ce210e8c2 105 private:
Adrian Suciu 30:990ce210e8c2 106 public:
Adrian Suciu 30:990ce210e8c2 107 /**
Adrian Suciu 30:990ce210e8c2 108 * @brief CN0398 constructor
Adrian Suciu 30:990ce210e8c2 109 * @param cs - CN0398 external ADC chip select pin
Adrian Suciu 30:990ce210e8c2 110 * @param swctrl0 - CN0398 ADP7118 enable pin
Adrian Suciu 30:990ce210e8c2 111 */
Adrian Suciu 30:990ce210e8c2 112 CN0398(PinName cs, PinName swctrl0);
Adrian Suciu 30:990ce210e8c2 113
Adrian Suciu 30:990ce210e8c2 114 /**
Adrian Suciu 30:990ce210e8c2 115 * @brief reads the temperature sensor
Adrian Suciu 30:990ce210e8c2 116 * @return temperature
Adrian Suciu 30:990ce210e8c2 117 */
Adrian Suciu 30:990ce210e8c2 118 float read_rtd();
Adrian Suciu 30:990ce210e8c2 119 /**
Adrian Suciu 30:990ce210e8c2 120 * @brief reads the pH sensor
Adrian Suciu 30:990ce210e8c2 121 * @param temperature(optional) - environment temperature
Adrian Suciu 30:990ce210e8c2 122 * @return reading of the pH sensor corrected with temperature(if provided)
Adrian Suciu 30:990ce210e8c2 123 */
Adrian Suciu 30:990ce210e8c2 124 float read_ph(float temperature = 25.0);
Adrian Suciu 30:990ce210e8c2 125
Adrian Suciu 30:990ce210e8c2 126 /**
Adrian Suciu 30:990ce210e8c2 127 * @brief reads the moisture sensor
Adrian Suciu 30:990ce210e8c2 128 * @return reading of the moisture sensor
Adrian Suciu 30:990ce210e8c2 129 */
Adrian Suciu 30:990ce210e8c2 130 float read_moist();
Adrian Suciu 30:990ce210e8c2 131
Adrian Suciu 30:990ce210e8c2 132 typedef enum {
Adrian Suciu 30:990ce210e8c2 133 P1 = 0,
Adrian Suciu 30:990ce210e8c2 134 P2 = 1,
Adrian Suciu 30:990ce210e8c2 135 P3 = 2,
Adrian Suciu 30:990ce210e8c2 136 P4 = 3
Adrian Suciu 30:990ce210e8c2 137 } ad_digital_output_t;
Adrian Suciu 30:990ce210e8c2 138
Adrian Suciu 30:990ce210e8c2 139 /**
Adrian Suciu 30:990ce210e8c2 140 * @brief reads the ADC channel
Adrian Suciu 30:990ce210e8c2 141 * @param ch channel to be read
Adrian Suciu 30:990ce210e8c2 142 * @return ADC reading in counts
Adrian Suciu 30:990ce210e8c2 143 */
Adrian Suciu 30:990ce210e8c2 144 int32_t read_channel(uint8_t ch);
Adrian Suciu 30:990ce210e8c2 145
Adrian Suciu 30:990ce210e8c2 146 /**
Adrian Suciu 30:990ce210e8c2 147 * @brief converts counts to voltage - unipolar conversion
Adrian Suciu 30:990ce210e8c2 148 * @param data in counts
Adrian Suciu 30:990ce210e8c2 149 * @param gain(optional) - default 1
Adrian Suciu 30:990ce210e8c2 150 * @param VREF(optional) - default 2.5 - reference voltage
Adrian Suciu 30:990ce210e8c2 151 * @return voltage
Adrian Suciu 30:990ce210e8c2 152 */
Adrian Suciu 30:990ce210e8c2 153 float data_to_voltage(uint32_t data, uint8_t gain = 1, float VREF = 2.5);
Adrian Suciu 30:990ce210e8c2 154
Adrian Suciu 30:990ce210e8c2 155 /**
Adrian Suciu 30:990ce210e8c2 156 * @brief converts counts to voltage - bipolar conversion
Adrian Suciu 30:990ce210e8c2 157 * @param data in counts
Adrian Suciu 30:990ce210e8c2 158 * @param gain(optional) - default 1
Adrian Suciu 30:990ce210e8c2 159 * @param VREF(optional) - default 2.5 - reference voltage
Adrian Suciu 30:990ce210e8c2 160 * @return voltage
Adrian Suciu 30:990ce210e8c2 161 */
Adrian Suciu 30:990ce210e8c2 162 float data_to_voltage_bipolar(uint32_t data, uint8_t gain = 1, float VREF = 2.5);
Adrian Suciu 30:990ce210e8c2 163
Adrian Suciu 30:990ce210e8c2 164 /**
Adrian Suciu 30:990ce210e8c2 165 * @brief enables an ADC channel
Adrian Suciu 30:990ce210e8c2 166 * @param channel - channel to be enabled
Adrian Suciu 30:990ce210e8c2 167 */
Adrian Suciu 30:990ce210e8c2 168 void enable_channel(int channel);
Adrian Suciu 30:990ce210e8c2 169 /**
Adrian Suciu 30:990ce210e8c2 170 * @brief disables an ADC channel
Adrian Suciu 30:990ce210e8c2 171 * @param channel - channel to be disabled
Adrian Suciu 30:990ce210e8c2 172 */
Adrian Suciu 30:990ce210e8c2 173 void disable_channel(int channel);
Adrian Suciu 30:990ce210e8c2 174
Adrian Suciu 30:990ce210e8c2 175 /**
Adrian Suciu 30:990ce210e8c2 176 * @brief performs pt 0 calibration. ph probe should be in calibration solution before calling this method
Adrian Suciu 30:990ce210e8c2 177 * @param temp - environment temperature
Adrian Suciu 30:990ce210e8c2 178 */
Adrian Suciu 30:990ce210e8c2 179 void calibrate_ph_pt0(float temperature = 25.0);
Adrian Suciu 30:990ce210e8c2 180
Adrian Suciu 30:990ce210e8c2 181 /**
Adrian Suciu 30:990ce210e8c2 182 * @brief performs pt 1 calibration. ph probe should be in calibration solution before calling this method
Adrian Suciu 30:990ce210e8c2 183 * @param temp - environment temperature
Adrian Suciu 30:990ce210e8c2 184 */
Adrian Suciu 30:990ce210e8c2 185 void calibrate_ph_pt1(float temperature = 25.0);
Adrian Suciu 30:990ce210e8c2 186
Adrian Suciu 30:990ce210e8c2 187 /**
Adrian Suciu 30:990ce210e8c2 188 * @brief performs offset calibration. pH probes should be shorted before calling this method
Adrian Suciu 30:990ce210e8c2 189 *
Adrian Suciu 30:990ce210e8c2 190 */
Adrian Suciu 30:990ce210e8c2 191 void calibrate_ph_offset();
Adrian Suciu 30:990ce210e8c2 192
Adrian Suciu 30:990ce210e8c2 193 /* void enable_current_source0(int current_source_channel);
Adrian Suciu 30:990ce210e8c2 194 void enable_current_source1(int current_source_channel);*/
Adrian Suciu 30:990ce210e8c2 195
Adrian Suciu 30:990ce210e8c2 196 /**
Adrian Suciu 30:990ce210e8c2 197 * @brief toggles the digital outputs on or off
Adrian Suciu 30:990ce210e8c2 198 * @param p - digital output
Adrian Suciu 30:990ce210e8c2 199 * @param state - state of the output
Adrian Suciu 30:990ce210e8c2 200 */
Adrian Suciu 30:990ce210e8c2 201 void set_digital_output(ad_digital_output_t p, bool state);
Adrian Suciu 30:990ce210e8c2 202
Adrian Suciu 30:990ce210e8c2 203 /**
Adrian Suciu 30:990ce210e8c2 204 * @brief triggers ADC start of single conversion
Adrian Suciu 30:990ce210e8c2 205 */
Adrian Suciu 30:990ce210e8c2 206 void start_single_conversion();
Adrian Suciu 30:990ce210e8c2 207
Adrian Suciu 30:990ce210e8c2 208 /**
Adrian Suciu 30:990ce210e8c2 209 * @brief resets the digital interface of the ADC
Adrian Suciu 30:990ce210e8c2 210 */
Adrian Suciu 30:990ce210e8c2 211 void reset();
Adrian Suciu 30:990ce210e8c2 212
Adrian Suciu 30:990ce210e8c2 213 /**
Adrian Suciu 30:990ce210e8c2 214 * @brief initializes the ADC
Adrian Suciu 30:990ce210e8c2 215 */
Adrian Suciu 30:990ce210e8c2 216 void setup();
Adrian Suciu 30:990ce210e8c2 217
Adrian Suciu 30:990ce210e8c2 218 /**
Adrian Suciu 30:990ce210e8c2 219 * @brief configures the ADC for the CN0398 application
Adrian Suciu 30:990ce210e8c2 220 */
Adrian Suciu 30:990ce210e8c2 221 void init();
Adrian Suciu 30:990ce210e8c2 222
Adrian Suciu 30:990ce210e8c2 223 AD7124 ad7124;
Adrian Suciu 30:990ce210e8c2 224 DigitalOut ADP7118Enable;
Adrian Suciu 30:990ce210e8c2 225
Adrian Suciu 30:990ce210e8c2 226 bool use_nernst = false;
Adrian Suciu 30:990ce210e8c2 227 const float default_offset_voltage = 0;
Adrian Suciu 30:990ce210e8c2 228 const uint16_t SENSOR_SETTLING_TIME = 400; /*in ms*/
Adrian Suciu 30:990ce210e8c2 229 float offset_voltage;
Adrian Suciu 30:990ce210e8c2 230 float default_calibration_ph[2][2] = {{4, 0.169534}, {10, -0.134135}};
Adrian Suciu 30:990ce210e8c2 231 float calibration_ph[2][2];
Adrian Suciu 30:990ce210e8c2 232 uint8_t solution0, solution1;
Adrian Suciu 30:990ce210e8c2 233
Adrian Suciu 30:990ce210e8c2 234 };
Adrian Suciu 30:990ce210e8c2 235 #endif