CN0398 hello world
For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
main.cpp@1:c8909fa1a876, 2016-10-26 (annotated)
- Committer:
- adisuciu
- Date:
- Wed Oct 26 13:49:36 2016 +0000
- Revision:
- 1:c8909fa1a876
- Parent:
- 0:3dc6864754ca
Added extra step in initialization
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:c8909fa1a876 | 77 | pc.printf("CN0398 - ph and moisture measurement demo\r\nPress any key to continue...\r\n"); |
adisuciu | 1:c8909fa1a876 | 78 | pc.getc(); |
adisuciu | 1:c8909fa1a876 | 79 | wait_ms(10); |
adisuciu | 1:c8909fa1a876 | 80 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 81 | |
adisuciu | 0:3dc6864754ca | 82 | pc.printf("Do you want to perform calibration [y/N] ? "); |
adisuciu | 0:3dc6864754ca | 83 | char response = pc.getc(); |
adisuciu | 0:3dc6864754ca | 84 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 85 | if(response == 'y' || response == 'Y') { |
adisuciu | 0:3dc6864754ca | 86 | pc.printf("Do you want to calibrate offset voltage [y/N] ? "); |
adisuciu | 0:3dc6864754ca | 87 | response = pc.getc(); |
adisuciu | 0:3dc6864754ca | 88 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 89 | if(response == 'y' || response == 'Y') { |
adisuciu | 0:3dc6864754ca | 90 | pc.printf("Calibration step 0. Short the pH probe and press any key to calibrate.\r\n"); |
adisuciu | 0:3dc6864754ca | 91 | pc.getc(); |
adisuciu | 0:3dc6864754ca | 92 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 93 | cn0398.calibrate_ph_offset(); |
adisuciu | 0:3dc6864754ca | 94 | } |
adisuciu | 0:3dc6864754ca | 95 | print_calibration_solutions(); |
adisuciu | 0:3dc6864754ca | 96 | |
adisuciu | 0:3dc6864754ca | 97 | bool response_ok = false; |
adisuciu | 0:3dc6864754ca | 98 | while(response_ok == false) { |
adisuciu | 0:3dc6864754ca | 99 | pc.printf("\nInput calibration solution used for first step [1-9][a-e] \n"); |
adisuciu | 0:3dc6864754ca | 100 | char response = pc.getc(); |
adisuciu | 0:3dc6864754ca | 101 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 102 | if(response >= '0' && response <= '9') { |
adisuciu | 0:3dc6864754ca | 103 | response_ok = true; |
adisuciu | 0:3dc6864754ca | 104 | cn0398.solution0 = response - '0'; |
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 if(response >= 'a' && response <= 'e') { |
adisuciu | 0:3dc6864754ca | 109 | response_ok = true; |
adisuciu | 0:3dc6864754ca | 110 | cn0398.solution0 = response - 'a' + 10; |
adisuciu | 0:3dc6864754ca | 111 | } else { |
adisuciu | 0:3dc6864754ca | 112 | response_ok = false; |
adisuciu | 0:3dc6864754ca | 113 | } |
adisuciu | 0:3dc6864754ca | 114 | } |
adisuciu | 0:3dc6864754ca | 115 | 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 | 116 | float temperature = cn0398.read_rtd(); |
adisuciu | 0:3dc6864754ca | 117 | pc.printf("Calibration step 1. Place pH probe in first calibration solution and press any key to start calibration.\r\n"); |
adisuciu | 0:3dc6864754ca | 118 | pc.getc(); |
adisuciu | 0:3dc6864754ca | 119 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 120 | cn0398.calibrate_ph_pt0(temperature); |
adisuciu | 0:3dc6864754ca | 121 | |
adisuciu | 0:3dc6864754ca | 122 | response_ok = false; |
adisuciu | 0:3dc6864754ca | 123 | while(response_ok == false) { |
adisuciu | 0:3dc6864754ca | 124 | pc.printf("Input calibration solution used for second step [1-9][a-e] \n"); |
adisuciu | 0:3dc6864754ca | 125 | char response = pc.getc(); |
adisuciu | 0:3dc6864754ca | 126 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 127 | if(response >= '0' && response <= '9') { |
adisuciu | 0:3dc6864754ca | 128 | response_ok = true; |
adisuciu | 0:3dc6864754ca | 129 | cn0398.solution1 = response - '0'; |
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 if(response >= 'a' && response <= 'e') { |
adisuciu | 0:3dc6864754ca | 134 | response_ok = true; |
adisuciu | 0:3dc6864754ca | 135 | cn0398.solution1 = response - 'a' + 10; |
adisuciu | 0:3dc6864754ca | 136 | } else { |
adisuciu | 0:3dc6864754ca | 137 | response_ok = false; |
adisuciu | 0:3dc6864754ca | 138 | } |
adisuciu | 0:3dc6864754ca | 139 | } |
adisuciu | 0:3dc6864754ca | 140 | 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 | 141 | pc.printf("Calibration step 2. Place pH probe in second calibration solution and press any key to start calibration.\r\n"); |
adisuciu | 0:3dc6864754ca | 142 | pc.getc(); |
adisuciu | 0:3dc6864754ca | 143 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 144 | cn0398.calibrate_ph_pt1(temperature); |
adisuciu | 0:3dc6864754ca | 145 | |
adisuciu | 0:3dc6864754ca | 146 | } else { |
adisuciu | 0:3dc6864754ca | 147 | cn0398.use_nernst = true; |
adisuciu | 0:3dc6864754ca | 148 | pc.printf("Do you want to load default calibration. If not, the Nernst equation will be used [y/N] ? "); |
adisuciu | 0:3dc6864754ca | 149 | char response = pc.getc(); |
adisuciu | 0:3dc6864754ca | 150 | flush_serial(); |
adisuciu | 0:3dc6864754ca | 151 | if(response == 'y' || response == 'Y') { |
adisuciu | 0:3dc6864754ca | 152 | cn0398.use_nernst = false; |
adisuciu | 0:3dc6864754ca | 153 | } |
adisuciu | 0:3dc6864754ca | 154 | } |
adisuciu | 0:3dc6864754ca | 155 | |
adisuciu | 0:3dc6864754ca | 156 | |
adisuciu | 0:3dc6864754ca | 157 | while(1) { |
adisuciu | 0:3dc6864754ca | 158 | float temperature = cn0398.read_rtd(); |
adisuciu | 0:3dc6864754ca | 159 | pc.printf("Temperature: %f\r\n", temperature); |
adisuciu | 0:3dc6864754ca | 160 | pc.printf("pH: %f\r\n", cn0398.read_ph(temperature)); |
adisuciu | 0:3dc6864754ca | 161 | #ifdef MOISTURE_SENSOR_PRESENT |
adisuciu | 0:3dc6864754ca | 162 | pc.printf("Moisture: %f\r\n", cn0398.read_moist()); |
adisuciu | 0:3dc6864754ca | 163 | #endif |
adisuciu | 0:3dc6864754ca | 164 | pc.printf("\r\n"); |
adisuciu | 0:3dc6864754ca | 165 | wait_ms(1000); |
adisuciu | 0:3dc6864754ca | 166 | } |
adisuciu | 0:3dc6864754ca | 167 | return 0; |
adisuciu | 0:3dc6864754ca | 168 | } |