Soil Measurements for pH and moisture

Dependencies:   AD7124

Dependents:   cn0398-helloworld

Fork of CN0398 by Analog Devices

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

Committer:
adisuciu
Date:
Wed Oct 26 13:49:06 2016 +0000
Revision:
3:7072863079d5
Parent:
2:f1b9c875e725
Added extra step in initialization

Who changed what in which revision?

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