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