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