first publish without code changes
Dependencies: AD7124
CN0398.cpp@4:17440c89a170, 2019-05-05 (annotated)
- Committer:
- rayanfer32
- Date:
- Sun May 05 08:57:52 2019 +0000
- Revision:
- 4:17440c89a170
- Parent:
- 2:f1b9c875e725
first commit no code changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adisuciu | 0:d6b384fb3c16 | 1 | #include "CN0398.h" |
adisuciu | 0:d6b384fb3c16 | 2 | #include "AD7124.h" |
adisuciu | 0:d6b384fb3c16 | 3 | #include <mbed.h> |
adisuciu | 0:d6b384fb3c16 | 4 | |
adisuciu | 0:d6b384fb3c16 | 5 | #define RREF (5000.0) |
adisuciu | 0:d6b384fb3c16 | 6 | #define TEMP_GAIN (16.0) |
adisuciu | 0:d6b384fb3c16 | 7 | #define PT100_RESISTANCE_TO_TEMP(x) ((x-100.0)/(0.385)) |
adisuciu | 0:d6b384fb3c16 | 8 | #define _2_23 (1<<23) |
adisuciu | 0:d6b384fb3c16 | 9 | |
adisuciu | 0:d6b384fb3c16 | 10 | #define CALIBRATION_NR_OF_SAMPLES (5) |
adisuciu | 0:d6b384fb3c16 | 11 | |
adisuciu | 0:d6b384fb3c16 | 12 | extern Serial pc; |
adisuciu | 0:d6b384fb3c16 | 13 | |
adisuciu | 0:d6b384fb3c16 | 14 | #define ms_delay (1) |
adisuciu | 0:d6b384fb3c16 | 15 | |
adisuciu | 0:d6b384fb3c16 | 16 | CN0398::CN0398(PinName cs, PinName adp7118enable) : ad7124(cs), ADP7118Enable(adp7118enable), offset_voltage(default_offset_voltage) |
adisuciu | 0:d6b384fb3c16 | 17 | { |
adisuciu | 2:f1b9c875e725 | 18 | use_nernst = false; |
adisuciu | 2:f1b9c875e725 | 19 | calibration_ph[0][0] = 4;//default_calibration_ph[0][0]; |
adisuciu | 2:f1b9c875e725 | 20 | calibration_ph[0][1] = 0.169534;//default_calibration_ph[0][1]; |
adisuciu | 2:f1b9c875e725 | 21 | calibration_ph[1][0] = 10;//default_calibration_ph[1][0]; |
adisuciu | 2:f1b9c875e725 | 22 | calibration_ph[1][1] = -0.134135;//default_calibration_ph[1][1]; |
adisuciu | 0:d6b384fb3c16 | 23 | solution0 = 0; |
adisuciu | 0:d6b384fb3c16 | 24 | solution1 = 0; |
adisuciu | 0:d6b384fb3c16 | 25 | } |
adisuciu | 0:d6b384fb3c16 | 26 | |
adisuciu | 0:d6b384fb3c16 | 27 | void CN0398::calibrate_ph_pt0(float temperature) |
adisuciu | 0:d6b384fb3c16 | 28 | { |
adisuciu | 0:d6b384fb3c16 | 29 | float volt = 0; |
adisuciu | 0:d6b384fb3c16 | 30 | for(int i = 0; i < CALIBRATION_NR_OF_SAMPLES; i++) { |
adisuciu | 0:d6b384fb3c16 | 31 | set_digital_output(P2, true); |
adisuciu | 0:d6b384fb3c16 | 32 | int32_t data = read_channel(0); |
adisuciu | 0:d6b384fb3c16 | 33 | set_digital_output(P2, false); |
adisuciu | 0:d6b384fb3c16 | 34 | volt += data_to_voltage_bipolar(data >> 8, 1, 3.3); |
adisuciu | 0:d6b384fb3c16 | 35 | } |
adisuciu | 0:d6b384fb3c16 | 36 | volt = volt / CALIBRATION_NR_OF_SAMPLES; |
adisuciu | 0:d6b384fb3c16 | 37 | if(temperature < 0) { |
adisuciu | 0:d6b384fb3c16 | 38 | calibration_ph[0][0] = ph_temp_lut[solution0][0]; |
adisuciu | 0:d6b384fb3c16 | 39 | } else { |
adisuciu | 0:d6b384fb3c16 | 40 | for(uint8_t i = 1; i < NUMBER_OF_TEMPERATURE_ENTRIES; i++) { |
adisuciu | 0:d6b384fb3c16 | 41 | if(temperature > ph_temperatures[i - 1] && temperature <= ph_temperatures[i]) { |
adisuciu | 0:d6b384fb3c16 | 42 | calibration_ph[0][0] = ph_temp_lut[solution0][i]; |
adisuciu | 0:d6b384fb3c16 | 43 | break; |
adisuciu | 0:d6b384fb3c16 | 44 | } |
adisuciu | 0:d6b384fb3c16 | 45 | } |
adisuciu | 0:d6b384fb3c16 | 46 | } |
adisuciu | 0:d6b384fb3c16 | 47 | calibration_ph[0][1] = volt; |
adisuciu | 0:d6b384fb3c16 | 48 | pc.printf("Calibration solution 1 ph: %f with sensor voltage of %f\r\n", calibration_ph[0][0], volt); |
adisuciu | 0:d6b384fb3c16 | 49 | } |
adisuciu | 0:d6b384fb3c16 | 50 | void CN0398::calibrate_ph_pt1(float temperature) |
adisuciu | 0:d6b384fb3c16 | 51 | { |
adisuciu | 0:d6b384fb3c16 | 52 | float volt = 0; |
adisuciu | 0:d6b384fb3c16 | 53 | for(int i = 0; i < CALIBRATION_NR_OF_SAMPLES; i++) { |
adisuciu | 0:d6b384fb3c16 | 54 | set_digital_output(P2, true); |
adisuciu | 0:d6b384fb3c16 | 55 | int32_t data = read_channel(0); |
adisuciu | 0:d6b384fb3c16 | 56 | set_digital_output(P2, false); |
adisuciu | 0:d6b384fb3c16 | 57 | volt += data_to_voltage_bipolar(data >> 8, 1, 3.3); |
adisuciu | 0:d6b384fb3c16 | 58 | } |
adisuciu | 0:d6b384fb3c16 | 59 | |
adisuciu | 0:d6b384fb3c16 | 60 | volt = volt / CALIBRATION_NR_OF_SAMPLES; |
adisuciu | 0:d6b384fb3c16 | 61 | if(temperature < 0) { |
adisuciu | 0:d6b384fb3c16 | 62 | calibration_ph[1][0] = ph_temp_lut[solution1][0]; |
adisuciu | 0:d6b384fb3c16 | 63 | } else { |
adisuciu | 0:d6b384fb3c16 | 64 | for(uint8_t i = 1; i < NUMBER_OF_TEMPERATURE_ENTRIES; i++) { |
adisuciu | 0:d6b384fb3c16 | 65 | if(temperature > ph_temperatures[i - 1] && temperature <= ph_temperatures[i]) { |
adisuciu | 0:d6b384fb3c16 | 66 | calibration_ph[1][0] = ph_temp_lut[solution1][i]; |
adisuciu | 0:d6b384fb3c16 | 67 | break; |
adisuciu | 0:d6b384fb3c16 | 68 | } |
adisuciu | 0:d6b384fb3c16 | 69 | } |
adisuciu | 0:d6b384fb3c16 | 70 | } |
adisuciu | 0:d6b384fb3c16 | 71 | calibration_ph[1][1] = volt; |
adisuciu | 0:d6b384fb3c16 | 72 | pc.printf("Calibration solution 2 ph: %f with sensor voltage of %f\r\n", calibration_ph[1][0], volt); |
adisuciu | 0:d6b384fb3c16 | 73 | } |
adisuciu | 0:d6b384fb3c16 | 74 | |
adisuciu | 0:d6b384fb3c16 | 75 | void CN0398::calibrate_ph_offset() |
adisuciu | 0:d6b384fb3c16 | 76 | { |
adisuciu | 0:d6b384fb3c16 | 77 | float volt = 0; |
adisuciu | 0:d6b384fb3c16 | 78 | for(int i = 0; i < CALIBRATION_NR_OF_SAMPLES; i++) { |
adisuciu | 0:d6b384fb3c16 | 79 | set_digital_output(P2, true); |
adisuciu | 0:d6b384fb3c16 | 80 | int32_t data = read_channel(0); |
adisuciu | 0:d6b384fb3c16 | 81 | set_digital_output(P2, false); |
adisuciu | 0:d6b384fb3c16 | 82 | volt += data_to_voltage_bipolar(data >> 8, 1, 3.3); |
adisuciu | 0:d6b384fb3c16 | 83 | } |
adisuciu | 0:d6b384fb3c16 | 84 | offset_voltage = volt / CALIBRATION_NR_OF_SAMPLES; |
adisuciu | 0:d6b384fb3c16 | 85 | pc.printf("Offset voltage is: %f \r\n", volt); |
adisuciu | 0:d6b384fb3c16 | 86 | } |
adisuciu | 0:d6b384fb3c16 | 87 | |
adisuciu | 0:d6b384fb3c16 | 88 | |
adisuciu | 0:d6b384fb3c16 | 89 | float CN0398::read_rtd() |
adisuciu | 0:d6b384fb3c16 | 90 | { |
adisuciu | 0:d6b384fb3c16 | 91 | float temperature = 25.0; |
adisuciu | 0:d6b384fb3c16 | 92 | #ifdef TEMPERATURE_SENSOR_PRESENT |
adisuciu | 0:d6b384fb3c16 | 93 | int32_t data = read_channel(2); |
adisuciu | 0:d6b384fb3c16 | 94 | data = (data >> 8) & 0x00ffffff; |
adisuciu | 0:d6b384fb3c16 | 95 | float resistance = ((static_cast<float>(data) - _2_23) * RREF) / (TEMP_GAIN * _2_23); |
adisuciu | 0:d6b384fb3c16 | 96 | #ifdef USE_LINEAR_TEMP_EQ |
adisuciu | 0:d6b384fb3c16 | 97 | temperature = PT100_RESISTANCE_TO_TEMP(resistance); |
adisuciu | 0:d6b384fb3c16 | 98 | #else |
adisuciu | 0:d6b384fb3c16 | 99 | |
adisuciu | 2:f1b9c875e725 | 100 | #define A (3.9083*pow(10.0,-3.0)) |
adisuciu | 2:f1b9c875e725 | 101 | #define B (-5.775*pow(10.0,-7.0)) |
adisuciu | 0:d6b384fb3c16 | 102 | /*if(resistance < 100.0) |
adisuciu | 0:d6b384fb3c16 | 103 | temperature = -242.02 + 2.228 * resistance + (2.5859 * pow(10, -3)) * pow(resistance, 2) - (48260 * pow(10, -6)) * pow(resistance, 3) - (2.8183 * pow(10, -3)) * pow(resistance, 4) + (1.5243 * pow(10, -10)) * pow(resistance, 5); |
adisuciu | 0:d6b384fb3c16 | 104 | else*/ |
adisuciu | 2:f1b9c875e725 | 105 | temperature = ((-A + sqrt(double(pow(A, 2.0) - 4 * B * (1 - resistance / 100.0))) ) / (2 * B)); |
adisuciu | 0:d6b384fb3c16 | 106 | #endif |
adisuciu | 0:d6b384fb3c16 | 107 | #endif |
adisuciu | 0:d6b384fb3c16 | 108 | return temperature; |
adisuciu | 0:d6b384fb3c16 | 109 | |
adisuciu | 0:d6b384fb3c16 | 110 | } |
adisuciu | 0:d6b384fb3c16 | 111 | |
adisuciu | 0:d6b384fb3c16 | 112 | int32_t CN0398::read_channel(uint8_t ch) |
adisuciu | 0:d6b384fb3c16 | 113 | { |
adisuciu | 0:d6b384fb3c16 | 114 | int32_t data; |
adisuciu | 0:d6b384fb3c16 | 115 | enable_channel(ch); |
adisuciu | 0:d6b384fb3c16 | 116 | start_single_conversion(); |
adisuciu | 0:d6b384fb3c16 | 117 | |
adisuciu | 0:d6b384fb3c16 | 118 | if (ad7124.WaitForConvReady(10000) == -3) { |
adisuciu | 0:d6b384fb3c16 | 119 | pc.printf("TIMEOUT"); |
adisuciu | 0:d6b384fb3c16 | 120 | return -1; |
adisuciu | 0:d6b384fb3c16 | 121 | } |
adisuciu | 0:d6b384fb3c16 | 122 | ad7124.ReadData(&data); |
adisuciu | 0:d6b384fb3c16 | 123 | disable_channel(ch); |
adisuciu | 0:d6b384fb3c16 | 124 | return data; |
adisuciu | 0:d6b384fb3c16 | 125 | |
adisuciu | 0:d6b384fb3c16 | 126 | } |
adisuciu | 0:d6b384fb3c16 | 127 | float CN0398::read_ph(float temperature) |
adisuciu | 0:d6b384fb3c16 | 128 | { |
adisuciu | 0:d6b384fb3c16 | 129 | float ph = 0; |
adisuciu | 0:d6b384fb3c16 | 130 | #ifdef PH_SENSOR_PRESENT |
adisuciu | 0:d6b384fb3c16 | 131 | set_digital_output(P2, true); |
adisuciu | 0:d6b384fb3c16 | 132 | int32_t data = read_channel(0); |
adisuciu | 0:d6b384fb3c16 | 133 | set_digital_output(P2, false); |
adisuciu | 0:d6b384fb3c16 | 134 | float volt = data_to_voltage_bipolar(data >> 8, 1, 3.3); |
adisuciu | 0:d6b384fb3c16 | 135 | #ifdef DEBUG_MODE |
adisuciu | 0:d6b384fb3c16 | 136 | pc.printf("pH sensor voltage - %f\n", volt); |
adisuciu | 0:d6b384fb3c16 | 137 | #endif |
adisuciu | 0:d6b384fb3c16 | 138 | |
adisuciu | 0:d6b384fb3c16 | 139 | if(use_nernst) { |
adisuciu | 0:d6b384fb3c16 | 140 | ph = -((volt - ZERO_POINT_TOLERANCE) / ((2.303 * AVOGADRO * (temperature + KELVIN_OFFSET)) / FARADAY_CONSTANT) ) + PH_ISO; |
adisuciu | 0:d6b384fb3c16 | 141 | } else { |
adisuciu | 0:d6b384fb3c16 | 142 | float m = (calibration_ph[1][0] - calibration_ph[0][0]) / (calibration_ph[1][1] - calibration_ph[0][1]); |
adisuciu | 0:d6b384fb3c16 | 143 | ph = m * (volt - calibration_ph[1][1] + offset_voltage) + calibration_ph[1][0]; |
adisuciu | 0:d6b384fb3c16 | 144 | } |
adisuciu | 0:d6b384fb3c16 | 145 | #endif |
adisuciu | 0:d6b384fb3c16 | 146 | return ph; |
adisuciu | 0:d6b384fb3c16 | 147 | } |
adisuciu | 0:d6b384fb3c16 | 148 | float CN0398::read_moist() |
adisuciu | 0:d6b384fb3c16 | 149 | { |
adisuciu | 0:d6b384fb3c16 | 150 | float moisture = 0; |
adisuciu | 0:d6b384fb3c16 | 151 | #ifdef MOISTURE_SENSOR_PRESENT |
adisuciu | 0:d6b384fb3c16 | 152 | ADP7118Enable = true; |
adisuciu | 0:d6b384fb3c16 | 153 | set_digital_output(P3, true); |
adisuciu | 0:d6b384fb3c16 | 154 | wait_ms(SENSOR_SETTLING_TIME); |
adisuciu | 0:d6b384fb3c16 | 155 | int32_t data = read_channel(1); |
adisuciu | 0:d6b384fb3c16 | 156 | ADP7118Enable = false; |
adisuciu | 0:d6b384fb3c16 | 157 | set_digital_output(P3, false); |
adisuciu | 0:d6b384fb3c16 | 158 | |
adisuciu | 0:d6b384fb3c16 | 159 | data = (data >> 8) & 0x00ffffff; |
adisuciu | 0:d6b384fb3c16 | 160 | float volt = data_to_voltage(data, 1, 3.3); |
adisuciu | 0:d6b384fb3c16 | 161 | #ifdef USE_MANUFACTURER_MOISTURE_EQ |
adisuciu | 0:d6b384fb3c16 | 162 | if(volt <= 1.1) { |
adisuciu | 0:d6b384fb3c16 | 163 | moisture = 10 * volt - 1; |
adisuciu | 0:d6b384fb3c16 | 164 | } else if(volt > 1.1 && volt <= 1.3) { |
adisuciu | 0:d6b384fb3c16 | 165 | moisture = 25 * volt - 17.5; |
adisuciu | 0:d6b384fb3c16 | 166 | } else if(volt > 1.3 && volt <= 1.82) { |
adisuciu | 0:d6b384fb3c16 | 167 | moisture = 48.08 * volt - 47.5; |
adisuciu | 0:d6b384fb3c16 | 168 | } else if(volt > 1.82) { |
adisuciu | 0:d6b384fb3c16 | 169 | moisture = 26.32 * volt - 7.89; |
adisuciu | 0:d6b384fb3c16 | 170 | } |
adisuciu | 0:d6b384fb3c16 | 171 | #else |
adisuciu | 0:d6b384fb3c16 | 172 | moisture = -1.18467 + 21.5371 * volt - 110.996 * (pow(volt, 2)) + 397.025 * (pow(volt, 3)) - 666.986 * (pow(volt, 4)) + 569.236 * (pow(volt, 5)) - 246.005 * (pow(volt, 6)) + 49.4867 * (pow(volt, 7)) - 3.37077 * (pow(volt, 8)); |
adisuciu | 0:d6b384fb3c16 | 173 | #endif |
adisuciu | 0:d6b384fb3c16 | 174 | if(moisture > 100) moisture = 100; |
adisuciu | 0:d6b384fb3c16 | 175 | if(moisture < 0 ) moisture = 0; |
adisuciu | 0:d6b384fb3c16 | 176 | #endif |
adisuciu | 0:d6b384fb3c16 | 177 | return moisture; |
adisuciu | 0:d6b384fb3c16 | 178 | } |
adisuciu | 0:d6b384fb3c16 | 179 | |
adisuciu | 0:d6b384fb3c16 | 180 | float CN0398::data_to_voltage_bipolar(uint32_t data, uint8_t gain, float VREF) |
adisuciu | 0:d6b384fb3c16 | 181 | { |
adisuciu | 0:d6b384fb3c16 | 182 | data = data & 0xFFFFFF; |
adisuciu | 0:d6b384fb3c16 | 183 | return ((data / static_cast<float>(0xFFFFFF / 2)) - 1) * (VREF / gain); |
adisuciu | 0:d6b384fb3c16 | 184 | } |
adisuciu | 0:d6b384fb3c16 | 185 | |
adisuciu | 0:d6b384fb3c16 | 186 | float CN0398::data_to_voltage(uint32_t data, uint8_t gain, float VREF) |
adisuciu | 0:d6b384fb3c16 | 187 | { |
adisuciu | 0:d6b384fb3c16 | 188 | data = data & 0xFFFFFF; |
adisuciu | 0:d6b384fb3c16 | 189 | return (data / static_cast<float>(0xFFFFFF)) * (VREF / gain); |
adisuciu | 0:d6b384fb3c16 | 190 | } |
adisuciu | 0:d6b384fb3c16 | 191 | |
adisuciu | 0:d6b384fb3c16 | 192 | void CN0398::enable_channel(int channel) |
adisuciu | 0:d6b384fb3c16 | 193 | { |
adisuciu | 0:d6b384fb3c16 | 194 | AD7124::ad7124_registers regNr = static_cast<AD7124::ad7124_registers> (AD7124::AD7124_Channel_0 + channel); //Select ADC_Control register |
adisuciu | 0:d6b384fb3c16 | 195 | uint32_t setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 196 | setValue |= (uint32_t) AD7124_CH_MAP_REG_CH_ENABLE; //Enable channel0 |
adisuciu | 0:d6b384fb3c16 | 197 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 198 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 199 | wait_ms(ms_delay); |
adisuciu | 0:d6b384fb3c16 | 200 | } |
adisuciu | 0:d6b384fb3c16 | 201 | |
adisuciu | 0:d6b384fb3c16 | 202 | void CN0398::disable_channel(int channel) |
adisuciu | 0:d6b384fb3c16 | 203 | { |
adisuciu | 0:d6b384fb3c16 | 204 | AD7124::ad7124_registers regNr = static_cast<AD7124::ad7124_registers> (AD7124::AD7124_Channel_0 + channel); //Select ADC_Control register |
adisuciu | 0:d6b384fb3c16 | 205 | uint32_t setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 206 | setValue &= (~(uint32_t) AD7124_CH_MAP_REG_CH_ENABLE); //Enable channel0 |
adisuciu | 0:d6b384fb3c16 | 207 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 208 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 209 | wait_ms(ms_delay); |
adisuciu | 0:d6b384fb3c16 | 210 | } |
adisuciu | 0:d6b384fb3c16 | 211 | |
adisuciu | 0:d6b384fb3c16 | 212 | /* |
adisuciu | 0:d6b384fb3c16 | 213 | void CN0398::enable_current_source0(int current_source_channel) |
adisuciu | 0:d6b384fb3c16 | 214 | { |
adisuciu | 0:d6b384fb3c16 | 215 | AD7124::ad7124_registers regNr = AD7124::AD7124_IOCon1; //Select ADC_Control register |
adisuciu | 0:d6b384fb3c16 | 216 | uint32_t setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 217 | setValue &= ~(AD7124_IO_CTRL1_REG_IOUT_CH0(0xF)); |
adisuciu | 0:d6b384fb3c16 | 218 | setValue |= AD7124_IO_CTRL1_REG_IOUT_CH0(current_source_channel);// set IOUT0 current to 500uA |
adisuciu | 0:d6b384fb3c16 | 219 | setValue &= 0xFFFFFF; |
adisuciu | 0:d6b384fb3c16 | 220 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 221 | wait_ms(ms_delay); |
adisuciu | 0:d6b384fb3c16 | 222 | } |
adisuciu | 0:d6b384fb3c16 | 223 | |
adisuciu | 0:d6b384fb3c16 | 224 | void CN0398::enable_current_source1(int current_source_channel) |
adisuciu | 0:d6b384fb3c16 | 225 | { |
adisuciu | 0:d6b384fb3c16 | 226 | AD7124::ad7124_registers regNr = AD7124::AD7124_IOCon1; //Select ADC_Control register |
adisuciu | 0:d6b384fb3c16 | 227 | uint32_t setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 228 | setValue &= ~(AD7124_IO_CTRL1_REG_IOUT_CH1(0xF)); |
adisuciu | 0:d6b384fb3c16 | 229 | setValue |= AD7124_IO_CTRL1_REG_IOUT_CH1(current_source_channel);// set IOUT0 current to 500uA |
adisuciu | 0:d6b384fb3c16 | 230 | setValue &= 0xFFFFFF; |
adisuciu | 0:d6b384fb3c16 | 231 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 232 | wait_ms(ms_delay); |
adisuciu | 0:d6b384fb3c16 | 233 | }*/ |
adisuciu | 0:d6b384fb3c16 | 234 | |
adisuciu | 0:d6b384fb3c16 | 235 | void CN0398::set_digital_output(ad_digital_output_t p, bool state) |
adisuciu | 0:d6b384fb3c16 | 236 | { |
adisuciu | 0:d6b384fb3c16 | 237 | AD7124::ad7124_registers regNr = AD7124::AD7124_IOCon1; //Select ADC_Control register |
adisuciu | 0:d6b384fb3c16 | 238 | uint32_t setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 239 | if(state) |
adisuciu | 0:d6b384fb3c16 | 240 | setValue |= ((AD7124_8_IO_CTRL1_REG_GPIO_DAT1) << p); |
adisuciu | 0:d6b384fb3c16 | 241 | else |
adisuciu | 0:d6b384fb3c16 | 242 | setValue &= (~((AD7124_8_IO_CTRL1_REG_GPIO_DAT1) << p)); |
adisuciu | 0:d6b384fb3c16 | 243 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 244 | wait_ms(ms_delay); |
adisuciu | 0:d6b384fb3c16 | 245 | } |
adisuciu | 0:d6b384fb3c16 | 246 | |
adisuciu | 0:d6b384fb3c16 | 247 | |
adisuciu | 0:d6b384fb3c16 | 248 | void CN0398::start_single_conversion() |
adisuciu | 0:d6b384fb3c16 | 249 | { |
adisuciu | 0:d6b384fb3c16 | 250 | AD7124::ad7124_registers regNr = AD7124::AD7124_ADC_Control; //Select ADC_Control register |
adisuciu | 0:d6b384fb3c16 | 251 | uint32_t setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 252 | setValue &= 0xFFC3; |
adisuciu | 0:d6b384fb3c16 | 253 | setValue |= 0x04; //single conversion; |
adisuciu | 0:d6b384fb3c16 | 254 | setValue |= AD7124_ADC_CTRL_REG_DATA_STATUS; |
adisuciu | 0:d6b384fb3c16 | 255 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 256 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC*/ |
adisuciu | 0:d6b384fb3c16 | 257 | wait_ms(ms_delay * 10); |
adisuciu | 0:d6b384fb3c16 | 258 | } |
adisuciu | 0:d6b384fb3c16 | 259 | |
adisuciu | 0:d6b384fb3c16 | 260 | void CN0398::reset() |
adisuciu | 0:d6b384fb3c16 | 261 | { |
adisuciu | 0:d6b384fb3c16 | 262 | ad7124.frequency(500000); |
adisuciu | 0:d6b384fb3c16 | 263 | ad7124.Reset(); |
adisuciu | 0:d6b384fb3c16 | 264 | pc.printf("Reseted AD7124\r\n"); |
adisuciu | 0:d6b384fb3c16 | 265 | wait_ms(1000); |
adisuciu | 0:d6b384fb3c16 | 266 | } |
adisuciu | 0:d6b384fb3c16 | 267 | |
adisuciu | 0:d6b384fb3c16 | 268 | void CN0398::setup() |
adisuciu | 0:d6b384fb3c16 | 269 | { |
adisuciu | 0:d6b384fb3c16 | 270 | ad7124.Setup(); |
adisuciu | 0:d6b384fb3c16 | 271 | } |
adisuciu | 0:d6b384fb3c16 | 272 | |
adisuciu | 0:d6b384fb3c16 | 273 | void CN0398::init() |
adisuciu | 0:d6b384fb3c16 | 274 | { |
adisuciu | 0:d6b384fb3c16 | 275 | uint32_t setValue; |
adisuciu | 0:d6b384fb3c16 | 276 | enum AD7124::ad7124_registers regNr; |
adisuciu | 0:d6b384fb3c16 | 277 | |
adisuciu | 0:d6b384fb3c16 | 278 | /* Set Config_0 0x19*/ |
adisuciu | 0:d6b384fb3c16 | 279 | regNr = AD7124::AD7124_Config_0; //Select Config_0 register - pH |
adisuciu | 0:d6b384fb3c16 | 280 | setValue = 0;//ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 281 | setValue |= AD7124_CFG_REG_BIPOLAR; //Select bipolar operation |
adisuciu | 0:d6b384fb3c16 | 282 | setValue |= AD7124_CFG_REG_BURNOUT(0); //Burnout current source off |
adisuciu | 0:d6b384fb3c16 | 283 | setValue |= AD7124_CFG_REG_REF_BUFP; |
adisuciu | 0:d6b384fb3c16 | 284 | setValue |= AD7124_CFG_REG_REF_BUFM; |
adisuciu | 0:d6b384fb3c16 | 285 | setValue |= AD7124_CFG_REG_AIN_BUFP; //Buffer AIN5 |
adisuciu | 0:d6b384fb3c16 | 286 | setValue |= AD7124_CFG_REG_AINN_BUFM; //Buffer AIN4 |
adisuciu | 0:d6b384fb3c16 | 287 | setValue |= AD7124_CFG_REG_REF_SEL(0); //REFIN1(+)/REFIN1(−). |
adisuciu | 0:d6b384fb3c16 | 288 | setValue |= AD7124_CFG_REG_PGA(0); |
adisuciu | 0:d6b384fb3c16 | 289 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 290 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 291 | |
adisuciu | 0:d6b384fb3c16 | 292 | /* Set Config_0 0x1A*/ |
adisuciu | 0:d6b384fb3c16 | 293 | regNr = AD7124::AD7124_Config_1; //Select Config_1 register - Moisture |
adisuciu | 0:d6b384fb3c16 | 294 | setValue = 0;//ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 295 | setValue &= ~AD7124_CFG_REG_BIPOLAR; //Select bipolar operation |
adisuciu | 0:d6b384fb3c16 | 296 | setValue |= AD7124_CFG_REG_BURNOUT(0); //Burnout current source off |
adisuciu | 0:d6b384fb3c16 | 297 | setValue |= AD7124_CFG_REG_REF_BUFP; |
adisuciu | 0:d6b384fb3c16 | 298 | setValue |= AD7124_CFG_REG_REF_BUFM; |
adisuciu | 0:d6b384fb3c16 | 299 | setValue |= AD7124_CFG_REG_AIN_BUFP; //Buffer AIN5 |
adisuciu | 0:d6b384fb3c16 | 300 | setValue |= AD7124_CFG_REG_AINN_BUFM; //Buffer AIN4*/ |
adisuciu | 0:d6b384fb3c16 | 301 | setValue |= AD7124_CFG_REG_REF_SEL(0); // REFIN1(+)/REFIN1(−). |
adisuciu | 0:d6b384fb3c16 | 302 | setValue |= AD7124_CFG_REG_PGA(0); |
adisuciu | 0:d6b384fb3c16 | 303 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 304 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 305 | |
adisuciu | 0:d6b384fb3c16 | 306 | /* Set Config_0 0x1B*/ |
adisuciu | 0:d6b384fb3c16 | 307 | regNr = AD7124::AD7124_Config_2; //Select Config_2 register - temp |
adisuciu | 0:d6b384fb3c16 | 308 | setValue = 0;//ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 309 | setValue |= AD7124_CFG_REG_BIPOLAR; //Select bipolar operation |
adisuciu | 0:d6b384fb3c16 | 310 | setValue |= AD7124_CFG_REG_BURNOUT(0); //Burnout current source off |
adisuciu | 0:d6b384fb3c16 | 311 | setValue |= AD7124_CFG_REG_REF_BUFP; |
adisuciu | 0:d6b384fb3c16 | 312 | setValue |= AD7124_CFG_REG_REF_BUFM; |
adisuciu | 0:d6b384fb3c16 | 313 | setValue |= AD7124_CFG_REG_AIN_BUFP; //Buffer AIN5 |
adisuciu | 0:d6b384fb3c16 | 314 | setValue |= AD7124_CFG_REG_AINN_BUFM; //Buffer AIN4 |
adisuciu | 0:d6b384fb3c16 | 315 | setValue |= AD7124_CFG_REG_REF_SEL(1); //REFIN2(+)/REFIN2(-). |
adisuciu | 0:d6b384fb3c16 | 316 | setValue |= AD7124_CFG_REG_PGA(4); // gain 16 |
adisuciu | 0:d6b384fb3c16 | 317 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 318 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 319 | |
adisuciu | 0:d6b384fb3c16 | 320 | /* Set Channel_0 register 0x09*/ |
adisuciu | 0:d6b384fb3c16 | 321 | regNr = AD7124::AD7124_Channel_0; // pH reading |
adisuciu | 0:d6b384fb3c16 | 322 | setValue = 0; |
adisuciu | 0:d6b384fb3c16 | 323 | setValue |= AD7124_CH_MAP_REG_SETUP(0); // Select setup0 |
adisuciu | 0:d6b384fb3c16 | 324 | setValue |= AD7124_CH_MAP_REG_AINP(6); // Set AIN4 as positive input |
adisuciu | 0:d6b384fb3c16 | 325 | setValue |= AD7124_CH_MAP_REG_AINM(7); // Set AIN5 as negative input |
adisuciu | 0:d6b384fb3c16 | 326 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 327 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 328 | |
adisuciu | 0:d6b384fb3c16 | 329 | regNr = AD7124::AD7124_Channel_1; // Moisture |
adisuciu | 0:d6b384fb3c16 | 330 | setValue = 0; |
adisuciu | 0:d6b384fb3c16 | 331 | setValue |= AD7124_CH_MAP_REG_SETUP(1); // Select setup0 |
adisuciu | 0:d6b384fb3c16 | 332 | setValue |= AD7124_CH_MAP_REG_AINP(8); // Set AIN4 as positive input |
adisuciu | 0:d6b384fb3c16 | 333 | setValue |= AD7124_CH_MAP_REG_AINM(19); // Set AIN5 as negative input |
adisuciu | 0:d6b384fb3c16 | 334 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 335 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 336 | |
adisuciu | 0:d6b384fb3c16 | 337 | regNr = AD7124::AD7124_Channel_2; // RTD - gain 16 |
adisuciu | 0:d6b384fb3c16 | 338 | setValue = 0; |
adisuciu | 0:d6b384fb3c16 | 339 | setValue |= AD7124_CH_MAP_REG_SETUP(2); // Select setup0 |
adisuciu | 0:d6b384fb3c16 | 340 | setValue |= AD7124_CH_MAP_REG_AINP(9); // Set AIN4 as positive input |
adisuciu | 0:d6b384fb3c16 | 341 | setValue |= AD7124_CH_MAP_REG_AINM(10); // Set AIN5 as negative input |
adisuciu | 0:d6b384fb3c16 | 342 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 343 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 344 | |
adisuciu | 0:d6b384fb3c16 | 345 | /* Set IO_Control_1 0x03 */ |
adisuciu | 0:d6b384fb3c16 | 346 | regNr = AD7124::AD7124_IOCon1; //Select IO_Control_1 register |
adisuciu | 0:d6b384fb3c16 | 347 | //setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 348 | setValue = 0; |
adisuciu | 0:d6b384fb3c16 | 349 | setValue |= AD7124_8_IO_CTRL1_REG_GPIO_CTRL2; // enable AIN3 as digital output |
adisuciu | 0:d6b384fb3c16 | 350 | setValue |= AD7124_8_IO_CTRL1_REG_GPIO_CTRL3; // enable AIN4 as digital output |
adisuciu | 0:d6b384fb3c16 | 351 | setValue |= AD7124_IO_CTRL1_REG_IOUT_CH0(11); // source ain11 |
adisuciu | 0:d6b384fb3c16 | 352 | setValue |= AD7124_IO_CTRL1_REG_IOUT_CH1(12); // source ain12 |
adisuciu | 0:d6b384fb3c16 | 353 | setValue |= AD7124_IO_CTRL1_REG_IOUT0(0x4);// set IOUT0 current to 500uA |
adisuciu | 0:d6b384fb3c16 | 354 | setValue |= AD7124_IO_CTRL1_REG_IOUT1(0x4);// set IOUT0 current to 500uA*/ |
adisuciu | 0:d6b384fb3c16 | 355 | setValue &= 0xFFFFFF; |
adisuciu | 0:d6b384fb3c16 | 356 | ad7124.WriteDeviceRegister(regNr, setValue);// Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 357 | |
adisuciu | 0:d6b384fb3c16 | 358 | // Set IO_Control_2 |
adisuciu | 0:d6b384fb3c16 | 359 | regNr = AD7124::AD7124_IOCon2; //Select IO_Control_2 register |
adisuciu | 0:d6b384fb3c16 | 360 | setValue = 0; |
adisuciu | 0:d6b384fb3c16 | 361 | setValue |= AD7124_8_IO_CTRL2_REG_GPIO_VBIAS7; // enable AIN3 as digital output |
adisuciu | 0:d6b384fb3c16 | 362 | setValue &= 0xFFFFFF; |
adisuciu | 0:d6b384fb3c16 | 363 | ad7124.WriteDeviceRegister(regNr, setValue);// Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 364 | |
adisuciu | 0:d6b384fb3c16 | 365 | |
adisuciu | 0:d6b384fb3c16 | 366 | /* Set ADC_Control 0x01 */ |
adisuciu | 0:d6b384fb3c16 | 367 | regNr = AD7124::AD7124_ADC_Control; //Select ADC_Control register |
adisuciu | 0:d6b384fb3c16 | 368 | setValue = ad7124.ReadDeviceRegister(regNr); |
adisuciu | 0:d6b384fb3c16 | 369 | setValue |= AD7124_ADC_CTRL_REG_DATA_STATUS; // set data status bit in order to check on which channel the conversion is |
adisuciu | 0:d6b384fb3c16 | 370 | setValue &= 0xFFC3; // remove prev mode bits |
adisuciu | 0:d6b384fb3c16 | 371 | setValue |= AD7124_ADC_CTRL_REG_MODE(2); |
adisuciu | 0:d6b384fb3c16 | 372 | setValue &= 0xFFFF; |
adisuciu | 0:d6b384fb3c16 | 373 | ad7124.WriteDeviceRegister(regNr, setValue); // Write data to ADC |
adisuciu | 0:d6b384fb3c16 | 374 | wait_ms(ms_delay); |
adisuciu | 0:d6b384fb3c16 | 375 | } |
adisuciu | 0:d6b384fb3c16 | 376 | |
adisuciu | 0:d6b384fb3c16 | 377 | |
adisuciu | 0:d6b384fb3c16 | 378 | |
adisuciu | 0:d6b384fb3c16 | 379 | |
adisuciu | 0:d6b384fb3c16 | 380 |