CN0398 hello world

Dependencies:   CN0398 mbed

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:
Mon Oct 24 16:09:11 2016 +0000
Revision:
0:3dc6864754ca
Child:
1:c8909fa1a876
Initial revision

Who changed what in which revision?

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