A collection of Analog Devices drivers for the mbed platform

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  *   @file     main.cpp
00003  *   @brief    Main file for the CN0398-example project
00004  *   @author   Analog Devices Inc.
00005  *
00006  * For support please go to:
00007  * Github: https://github.com/analogdevicesinc/mbed-adi
00008  * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
00009  * Product: www.analog.com/EVAL-CN0398-ARDZ
00010  * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
00011 
00012  ********************************************************************************
00013  * Copyright 2016(c) Analog Devices, Inc.
00014  *
00015  * All rights reserved.
00016  *
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions are met:
00019  *  - Redistributions of source code must retain the above copyright
00020  *    notice, this list of conditions and the following disclaimer.
00021  *  - Redistributions in binary form must reproduce the above copyright
00022  *    notice, this list of conditions and the following disclaimer in
00023  *    the documentation and/or other materials provided with the
00024  *    distribution.
00025  *  - Neither the name of Analog Devices, Inc. nor the names of its
00026  *    contributors may be used to endorse or promote products derived
00027  *    from this software without specific prior written permission.
00028  *  - The use of this software may or may not infringe the patent rights
00029  *    of one or more patent holders.  This license does not release you
00030  *    from the requirement that you obtain separate licenses from these
00031  *    patent holders to use this software.
00032  *  - Use of the software either in source or binary form, must be run
00033  *    on or directly connected to an Analog Devices Inc. component.
00034  *
00035  * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00036  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00037  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00038  * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00039  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00040  * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00041  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00042  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00043  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00044  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00045  *
00046  ********************************************************************************/
00047 #include "mbed.h"
00048 #include "CN0398.h"
00049 
00050 Serial pc(SERIAL_TX, SERIAL_RX);
00051 
00052 /**
00053  * @brief: Prints calibration solutions
00054  */
00055 void print_calibration_solutions()
00056 {
00057     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");
00058 }
00059 
00060 /**
00061  * @brief: flushes the serial interface.
00062  */
00063 void flush_serial()
00064 {
00065     wait_ms(10); // wait for all data to come through
00066     while (pc.readable())  pc.getc();
00067 }
00068 
00069 int main()
00070 {
00071     pc.baud(115200);
00072     CN0398 cn0398(D10, D4);
00073     cn0398.reset();
00074     cn0398.setup();
00075     cn0398.init();
00076     wait_ms(500);
00077     pc.printf("CN0398 - ph and moisture measurement demo\r\nPress any key to continue...\r\n");
00078     pc.getc();
00079     wait_ms(10);
00080     flush_serial();
00081 
00082     pc.printf("Do you want to perform calibration [y/N] ? ");
00083     char response = pc.getc();
00084     flush_serial();
00085     if(response == 'y' || response == 'Y') {
00086         pc.printf("Do you want to calibrate offset voltage [y/N] ? ");
00087         response = pc.getc();
00088         flush_serial();
00089         if(response == 'y' || response == 'Y') {
00090             pc.printf("Calibration step 0. Short the pH probe and press any key to calibrate.\r\n");
00091             pc.getc();
00092             flush_serial();
00093             cn0398.calibrate_ph_offset();
00094         }
00095         print_calibration_solutions();
00096 
00097         bool response_ok = false;
00098         while(response_ok == false) {
00099             pc.printf("\nInput calibration solution used for first step [1-9][a-e] \n");
00100             char response = pc.getc();
00101             flush_serial();
00102             if(response >= '0' && response <= '9')  {
00103                 response_ok = true;
00104                 cn0398.solution0 = response - '0';
00105             } else if(response >= 'A' && response <= 'E') {
00106                 response_ok = true;
00107                 cn0398.solution0 = response - 'A' + 10;
00108             } else if(response >= 'a' && response <= 'e') {
00109                 response_ok = true;
00110                 cn0398.solution0 = response - 'a' + 10;
00111             } else {
00112                 response_ok = false;
00113             }
00114         }
00115         pc.printf("%s solution selected. pH of the solution @ 25 degrees = %f \n", solutions[cn0398.solution0], ph_temp_lut[cn0398.solution0][11]);
00116         float temperature = cn0398.read_rtd();
00117         pc.printf("Calibration step 1. Place pH probe in first calibration solution and press any key to start calibration.\r\n");
00118         pc.getc();
00119         flush_serial();
00120         cn0398.calibrate_ph_pt0(temperature);
00121 
00122         response_ok = false;
00123         while(response_ok == false) {
00124             pc.printf("Input calibration solution used for second step [1-9][a-e] \n");
00125             char response = pc.getc();
00126             flush_serial();
00127             if(response >= '0' && response <= '9')  {
00128                 response_ok = true;
00129                 cn0398.solution1 = response - '0';
00130             } else if(response >= 'A' && response <= 'E') {
00131                 response_ok = true;
00132                 cn0398.solution1 = response - 'A' + 10;
00133             } else if(response >= 'a' && response <= 'e') {
00134                 response_ok = true;
00135                 cn0398.solution1 = response - 'a' + 10;
00136             } else {
00137                 response_ok = false;
00138             }
00139         }
00140         pc.printf("%s solution selected. pH of the solution @ 25 degrees = %f \n", solutions[cn0398.solution1], ph_temp_lut[cn0398.solution1][11]);
00141         pc.printf("Calibration step 2. Place pH probe in second calibration solution and press any key to start calibration.\r\n");
00142         pc.getc();
00143         flush_serial();
00144         cn0398.calibrate_ph_pt1(temperature);
00145 
00146     } else {
00147         cn0398.use_nernst = true;
00148         pc.printf("Do you want to load default calibration. If not, the Nernst equation will be used [y/N] ? ");
00149         char response = pc.getc();
00150         flush_serial();
00151         if(response == 'y' || response == 'Y') {
00152             cn0398.use_nernst = false;
00153         }
00154     }
00155 
00156 
00157     while(1) {
00158         float temperature = cn0398.read_rtd();
00159         pc.printf("Temperature: %f\r\n", temperature);
00160         pc.printf("pH: %f\r\n", cn0398.read_ph(temperature));
00161 #ifdef MOISTURE_SENSOR_PRESENT
00162         pc.printf("Moisture: %f\r\n", cn0398.read_moist());
00163 #endif
00164         pc.printf("\r\n");
00165         wait_ms(1000);
00166     }
00167     return 0;
00168 }