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:
Wed Oct 26 16:47:55 2016 +0300
Revision:
32:97d248e81e7f
Parent:
30:990ce210e8c2
CN0398 - Added extra step in initialization

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Adrian Suciu 30:990ce210e8c2 1 /**
Adrian Suciu 30:990ce210e8c2 2 * @file main.cpp
Adrian Suciu 30:990ce210e8c2 3 * @brief Main file for the CN0398-example project
Adrian Suciu 30:990ce210e8c2 4 * @author Analog Devices Inc.
Adrian Suciu 30:990ce210e8c2 5 *
Adrian Suciu 30:990ce210e8c2 6 * For support please go to:
Adrian Suciu 30:990ce210e8c2 7 * Github: https://github.com/analogdevicesinc/mbed-adi
Adrian Suciu 30:990ce210e8c2 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
Adrian Suciu 30:990ce210e8c2 9 * Product: www.analog.com/EVAL-CN0398-ARDZ
Adrian Suciu 30:990ce210e8c2 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
Adrian Suciu 30:990ce210e8c2 11
Adrian Suciu 30:990ce210e8c2 12 ********************************************************************************
Adrian Suciu 30:990ce210e8c2 13 * Copyright 2016(c) Analog Devices, Inc.
Adrian Suciu 30:990ce210e8c2 14 *
Adrian Suciu 30:990ce210e8c2 15 * All rights reserved.
Adrian Suciu 30:990ce210e8c2 16 *
Adrian Suciu 30:990ce210e8c2 17 * Redistribution and use in source and binary forms, with or without
Adrian Suciu 30:990ce210e8c2 18 * modification, are permitted provided that the following conditions are met:
Adrian Suciu 30:990ce210e8c2 19 * - Redistributions of source code must retain the above copyright
Adrian Suciu 30:990ce210e8c2 20 * notice, this list of conditions and the following disclaimer.
Adrian Suciu 30:990ce210e8c2 21 * - Redistributions in binary form must reproduce the above copyright
Adrian Suciu 30:990ce210e8c2 22 * notice, this list of conditions and the following disclaimer in
Adrian Suciu 30:990ce210e8c2 23 * the documentation and/or other materials provided with the
Adrian Suciu 30:990ce210e8c2 24 * distribution.
Adrian Suciu 30:990ce210e8c2 25 * - Neither the name of Analog Devices, Inc. nor the names of its
Adrian Suciu 30:990ce210e8c2 26 * contributors may be used to endorse or promote products derived
Adrian Suciu 30:990ce210e8c2 27 * from this software without specific prior written permission.
Adrian Suciu 30:990ce210e8c2 28 * - The use of this software may or may not infringe the patent rights
Adrian Suciu 30:990ce210e8c2 29 * of one or more patent holders. This license does not release you
Adrian Suciu 30:990ce210e8c2 30 * from the requirement that you obtain separate licenses from these
Adrian Suciu 30:990ce210e8c2 31 * patent holders to use this software.
Adrian Suciu 30:990ce210e8c2 32 * - Use of the software either in source or binary form, must be run
Adrian Suciu 30:990ce210e8c2 33 * on or directly connected to an Analog Devices Inc. component.
Adrian Suciu 30:990ce210e8c2 34 *
Adrian Suciu 30:990ce210e8c2 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
Adrian Suciu 30:990ce210e8c2 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
Adrian Suciu 30:990ce210e8c2 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Adrian Suciu 30:990ce210e8c2 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
Adrian Suciu 30:990ce210e8c2 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Adrian Suciu 30:990ce210e8c2 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
Adrian Suciu 30:990ce210e8c2 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Adrian Suciu 30:990ce210e8c2 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Adrian Suciu 30:990ce210e8c2 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Adrian Suciu 30:990ce210e8c2 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Adrian Suciu 30:990ce210e8c2 45 *
Adrian Suciu 30:990ce210e8c2 46 ********************************************************************************/
Adrian Suciu 30:990ce210e8c2 47 #include "mbed.h"
Adrian Suciu 30:990ce210e8c2 48 #include "CN0398.h"
Adrian Suciu 30:990ce210e8c2 49
Adrian Suciu 30:990ce210e8c2 50 Serial pc(SERIAL_TX, SERIAL_RX);
Adrian Suciu 30:990ce210e8c2 51
Adrian Suciu 30:990ce210e8c2 52 /**
Adrian Suciu 30:990ce210e8c2 53 * @brief: Prints calibration solutions
Adrian Suciu 30:990ce210e8c2 54 */
Adrian Suciu 30:990ce210e8c2 55 void print_calibration_solutions()
Adrian Suciu 30:990ce210e8c2 56 {
Adrian Suciu 30:990ce210e8c2 57 pc.printf("0. ACETATE\n1. BORATE\n2. CAOH2\n3. CARBONATE\n4. CITRATE\n5. HCL\n6. OXALATE\n7. PHOSPHATE0\n8. PHOSPHATE1\n9. PHOSPHATE2\nA. PHTHALATE\nB. TARTRATE\nC. TRIS\nD. pH4\nE. pH 10");
Adrian Suciu 30:990ce210e8c2 58 }
Adrian Suciu 30:990ce210e8c2 59
Adrian Suciu 30:990ce210e8c2 60 /**
Adrian Suciu 30:990ce210e8c2 61 * @brief: flushes the serial interface.
Adrian Suciu 30:990ce210e8c2 62 */
Adrian Suciu 30:990ce210e8c2 63 void flush_serial()
Adrian Suciu 30:990ce210e8c2 64 {
Adrian Suciu 30:990ce210e8c2 65 wait_ms(10); // wait for all data to come through
Adrian Suciu 30:990ce210e8c2 66 while (pc.readable()) pc.getc();
Adrian Suciu 30:990ce210e8c2 67 }
Adrian Suciu 30:990ce210e8c2 68
Adrian Suciu 30:990ce210e8c2 69 int main()
Adrian Suciu 30:990ce210e8c2 70 {
Adrian Suciu 30:990ce210e8c2 71 pc.baud(115200);
Adrian Suciu 30:990ce210e8c2 72 CN0398 cn0398(D10, D4);
Adrian Suciu 30:990ce210e8c2 73 cn0398.reset();
Adrian Suciu 30:990ce210e8c2 74 cn0398.setup();
Adrian Suciu 30:990ce210e8c2 75 cn0398.init();
Adrian Suciu 30:990ce210e8c2 76 wait_ms(500);
Adrian Suciu 32:97d248e81e7f 77 pc.printf("CN0398 - ph and moisture measurement demo\r\nPress any key to continue...\r\n");
Adrian Suciu 32:97d248e81e7f 78 pc.getc();
Adrian Suciu 32:97d248e81e7f 79 wait_ms(10);
Adrian Suciu 32:97d248e81e7f 80 flush_serial();
Adrian Suciu 30:990ce210e8c2 81
Adrian Suciu 30:990ce210e8c2 82 pc.printf("Do you want to perform calibration [y/N] ? ");
Adrian Suciu 30:990ce210e8c2 83 char response = pc.getc();
Adrian Suciu 30:990ce210e8c2 84 flush_serial();
Adrian Suciu 30:990ce210e8c2 85 if(response == 'y' || response == 'Y') {
Adrian Suciu 30:990ce210e8c2 86 pc.printf("Do you want to calibrate offset voltage [y/N] ? ");
Adrian Suciu 30:990ce210e8c2 87 response = pc.getc();
Adrian Suciu 30:990ce210e8c2 88 flush_serial();
Adrian Suciu 30:990ce210e8c2 89 if(response == 'y' || response == 'Y') {
Adrian Suciu 30:990ce210e8c2 90 pc.printf("Calibration step 0. Short the pH probe and press any key to calibrate.\r\n");
Adrian Suciu 30:990ce210e8c2 91 pc.getc();
Adrian Suciu 30:990ce210e8c2 92 flush_serial();
Adrian Suciu 30:990ce210e8c2 93 cn0398.calibrate_ph_offset();
Adrian Suciu 30:990ce210e8c2 94 }
Adrian Suciu 30:990ce210e8c2 95 print_calibration_solutions();
Adrian Suciu 30:990ce210e8c2 96
Adrian Suciu 30:990ce210e8c2 97 bool response_ok = false;
Adrian Suciu 30:990ce210e8c2 98 while(response_ok == false) {
Adrian Suciu 30:990ce210e8c2 99 pc.printf("\nInput calibration solution used for first step [1-9][a-e] \n");
Adrian Suciu 30:990ce210e8c2 100 char response = pc.getc();
Adrian Suciu 30:990ce210e8c2 101 flush_serial();
Adrian Suciu 30:990ce210e8c2 102 if(response >= '0' && response <= '9') {
Adrian Suciu 30:990ce210e8c2 103 response_ok = true;
Adrian Suciu 30:990ce210e8c2 104 cn0398.solution0 = response - '0';
Adrian Suciu 30:990ce210e8c2 105 } else if(response >= 'A' && response <= 'E') {
Adrian Suciu 30:990ce210e8c2 106 response_ok = true;
Adrian Suciu 30:990ce210e8c2 107 cn0398.solution0 = response - 'A' + 10;
Adrian Suciu 30:990ce210e8c2 108 } else if(response >= 'a' && response <= 'e') {
Adrian Suciu 30:990ce210e8c2 109 response_ok = true;
Adrian Suciu 30:990ce210e8c2 110 cn0398.solution0 = response - 'a' + 10;
Adrian Suciu 30:990ce210e8c2 111 } else {
Adrian Suciu 30:990ce210e8c2 112 response_ok = false;
Adrian Suciu 30:990ce210e8c2 113 }
Adrian Suciu 30:990ce210e8c2 114 }
Adrian Suciu 30:990ce210e8c2 115 pc.printf("%s solution selected. pH of the solution @ 25 degrees = %f \n", solutions[cn0398.solution0], ph_temp_lut[cn0398.solution0][11]);
Adrian Suciu 30:990ce210e8c2 116 float temperature = cn0398.read_rtd();
Adrian Suciu 30:990ce210e8c2 117 pc.printf("Calibration step 1. Place pH probe in first calibration solution and press any key to start calibration.\r\n");
Adrian Suciu 30:990ce210e8c2 118 pc.getc();
Adrian Suciu 30:990ce210e8c2 119 flush_serial();
Adrian Suciu 30:990ce210e8c2 120 cn0398.calibrate_ph_pt0(temperature);
Adrian Suciu 30:990ce210e8c2 121
Adrian Suciu 30:990ce210e8c2 122 response_ok = false;
Adrian Suciu 30:990ce210e8c2 123 while(response_ok == false) {
Adrian Suciu 30:990ce210e8c2 124 pc.printf("Input calibration solution used for second step [1-9][a-e] \n");
Adrian Suciu 30:990ce210e8c2 125 char response = pc.getc();
Adrian Suciu 30:990ce210e8c2 126 flush_serial();
Adrian Suciu 30:990ce210e8c2 127 if(response >= '0' && response <= '9') {
Adrian Suciu 30:990ce210e8c2 128 response_ok = true;
Adrian Suciu 30:990ce210e8c2 129 cn0398.solution1 = response - '0';
Adrian Suciu 30:990ce210e8c2 130 } else if(response >= 'A' && response <= 'E') {
Adrian Suciu 30:990ce210e8c2 131 response_ok = true;
Adrian Suciu 30:990ce210e8c2 132 cn0398.solution1 = response - 'A' + 10;
Adrian Suciu 30:990ce210e8c2 133 } else if(response >= 'a' && response <= 'e') {
Adrian Suciu 30:990ce210e8c2 134 response_ok = true;
Adrian Suciu 30:990ce210e8c2 135 cn0398.solution1 = response - 'a' + 10;
Adrian Suciu 30:990ce210e8c2 136 } else {
Adrian Suciu 30:990ce210e8c2 137 response_ok = false;
Adrian Suciu 30:990ce210e8c2 138 }
Adrian Suciu 30:990ce210e8c2 139 }
Adrian Suciu 30:990ce210e8c2 140 pc.printf("%s solution selected. pH of the solution @ 25 degrees = %f \n", solutions[cn0398.solution1], ph_temp_lut[cn0398.solution1][11]);
Adrian Suciu 30:990ce210e8c2 141 pc.printf("Calibration step 2. Place pH probe in second calibration solution and press any key to start calibration.\r\n");
Adrian Suciu 30:990ce210e8c2 142 pc.getc();
Adrian Suciu 30:990ce210e8c2 143 flush_serial();
Adrian Suciu 30:990ce210e8c2 144 cn0398.calibrate_ph_pt1(temperature);
Adrian Suciu 30:990ce210e8c2 145
Adrian Suciu 30:990ce210e8c2 146 } else {
Adrian Suciu 30:990ce210e8c2 147 cn0398.use_nernst = true;
Adrian Suciu 30:990ce210e8c2 148 pc.printf("Do you want to load default calibration. If not, the Nernst equation will be used [y/N] ? ");
Adrian Suciu 30:990ce210e8c2 149 char response = pc.getc();
Adrian Suciu 30:990ce210e8c2 150 flush_serial();
Adrian Suciu 30:990ce210e8c2 151 if(response == 'y' || response == 'Y') {
Adrian Suciu 30:990ce210e8c2 152 cn0398.use_nernst = false;
Adrian Suciu 30:990ce210e8c2 153 }
Adrian Suciu 30:990ce210e8c2 154 }
Adrian Suciu 30:990ce210e8c2 155
Adrian Suciu 30:990ce210e8c2 156
Adrian Suciu 30:990ce210e8c2 157 while(1) {
Adrian Suciu 30:990ce210e8c2 158 float temperature = cn0398.read_rtd();
Adrian Suciu 30:990ce210e8c2 159 pc.printf("Temperature: %f\r\n", temperature);
Adrian Suciu 30:990ce210e8c2 160 pc.printf("pH: %f\r\n", cn0398.read_ph(temperature));
Adrian Suciu 30:990ce210e8c2 161 #ifdef MOISTURE_SENSOR_PRESENT
Adrian Suciu 30:990ce210e8c2 162 pc.printf("Moisture: %f\r\n", cn0398.read_moist());
Adrian Suciu 30:990ce210e8c2 163 #endif
Adrian Suciu 30:990ce210e8c2 164 pc.printf("\r\n");
Adrian Suciu 30:990ce210e8c2 165 wait_ms(1000);
Adrian Suciu 30:990ce210e8c2 166 }
Adrian Suciu 30:990ce210e8c2 167 return 0;
Adrian Suciu 30:990ce210e8c2 168 }