CN0357 shield demo software

Dependencies:   CN0357 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:
Thu May 19 08:50:56 2016 +0000
Revision:
0:31fb3534c148
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adisuciu 0:31fb3534c148 1 /**
adisuciu 0:31fb3534c148 2 * @file main.cpp
adisuciu 0:31fb3534c148 3 * @brief Main file for the CN0357-example project
adisuciu 0:31fb3534c148 4 * @author Analog Devices Inc.
adisuciu 0:31fb3534c148 5 *
adisuciu 0:31fb3534c148 6 * For support please go to:
adisuciu 0:31fb3534c148 7 * Github: https://github.com/analogdevicesinc/mbed-adi
adisuciu 0:31fb3534c148 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
adisuciu 0:31fb3534c148 9 * Product: www.analog.com/EVAL-CN0357-ARDZ
adisuciu 0:31fb3534c148 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
adisuciu 0:31fb3534c148 11
adisuciu 0:31fb3534c148 12 ********************************************************************************
adisuciu 0:31fb3534c148 13 * Copyright 2016(c) Analog Devices, Inc.
adisuciu 0:31fb3534c148 14 *
adisuciu 0:31fb3534c148 15 * All rights reserved.
adisuciu 0:31fb3534c148 16 *
adisuciu 0:31fb3534c148 17 * Redistribution and use in source and binary forms, with or without
adisuciu 0:31fb3534c148 18 * modification, are permitted provided that the following conditions are met:
adisuciu 0:31fb3534c148 19 * - Redistributions of source code must retain the above copyright
adisuciu 0:31fb3534c148 20 * notice, this list of conditions and the following disclaimer.
adisuciu 0:31fb3534c148 21 * - Redistributions in binary form must reproduce the above copyright
adisuciu 0:31fb3534c148 22 * notice, this list of conditions and the following disclaimer in
adisuciu 0:31fb3534c148 23 * the documentation and/or other materials provided with the
adisuciu 0:31fb3534c148 24 * distribution.
adisuciu 0:31fb3534c148 25 * - Neither the name of Analog Devices, Inc. nor the names of its
adisuciu 0:31fb3534c148 26 * contributors may be used to endorse or promote products derived
adisuciu 0:31fb3534c148 27 * from this software without specific prior written permission.
adisuciu 0:31fb3534c148 28 * - The use of this software may or may not infringe the patent rights
adisuciu 0:31fb3534c148 29 * of one or more patent holders. This license does not release you
adisuciu 0:31fb3534c148 30 * from the requirement that you obtain separate licenses from these
adisuciu 0:31fb3534c148 31 * patent holders to use this software.
adisuciu 0:31fb3534c148 32 * - Use of the software either in source or binary form, must be run
adisuciu 0:31fb3534c148 33 * on or directly connected to an Analog Devices Inc. component.
adisuciu 0:31fb3534c148 34 *
adisuciu 0:31fb3534c148 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
adisuciu 0:31fb3534c148 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
adisuciu 0:31fb3534c148 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
adisuciu 0:31fb3534c148 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
adisuciu 0:31fb3534c148 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
adisuciu 0:31fb3534c148 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
adisuciu 0:31fb3534c148 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
adisuciu 0:31fb3534c148 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
adisuciu 0:31fb3534c148 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
adisuciu 0:31fb3534c148 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adisuciu 0:31fb3534c148 45 *
adisuciu 0:31fb3534c148 46 ********************************************************************************/
adisuciu 0:31fb3534c148 47 #include "mbed.h"
adisuciu 0:31fb3534c148 48 #include "CN0357.h"
adisuciu 0:31fb3534c148 49
adisuciu 0:31fb3534c148 50 /** @mainpage
adisuciu 0:31fb3534c148 51 * CN0357 single-supply, low noise, portable gas detector circuit using an
adisuciu 0:31fb3534c148 52 * electrochemical sensor. The Alphasense CO-AX carbon monoxide sensor is used
adisuciu 0:31fb3534c148 53 * in this example. Electrochemical sensors offer several advantages for
adisuciu 0:31fb3534c148 54 * instruments that detect or measure the concentration of many toxic gases.
adisuciu 0:31fb3534c148 55 * Most sensors are gas specific and have usable resolutions under one part per
adisuciu 0:31fb3534c148 56 * million (ppm) of gas concentration.
adisuciu 0:31fb3534c148 57 *
adisuciu 0:31fb3534c148 58 * EVAL-CN0357 example program for MBED platforms. To achieve connection between
adisuciu 0:31fb3534c148 59 * the shield and the ST Nucleo, the following connections need to be made:
adisuciu 0:31fb3534c148 60 *
adisuciu 0:31fb3534c148 61 * - ICSP pin 1 -> pin 12 of the Arduino header
adisuciu 0:31fb3534c148 62 * - ICSP pin 3 -> pin 13 of the Arduino header
adisuciu 0:31fb3534c148 63 * - ICSP pin 4 -> pin 11 of the Arduino header
adisuciu 0:31fb3534c148 64 * - if VIN is not supplied, 5V to VIN
adisuciu 0:31fb3534c148 65 *
adisuciu 0:31fb3534c148 66 * @page CN0357 CN0357 Analog Devices wiki page
adisuciu 0:31fb3534c148 67 * @brief link->
adisuciu 0:31fb3534c148 68 * https://wiki.analog.com/resources/eval/user-guides/eval-adicup360/hardware/cn0357
adisuciu 0:31fb3534c148 69 * @page AD7790 AD7790 datasheet
adisuciu 0:31fb3534c148 70 * @brief link ->
adisuciu 0:31fb3534c148 71 * http://www.analog.com/media/en/technical-documentation/data-sheets/AD7790.pdf
adisuciu 0:31fb3534c148 72 * @page AD5270 AD5270 datasheet
adisuciu 0:31fb3534c148 73 * @brief link ->
adisuciu 0:31fb3534c148 74 * http://www.analog.com/media/en/technical-documentation/data-sheets/AD5270_5271.pdf
adisuciu 0:31fb3534c148 75 *
adisuciu 0:31fb3534c148 76 */
adisuciu 0:31fb3534c148 77 const float SENSOR_RANGE = 2000; ///< gas sensor range in PPM
adisuciu 0:31fb3534c148 78 const float SENSOR_SENSITIVITY = (65 * 0.000000001/*pow(10, -9)*/); ///< gas sensor sensitivity in A/ppm - 65 nA/ppm
adisuciu 0:31fb3534c148 79
adisuciu 0:31fb3534c148 80 Serial pc(USBTX, USBRX); ///< Serial interface to the pc
adisuciu 0:31fb3534c148 81
adisuciu 0:31fb3534c148 82 /**
adisuciu 0:31fb3534c148 83 @brief Displays CN0357 circuit readings and data to the UART
adisuciu 0:31fb3534c148 84
adisuciu 0:31fb3534c148 85 @param ui16Data - ADC data register value to be displayed
adisuciu 0:31fb3534c148 86 @param fData1 - ADC input voltage reading to be displayed
adisuciu 0:31fb3534c148 87 @param fdata2 - Gas Concentration reading to be displayed
adisuciu 0:31fb3534c148 88
adisuciu 0:31fb3534c148 89 **/
adisuciu 0:31fb3534c148 90 void display_data(uint8_t ui8Status_Reg, uint16_t ui16Data, float fData1, float fdata2)
adisuciu 0:31fb3534c148 91 {
adisuciu 0:31fb3534c148 92 pc.printf("\r\nStatus Register value: 0x%x", ui8Status_Reg);
adisuciu 0:31fb3534c148 93 pc.printf("\r\nADC Data Register Value = %#08x", ui16Data); /** Send valid ADC data register value*/
adisuciu 0:31fb3534c148 94 pc.printf("\r\nADC Input Voltage input = %f V", fData1); /** Send valid voltage input value */
adisuciu 0:31fb3534c148 95 pc.printf("\r\nGas Concentration = %f PPM", fdata2); /** Send valid gas concentration value */
adisuciu 0:31fb3534c148 96
adisuciu 0:31fb3534c148 97 pc.printf("\r\n");
adisuciu 0:31fb3534c148 98 }
adisuciu 0:31fb3534c148 99
adisuciu 0:31fb3534c148 100 /**
adisuciu 0:31fb3534c148 101 * Project entry-point - initializes the CN0357 shield, reads the data when the ADC is ready and outputs the sensor
adisuciu 0:31fb3534c148 102 * value in PPM
adisuciu 0:31fb3534c148 103 */
adisuciu 0:31fb3534c148 104
adisuciu 0:31fb3534c148 105 #define SINGLE_CONVERSION
adisuciu 0:31fb3534c148 106 //#define CONTINOUS_CONVERSION
adisuciu 0:31fb3534c148 107
adisuciu 0:31fb3534c148 108 int main()
adisuciu 0:31fb3534c148 109 {
adisuciu 0:31fb3534c148 110 /* Main variables */
adisuciu 0:31fb3534c148 111 CN0357 cn0357;
adisuciu 0:31fb3534c148 112 uint8_t ui8Status_Reg = 0;
adisuciu 0:31fb3534c148 113 #ifdef SINGLE_CONVERSION
adisuciu 0:31fb3534c148 114 cn0357.init(SENSOR_RANGE, SENSOR_SENSITIVITY);
adisuciu 0:31fb3534c148 115 #elif defined CONTINOUS_CONVERSION
adisuciu 0:31fb3534c148 116 cn0357.init(SENSOR_RANGE, SENSOR_SENSITIVITY, CN0357::INTERNAL_AD7790, 0x00, 0x07);
adisuciu 0:31fb3534c148 117 #else
adisuciu 0:31fb3534c148 118 #error define SINGLE_CONVERSION or CONTINOUS_CONVERSION, but not both
adisuciu 0:31fb3534c148 119 #endif
adisuciu 0:31fb3534c148 120
adisuciu 0:31fb3534c148 121
adisuciu 0:31fb3534c148 122 /* Infinite loop */
adisuciu 0:31fb3534c148 123 while (1) {
adisuciu 0:31fb3534c148 124 wait_ms(1000);
adisuciu 0:31fb3534c148 125 #ifdef CONTINOUS_CONVERSION
adisuciu 0:31fb3534c148 126 ui8Status_Reg = cn0357.read_adc_status(); // Read ADC Status Register //
adisuciu 0:31fb3534c148 127
adisuciu 0:31fb3534c148 128 if (ui8Status_Reg != 0x08) { // Checks if ADC data is available
adisuciu 0:31fb3534c148 129 pc.printf("\r\nStatus Register value: 0x%x", ui8Status_Reg);
adisuciu 0:31fb3534c148 130 } else
adisuciu 0:31fb3534c148 131 #endif
adisuciu 0:31fb3534c148 132 {
adisuciu 0:31fb3534c148 133 uint16_t ui16Adcdata = cn0357.read_sensor();
adisuciu 0:31fb3534c148 134 float fAdcVoltage = cn0357.data_to_voltage(ui16Adcdata); // Convert ADC data to voltage
adisuciu 0:31fb3534c148 135 float fConcentration = cn0357.calc_ppm(fAdcVoltage); // Convert voltage to Gas concentration
adisuciu 0:31fb3534c148 136 display_data(ui8Status_Reg, ui16Adcdata, fAdcVoltage, fConcentration); // Display data thru UART
adisuciu 0:31fb3534c148 137 }
adisuciu 0:31fb3534c148 138 }
adisuciu 0:31fb3534c148 139
adisuciu 0:31fb3534c148 140
adisuciu 0:31fb3534c148 141 /* Infinite loop, never returns. */
adisuciu 0:31fb3534c148 142 }