Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CN0397.cpp
00001 /** 00002 * @file CN0397.cpp 00003 * @brief Source file for the CN0397 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-CN0397-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 00048 #include "CN0397.h" 00049 #include "AD7798.h" 00050 #include "Timer.h" 00051 #include <stdio.h> 00052 #include <math.h> 00053 #include <mbed.h> 00054 00055 extern Serial pc; 00056 00057 00058 void flush_serial() 00059 { 00060 wait_ms(10); // wait for all data to come through 00061 while (pc.readable()) pc.getc(); 00062 } 00063 00064 00065 CN0397::CN0397(PinName cs) : ad7798(cs) 00066 { 00067 00068 } 00069 void CN0397::display_data(void) 00070 { 00071 00072 uint8_t channel = 0, i; 00073 00074 /* for(channel = 0; channel < CHANNELS; channel++){ 00075 00076 pc.printf("%s (channel %d):\n", colour[channel], (channel + 1)); 00077 pc.printf("\tADC output = %u (%#06x)\n", adcValue[channel], adcValue[channel]); 00078 pc.printf("\tVoltage = %f mV\n", voltageValue[channel]); 00079 pc.printf("\tLight Intensity = %.2f lux\n", intensityValue[channel]); 00080 pc.printf("\tLight Concentration = %.2f%c\n", lightConcentration[channel], 37); 00081 00082 }*/ 00083 00084 00085 for(channel = 0; channel < CHANNELS; channel++) { 00086 00087 pc.printf("\t\t\033[2;%dm%s\033[0m channel:\t\t", ColorPrint[channel], colour[channel]); 00088 } 00089 00090 pc.printf("\n"); 00091 pc.printf("\t"); 00092 00093 /* 00094 for(channel = 0; channel < CHANNELS; channel++) { 00095 00096 pc.printf("\t\t\tADC output = %u (%#06x)", adcValue[channel], adcValue[channel]); 00097 } 00098 00099 pc.printf("\n"); 00100 pc.printf("\t"); 00101 00102 */ 00103 for(channel = 0; channel < CHANNELS; channel++) { 00104 00105 pc.printf("\t\tLight Intensity = %.2f lux", intensityValue[channel]); 00106 } 00107 00108 pc.printf("\n"); 00109 pc.printf("\t"); 00110 00111 00112 for(channel = 0; channel < CHANNELS; channel++) { 00113 00114 pc.printf("\t\tLight Concentration = %.2f%c", lightConcentration[channel], 37); 00115 } 00116 00117 pc.printf("\n");/*pc.printf("\t"); 00118 00119 for(channel = 0; channel < CHANNELS; channel++) { 00120 00121 pc.printf("\t\t\tLight Concentration = %.2f%c", lightConcentration[channel], 37); 00122 } 00123 00124 pc.printf("\n");pc.printf("\t"); 00125 00126 /* for(channel = 0; channel < CHANNELS; channel++){ 00127 00128 pc.printf("\t\t\t\033[2;%dm|\033[0m\t", ColorPrint[channel]); 00129 00130 }*/ 00131 00132 for(channel = 0; channel < 5; channel++) { 00133 pc.printf("\n"); 00134 } 00135 00136 // pc.printf("\n"); 00137 // pc.printf("\n"); 00138 pc.printf("\n"); 00139 } 00140 00141 00142 void CN0397::data_to_voltage(uint16_t adcValue, float *voltage) 00143 { 00144 00145 *voltage = (float)(adcValue * V_REF) / (float)(_2_16 * gainAdc); 00146 00147 } 00148 00149 void CN0397::init(void) 00150 { 00151 ad7798.reset(); 00152 uint8_t channel; 00153 00154 if(ad7798.init()) { 00155 00156 ad7798.set_coding_mode(AD7798_UNIPOLAR); 00157 ad7798.set_mode(AD7798_MODE_SINGLE); 00158 ad7798.set_gain(ADC_GAIN); 00159 ad7798.set_filter(ADC_SPS); 00160 ad7798.set_reference(AD7798_REFDET_ENA); 00161 00162 } 00163 00164 gainAdc = Gain[ADC_GAIN]; 00165 #ifdef USE_CALIBRATION 00166 pc.printf("Calibrate the system:\n"); 00167 pc.printf("\n"); 00168 00169 for(channel = 0; channel < CHANNELS; channel++) { 00170 00171 pc.printf("\tCalibrate channel %d: be sure that %s photodiode is cover and press <ENTER>.\n", (Channels[channel] + 1), colour[channel]); 00172 pc.getc(); 00173 flush_serial(); 00174 calibration(Channels[channel]); 00175 pc.printf("\t\tChannel %d is calibrated!\n", (Channels[channel] + 1)); 00176 pc.printf("\n"); 00177 00178 } 00179 00180 printf("System calibration complete!\n"); 00181 printf("\n"); 00182 printf("\n"); 00183 00184 #endif 00185 } 00186 00187 void CN0397::calc_light_intensity(uint8_t channel, uint16_t adcValue, float *intensity) 00188 { 00189 00190 *intensity = adcValue * Lux_LSB[channel]; 00191 00192 } 00193 00194 void CN0397::calc_light_concentration(uint8_t channel, float intensity, float *conc) 00195 { 00196 00197 *conc = (intensity * 100) / Optimal_Levels[channel]; 00198 00199 } 00200 00201 void CN0397::set_app_data(void) 00202 { 00203 uint8_t channel, rgbChannel; 00204 uint16_t *adcData = &adcValue[0]; 00205 float *voltageData = &voltageValue[0], *intensityData = &intensityValue[0]; 00206 float *concData = &lightConcentration[0]; 00207 00208 for(channel = 0; channel < CHANNELS; channel++) { 00209 00210 rgbChannel = Channels[channel]; 00211 00212 ad7798.set_channel(channel); 00213 00214 adcData = &adcValue[rgbChannel]; 00215 voltageData = &voltageValue[rgbChannel]; 00216 intensityData = &intensityValue[rgbChannel]; 00217 concData = &lightConcentration[rgbChannel]; 00218 ad7798.read_data(channel, adcData); 00219 data_to_voltage(*adcData, voltageData); 00220 calc_light_intensity(rgbChannel, *adcData, intensityData); 00221 calc_light_concentration(rgbChannel, *intensityData, concData); 00222 00223 /* adcData++; 00224 voltageData++; 00225 intensityData++; 00226 concData++;*/ 00227 00228 } 00229 } 00230 00231 void CN0397::calibration(uint8_t channel) 00232 { 00233 00234 uint16_t setValue; 00235 00236 ad7798.set_channel(channel); //select channel to calibrate 00237 00238 // Perform system zero-scale calibration 00239 setValue = ad7798.get_register_value(AD7798_REG_MODE, 2); 00240 setValue &= ~(AD7798_MODE_SEL(0x07)); 00241 setValue |= AD7798_MODE_SEL(AD7798_MODE_CAL_SYS_ZERO); 00242 ad7798.set_register_value(AD7798_REG_MODE, setValue, 2); 00243 00244 while((ad7798.get_register_value( AD7798_REG_STAT, 1) & channel) != channel); // wait for RDY bit to go low 00245 00246 while(ad7798.get_register_value(AD7798_REG_MODE, 2) != 0x4005); // wait for ADC to go in idle mode 00247 00248 00249 } 00250 00251 00252 00253
Generated on Tue Jul 12 2022 17:59:52 by
1.7.2
CN0357 - Toxic gas measurement
CN0216 - Weight Scale