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.
CN0216.cpp
00001 /** 00002 * @file cn0216.cpp 00003 * @brief Source file for CN0216 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-CN0216-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 "mbed.h" 00049 #include "AD7791.h" 00050 #include "CN0216.h" 00051 extern Serial pc; 00052 00053 /** 00054 * CN0216 constructor 00055 * @param CSAD7791 - Chipselect of the AD7791 00056 * @param MOSI - MOSI line of the SPI bus 00057 * @param MISO - MISO line of the SPI bus 00058 * @param SCK - SCK line of the SPI bus 00059 */ 00060 CN0216::CN0216(PinName CSAD7791, PinName MOSI, PinName MISO, PinName SCK) : ad7791(1.2, CSAD7791, MOSI, MISO, SCK) 00061 { 00062 _cal_weight = 0; 00063 _zero_scale_value = 0; 00064 _full_scale_value = 0; 00065 _weight_units_per_bit = 0; 00066 } 00067 00068 /** 00069 * Initializes the mode and filter values of the AD7791 and sets the weight to be used in calibration 00070 * @param cal_weight - weight used in calibration 00071 * @param mode_val - value of the mode register 00072 * @param filter_val - value of the filter register 00073 */ 00074 void CN0216::init(float cal_weight, uint8_t mode_val, uint8_t filter_val) 00075 { 00076 _cal_weight = cal_weight; 00077 ad7791.frequency(500000); 00078 wait_ms(50); 00079 ad7791.reset(); 00080 wait_ms(50); 00081 ad7791.write_mode_reg(mode_val); 00082 wait_us(2); 00083 ad7791.write_filter_reg(filter_val); 00084 wait_ms(50); 00085 } 00086 00087 /** 00088 * Calibrates the CN0216 weigh scale 00089 * @param cal - calibration step. 00090 * Step CN0216::ZERO_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the minimum as value for the zero scale 00091 * Step CN0216::FULL_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the average as value for the full scale 00092 * Step COMPUTE_UNITS_PER_BIT will compute the grams per bit used in weight computation. 00093 */ 00094 void CN0216::calibrate(CalibrationStep_t cal) 00095 { 00096 uint64_t sum = 0; 00097 uint32_t min = 0xFFFFFFFF; 00098 uint32_t sample = 0; 00099 switch(cal) { 00100 case ZERO_SCALE_CALIBRATION: 00101 case FULL_SCALE_CALIBRATION: 00102 for(int i = 0; i < _NUMBER_OF_SAMPLES; i++) { 00103 sample = ad7791.read_u32(); 00104 min = (min < sample) ? min : sample; 00105 sum += ad7791.read_u32(); 00106 wait_us(5); 00107 } 00108 if(cal == ZERO_SCALE_CALIBRATION) { 00109 // pc.printf("ZERO SCALE VALUE = %x",sum); 00110 _zero_scale_value = min; 00111 } else { 00112 // pc.printf("FULL SCALE VALUE = %x",sum); 00113 sum = sum / _NUMBER_OF_SAMPLES; 00114 _full_scale_value = sum; 00115 } 00116 break; 00117 00118 case COMPUTE_UNITS_PER_BIT: 00119 _weight_units_per_bit = _cal_weight / (static_cast<float> (_full_scale_value - _zero_scale_value)); /* Calculate number of grams per LSB */ 00120 // pc.printf("GRAMS/LSB = %f", _grams_per_bit); 00121 break; 00122 default: 00123 break; 00124 } 00125 00126 } 00127 00128 /** 00129 * Computes the weight based on the formula 00130 * weight = (data - zeroscale) * weight_units_per_bit 00131 * @param data read from the ADC 00132 * @return weight based on data 00133 */ 00134 float CN0216::compute_weight(uint32_t data) 00135 { 00136 // pc.printf("\r\nFULL_SCALE_VALUE = %x\r\nZERO_SCALE_VALUE = %x\r\nDATA READ = %x\r\nGRAMS/LSB = %f\r\n",_full_scale_value,data,_zero_scale_value,_grams_per_bit); 00137 if(data < _zero_scale_value) 00138 data = _zero_scale_value; // clamp data to 0 00139 float weight_in_grams = (static_cast<float>((data) - _zero_scale_value)) * _weight_units_per_bit; /* Calculate weight */ 00140 return weight_in_grams; 00141 } 00142 00143 /** 00144 * Reads the AD7791 00145 * @return value read by the ADC 00146 */ 00147 uint32_t CN0216::read_u32() 00148 { 00149 return ad7791.read_u32(); 00150 } 00151 00152 /** 00153 * Reads the ADC and computes the weight based on the formula described above. 00154 * @return weight 00155 */ 00156 float CN0216::read_weight() 00157 { 00158 uint32_t weight = read_u32(); 00159 return compute_weight(weight); 00160 } 00161
Generated on Tue Jul 12 2022 17:59:52 by
 1.7.2
 1.7.2 
     CN0357 - Toxic gas measurement
            CN0357 - Toxic gas measurement
         CN0216 - Weight Scale
            CN0216 - Weight Scale