CN0397 (Smart Visible Light Detection)

Dependencies:   AD7798

Dependents:   cn0397-helloworld

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:
Tue Nov 08 09:56:25 2016 +0000
Revision:
1:77eb0888c15a
Parent:
0:61b289f9bab0
Fixed for mbed online compiler (array initialization not allowed within class declaration)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adisuciu 0:61b289f9bab0 1 /**
adisuciu 0:61b289f9bab0 2 * @file CN0397.cpp
adisuciu 0:61b289f9bab0 3 * @brief Header file for the CN0397
adisuciu 0:61b289f9bab0 4 * @author Analog Devices Inc.
adisuciu 0:61b289f9bab0 5 *
adisuciu 0:61b289f9bab0 6 * For support please go to:
adisuciu 0:61b289f9bab0 7 * Github: https://github.com/analogdevicesinc/mbed-adi
adisuciu 0:61b289f9bab0 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
adisuciu 0:61b289f9bab0 9 * Product: www.analog.com/EVAL-CN0397-ARDZ
adisuciu 0:61b289f9bab0 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
adisuciu 0:61b289f9bab0 11
adisuciu 0:61b289f9bab0 12 ********************************************************************************
adisuciu 0:61b289f9bab0 13 * Copyright 2016(c) Analog Devices, Inc.
adisuciu 0:61b289f9bab0 14 *
adisuciu 0:61b289f9bab0 15 * All rights reserved.
adisuciu 0:61b289f9bab0 16 *
adisuciu 0:61b289f9bab0 17 * Redistribution and use in source and binary forms, with or without
adisuciu 0:61b289f9bab0 18 * modification, are permitted provided that the following conditions are met:
adisuciu 0:61b289f9bab0 19 * - Redistributions of source code must retain the above copyright
adisuciu 0:61b289f9bab0 20 * notice, this list of conditions and the following disclaimer.
adisuciu 0:61b289f9bab0 21 * - Redistributions in binary form must reproduce the above copyright
adisuciu 0:61b289f9bab0 22 * notice, this list of conditions and the following disclaimer in
adisuciu 0:61b289f9bab0 23 * the documentation and/or other materials provided with the
adisuciu 0:61b289f9bab0 24 * distribution.
adisuciu 0:61b289f9bab0 25 * - Neither the name of Analog Devices, Inc. nor the names of its
adisuciu 0:61b289f9bab0 26 * contributors may be used to endorse or promote products derived
adisuciu 0:61b289f9bab0 27 * from this software without specific prior written permission.
adisuciu 0:61b289f9bab0 28 * - The use of this software may or may not infringe the patent rights
adisuciu 0:61b289f9bab0 29 * of one or more patent holders. This license does not release you
adisuciu 0:61b289f9bab0 30 * from the requirement that you obtain separate licenses from these
adisuciu 0:61b289f9bab0 31 * patent holders to use this software.
adisuciu 0:61b289f9bab0 32 * - Use of the software either in source or binary form, must be run
adisuciu 0:61b289f9bab0 33 * on or directly connected to an Analog Devices Inc. component.
adisuciu 0:61b289f9bab0 34 *
adisuciu 0:61b289f9bab0 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
adisuciu 0:61b289f9bab0 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
adisuciu 0:61b289f9bab0 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
adisuciu 0:61b289f9bab0 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
adisuciu 0:61b289f9bab0 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
adisuciu 0:61b289f9bab0 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
adisuciu 0:61b289f9bab0 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
adisuciu 0:61b289f9bab0 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
adisuciu 0:61b289f9bab0 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
adisuciu 0:61b289f9bab0 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adisuciu 0:61b289f9bab0 45 *
adisuciu 0:61b289f9bab0 46 ********************************************************************************/
adisuciu 0:61b289f9bab0 47
adisuciu 0:61b289f9bab0 48 #ifndef CN0397_H_
adisuciu 0:61b289f9bab0 49 #define CN0397_H_
adisuciu 0:61b289f9bab0 50
adisuciu 0:61b289f9bab0 51 #include <stdio.h>
adisuciu 0:61b289f9bab0 52 #include <string.h>
adisuciu 0:61b289f9bab0 53 #include "AD7798.h"
adisuciu 0:61b289f9bab0 54
adisuciu 0:61b289f9bab0 55 #define REGISTERS_VALUES 3
adisuciu 0:61b289f9bab0 56 #define CONVERSION_DATA 4
adisuciu 0:61b289f9bab0 57
adisuciu 0:61b289f9bab0 58 #define V_REF 3150.0 // [mV]
adisuciu 0:61b289f9bab0 59 #define _2_16 65535.0 // 2^16
adisuciu 0:61b289f9bab0 60
adisuciu 0:61b289f9bab0 61
adisuciu 0:61b289f9bab0 62 /* Available settings:
adisuciu 0:61b289f9bab0 63 * AD7798_CH_AIN1P_AIN1M - select channel 1
adisuciu 0:61b289f9bab0 64 * AD7798_CH_AIN2P_AIN2M - select channel 2
adisuciu 0:61b289f9bab0 65 * AD7798_CH_AIN3P_AIN3M - select channel 3
adisuciu 0:61b289f9bab0 66 */
adisuciu 0:61b289f9bab0 67 #define ADC_CHANNEL AD7798_CH_AIN2P_AIN2M
adisuciu 0:61b289f9bab0 68
adisuciu 0:61b289f9bab0 69 /* Available settings:
adisuciu 0:61b289f9bab0 70 * AD7798_GAIN_1, AD7798_GAIN_2,
adisuciu 0:61b289f9bab0 71 * AD7798_GAIN_4, AD7798_GAIN_8,
adisuciu 0:61b289f9bab0 72 * AD7798_GAIN_16, AD7798_GAIN_32,
adisuciu 0:61b289f9bab0 73 * AD7798_GAIN_64, AD7798_GAIN_128
adisuciu 0:61b289f9bab0 74 */
adisuciu 0:61b289f9bab0 75 #define ADC_GAIN AD7798_GAIN_1
adisuciu 0:61b289f9bab0 76 /* Available settings:
adisuciu 0:61b289f9bab0 77 * Check available value from datasheet
adisuciu 0:61b289f9bab0 78 */
adisuciu 0:61b289f9bab0 79 #define ADC_SPS 0x05 /*50SPS*/
adisuciu 0:61b289f9bab0 80
adisuciu 0:61b289f9bab0 81
adisuciu 0:61b289f9bab0 82 /* Available settings:
adisuciu 0:61b289f9bab0 83 * How often to display output values on terminal -> msec
adisuciu 0:61b289f9bab0 84 */
adisuciu 0:61b289f9bab0 85 #define DISPLAY_REFRESH 1000 /*[msec]*/
adisuciu 0:61b289f9bab0 86
adisuciu 0:61b289f9bab0 87 #define CHANNELS 3
adisuciu 0:61b289f9bab0 88
adisuciu 0:61b289f9bab0 89
adisuciu 0:61b289f9bab0 90 #define USE_CALIBRATION /* Select if you want to use system zero-scale calibration before reading the system data*/
adisuciu 0:61b289f9bab0 91 /**
adisuciu 0:61b289f9bab0 92 * Helper function used to flush the serial interface
adisuciu 0:61b289f9bab0 93 */
adisuciu 0:61b289f9bab0 94 void flush_serial();
adisuciu 0:61b289f9bab0 95
adisuciu 0:61b289f9bab0 96 /**
adisuciu 0:61b289f9bab0 97 * @brief The CN0397 shield class
adisuciu 0:61b289f9bab0 98 */
adisuciu 0:61b289f9bab0 99 class CN0397
adisuciu 0:61b289f9bab0 100 {
adisuciu 0:61b289f9bab0 101 public:
adisuciu 0:61b289f9bab0 102 /**
adisuciu 0:61b289f9bab0 103 * @brief The CN0397 class constructor
adisuciu 0:61b289f9bab0 104 */
adisuciu 0:61b289f9bab0 105 CN0397(PinName cs);
adisuciu 0:61b289f9bab0 106 /**
adisuciu 0:61b289f9bab0 107 * @brief Initialization method of the class. Initializes the AD7798 and initiates calibration if needed
adisuciu 0:61b289f9bab0 108 */
adisuciu 0:61b289f9bab0 109 void init(void);
adisuciu 0:61b289f9bab0 110
adisuciu 0:61b289f9bab0 111 /**
adisuciu 0:61b289f9bab0 112 * @brief Displays data on the serial interface
adisuciu 0:61b289f9bab0 113 */
adisuciu 0:61b289f9bab0 114 void display_data(void);
adisuciu 0:61b289f9bab0 115
adisuciu 0:61b289f9bab0 116 /**
adisuciu 0:61b289f9bab0 117 * @brief Converts ADC counts to voltage
adisuciu 0:61b289f9bab0 118 * @param adcValue - ADC counts
adisuciu 0:61b289f9bab0 119 * @param voltage - computed voltage
adisuciu 0:61b289f9bab0 120 */
adisuciu 0:61b289f9bab0 121 void data_to_voltage(uint16_t adcValue, float *voltage);
adisuciu 0:61b289f9bab0 122
adisuciu 0:61b289f9bab0 123 /**
adisuciu 0:61b289f9bab0 124 * @brief Computes light intensity of the channel
adisuciu 0:61b289f9bab0 125 * @param channel - channel to be converted
adisuciu 0:61b289f9bab0 126 * @param adcValue - ADC counts
adisuciu 0:61b289f9bab0 127 * @param intensity - computed light intensity
adisuciu 0:61b289f9bab0 128 */
adisuciu 0:61b289f9bab0 129 void calc_light_intensity(uint8_t channel, uint16_t adcValue, float *intensity);
adisuciu 0:61b289f9bab0 130
adisuciu 0:61b289f9bab0 131 /**
adisuciu 0:61b289f9bab0 132 * @brief Computes light concentration from light intensity
adisuciu 0:61b289f9bab0 133 * @param channel - channel to be converted
adisuciu 0:61b289f9bab0 134 * @param intensity - light intensity
adisuciu 0:61b289f9bab0 135 * @param conc - computed light concentration
adisuciu 0:61b289f9bab0 136 */
adisuciu 0:61b289f9bab0 137 void calc_light_concentration(uint8_t channel, float intensity, float *conc);
adisuciu 0:61b289f9bab0 138
adisuciu 0:61b289f9bab0 139 /**
adisuciu 0:61b289f9bab0 140 * @brief Reads the ADC channels and computes intensity and concentration
adisuciu 0:61b289f9bab0 141 */
adisuciu 0:61b289f9bab0 142 void set_app_data(void);
adisuciu 0:61b289f9bab0 143
adisuciu 0:61b289f9bab0 144 /**
adisuciu 0:61b289f9bab0 145 * @brief Calibrates the channel
adisuciu 0:61b289f9bab0 146 * @param channel - channel to be calibrated
adisuciu 0:61b289f9bab0 147 */
adisuciu 0:61b289f9bab0 148 void calibration(uint8_t channel);
adisuciu 0:61b289f9bab0 149
adisuciu 0:61b289f9bab0 150 /**
adisuciu 0:61b289f9bab0 151 * @brief Instance of the AD7798
adisuciu 0:61b289f9bab0 152 */
adisuciu 0:61b289f9bab0 153 AD7798 ad7798;
adisuciu 0:61b289f9bab0 154 private:
adisuciu 0:61b289f9bab0 155
adisuciu 0:61b289f9bab0 156 uint8_t statusReg, idReg, ioReg, gainAdc;
adisuciu 0:61b289f9bab0 157 uint16_t modeReg, configReg, offsetReg, fullscaleReg, dataReg;
adisuciu 0:61b289f9bab0 158 uint16_t adcValue[3];
adisuciu 0:61b289f9bab0 159 float voltageValue[3], intensityValue[3], lightConcentration[3];
adisuciu 1:77eb0888c15a 160
adisuciu 1:77eb0888c15a 161 static const uint8_t Channels[3];
adisuciu 1:77eb0888c15a 162 static const char colour[3][6];
adisuciu 1:77eb0888c15a 163 static const uint8_t ColorPrint[3];
adisuciu 1:77eb0888c15a 164 static const uint8_t Gain[8];
adisuciu 1:77eb0888c15a 165 static const float Lux_LSB[3];
adisuciu 1:77eb0888c15a 166 static const float Optimal_Levels[3];
adisuciu 1:77eb0888c15a 167
adisuciu 0:61b289f9bab0 168 };
adisuciu 0:61b289f9bab0 169
adisuciu 0:61b289f9bab0 170 #endif /* CN0397_H_ */
adisuciu 0:61b289f9bab0 171