CN0216 weigh scale sensor library

Dependencies:   AD7791

Dependents:   CN0216-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:
Thu Aug 18 12:36:12 2016 +0000
Revision:
2:ea269fe29bab
Parent:
0:afcb81077332
Implemented missing doxygen tags

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adisuciu 0:afcb81077332 1 /**
adisuciu 0:afcb81077332 2 * @file cn0216.cpp
adisuciu 0:afcb81077332 3 * @brief Source file for CN0216
adisuciu 0:afcb81077332 4 * @author Analog Devices Inc.
adisuciu 0:afcb81077332 5 *
adisuciu 0:afcb81077332 6 * For support please go to:
adisuciu 0:afcb81077332 7 * Github: https://github.com/analogdevicesinc/mbed-adi
adisuciu 0:afcb81077332 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
adisuciu 0:afcb81077332 9 * Product: www.analog.com/EVAL-CN0216-ARDZ
adisuciu 0:afcb81077332 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
adisuciu 0:afcb81077332 11
adisuciu 0:afcb81077332 12 ********************************************************************************
adisuciu 0:afcb81077332 13 * Copyright 2016(c) Analog Devices, Inc.
adisuciu 0:afcb81077332 14 *
adisuciu 0:afcb81077332 15 * All rights reserved.
adisuciu 0:afcb81077332 16 *
adisuciu 0:afcb81077332 17 * Redistribution and use in source and binary forms, with or without
adisuciu 0:afcb81077332 18 * modification, are permitted provided that the following conditions are met:
adisuciu 0:afcb81077332 19 * - Redistributions of source code must retain the above copyright
adisuciu 0:afcb81077332 20 * notice, this list of conditions and the following disclaimer.
adisuciu 0:afcb81077332 21 * - Redistributions in binary form must reproduce the above copyright
adisuciu 0:afcb81077332 22 * notice, this list of conditions and the following disclaimer in
adisuciu 0:afcb81077332 23 * the documentation and/or other materials provided with the
adisuciu 0:afcb81077332 24 * distribution.
adisuciu 0:afcb81077332 25 * - Neither the name of Analog Devices, Inc. nor the names of its
adisuciu 0:afcb81077332 26 * contributors may be used to endorse or promote products derived
adisuciu 0:afcb81077332 27 * from this software without specific prior written permission.
adisuciu 0:afcb81077332 28 * - The use of this software may or may not infringe the patent rights
adisuciu 0:afcb81077332 29 * of one or more patent holders. This license does not release you
adisuciu 0:afcb81077332 30 * from the requirement that you obtain separate licenses from these
adisuciu 0:afcb81077332 31 * patent holders to use this software.
adisuciu 0:afcb81077332 32 * - Use of the software either in source or binary form, must be run
adisuciu 0:afcb81077332 33 * on or directly connected to an Analog Devices Inc. component.
adisuciu 0:afcb81077332 34 *
adisuciu 0:afcb81077332 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
adisuciu 0:afcb81077332 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
adisuciu 0:afcb81077332 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
adisuciu 0:afcb81077332 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
adisuciu 0:afcb81077332 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
adisuciu 0:afcb81077332 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
adisuciu 0:afcb81077332 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
adisuciu 0:afcb81077332 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
adisuciu 0:afcb81077332 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
adisuciu 0:afcb81077332 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adisuciu 0:afcb81077332 45 *
adisuciu 0:afcb81077332 46 ********************************************************************************/
adisuciu 0:afcb81077332 47
adisuciu 0:afcb81077332 48 #include "mbed.h"
adisuciu 0:afcb81077332 49 #include "AD7791.h"
adisuciu 0:afcb81077332 50 #include "CN0216.h"
adisuciu 0:afcb81077332 51 extern Serial pc;
adisuciu 0:afcb81077332 52
adisuciu 0:afcb81077332 53 /**
adisuciu 0:afcb81077332 54 * CN0216 constructor
adisuciu 0:afcb81077332 55 * @param CSAD7791 - Chipselect of the AD7791
adisuciu 0:afcb81077332 56 * @param MOSI - MOSI line of the SPI bus
adisuciu 0:afcb81077332 57 * @param MISO - MISO line of the SPI bus
adisuciu 0:afcb81077332 58 * @param SCK - SCK line of the SPI bus
adisuciu 0:afcb81077332 59 */
adisuciu 0:afcb81077332 60 CN0216::CN0216(PinName CSAD7791, PinName MOSI, PinName MISO, PinName SCK) : ad7791(1.2, CSAD7791, MOSI, MISO, SCK)
adisuciu 0:afcb81077332 61 {
adisuciu 0:afcb81077332 62 _cal_weight = 0;
adisuciu 0:afcb81077332 63 _zero_scale_value = 0;
adisuciu 0:afcb81077332 64 _full_scale_value = 0;
adisuciu 0:afcb81077332 65 _weight_units_per_bit = 0;
adisuciu 0:afcb81077332 66 }
adisuciu 0:afcb81077332 67
adisuciu 0:afcb81077332 68 /**
adisuciu 0:afcb81077332 69 * Initializes the mode and filter values of the AD7791 and sets the weight to be used in calibration
adisuciu 0:afcb81077332 70 * @param cal_weight - weight used in calibration
adisuciu 0:afcb81077332 71 * @param mode_val - value of the mode register
adisuciu 0:afcb81077332 72 * @param filter_val - value of the filter register
adisuciu 0:afcb81077332 73 */
adisuciu 0:afcb81077332 74 void CN0216::init(float cal_weight, uint8_t mode_val, uint8_t filter_val)
adisuciu 0:afcb81077332 75 {
adisuciu 0:afcb81077332 76 _cal_weight = cal_weight;
adisuciu 0:afcb81077332 77 ad7791.frequency(500000);
adisuciu 0:afcb81077332 78 wait_ms(50);
adisuciu 0:afcb81077332 79 ad7791.reset();
adisuciu 0:afcb81077332 80 wait_ms(50);
adisuciu 0:afcb81077332 81 ad7791.write_mode_reg(mode_val);
adisuciu 0:afcb81077332 82 wait_us(2);
adisuciu 0:afcb81077332 83 ad7791.write_filter_reg(filter_val);
adisuciu 0:afcb81077332 84 wait_ms(50);
adisuciu 0:afcb81077332 85 }
adisuciu 0:afcb81077332 86
adisuciu 0:afcb81077332 87 /**
adisuciu 0:afcb81077332 88 * Calibrates the CN0216 weigh scale
adisuciu 0:afcb81077332 89 * @param cal - calibration step.
adisuciu 0:afcb81077332 90 * Step CN0216::ZERO_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the minimum as value for the zero scale
adisuciu 0:afcb81077332 91 * Step CN0216::FULL_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the average as value for the full scale
adisuciu 0:afcb81077332 92 * Step COMPUTE_UNITS_PER_BIT will compute the grams per bit used in weight computation.
adisuciu 0:afcb81077332 93 */
adisuciu 0:afcb81077332 94 void CN0216::calibrate(CalibrationStep_t cal)
adisuciu 0:afcb81077332 95 {
adisuciu 0:afcb81077332 96 uint64_t sum = 0;
adisuciu 0:afcb81077332 97 uint32_t min = 0xFFFFFFFF;
adisuciu 0:afcb81077332 98 uint32_t sample = 0;
adisuciu 0:afcb81077332 99 switch(cal) {
adisuciu 0:afcb81077332 100 case ZERO_SCALE_CALIBRATION:
adisuciu 0:afcb81077332 101 case FULL_SCALE_CALIBRATION:
adisuciu 0:afcb81077332 102 for(int i = 0; i < _NUMBER_OF_SAMPLES; i++) {
adisuciu 0:afcb81077332 103 sample = ad7791.read_u32();
adisuciu 0:afcb81077332 104 min = (min < sample) ? min : sample;
adisuciu 0:afcb81077332 105 sum += ad7791.read_u32();
adisuciu 0:afcb81077332 106 wait_us(5);
adisuciu 0:afcb81077332 107 }
adisuciu 0:afcb81077332 108 if(cal == ZERO_SCALE_CALIBRATION) {
adisuciu 0:afcb81077332 109 // pc.printf("ZERO SCALE VALUE = %x",sum);
adisuciu 0:afcb81077332 110 _zero_scale_value = min;
adisuciu 0:afcb81077332 111 } else {
adisuciu 0:afcb81077332 112 // pc.printf("FULL SCALE VALUE = %x",sum);
adisuciu 0:afcb81077332 113 sum = sum / _NUMBER_OF_SAMPLES;
adisuciu 0:afcb81077332 114 _full_scale_value = sum;
adisuciu 0:afcb81077332 115 }
adisuciu 0:afcb81077332 116 break;
adisuciu 0:afcb81077332 117
adisuciu 0:afcb81077332 118 case COMPUTE_UNITS_PER_BIT:
adisuciu 0:afcb81077332 119 _weight_units_per_bit = _cal_weight / (static_cast<float> (_full_scale_value - _zero_scale_value)); /* Calculate number of grams per LSB */
adisuciu 0:afcb81077332 120 // pc.printf("GRAMS/LSB = %f", _grams_per_bit);
adisuciu 0:afcb81077332 121 break;
adisuciu 0:afcb81077332 122 default:
adisuciu 0:afcb81077332 123 break;
adisuciu 0:afcb81077332 124 }
adisuciu 0:afcb81077332 125
adisuciu 0:afcb81077332 126 }
adisuciu 0:afcb81077332 127
adisuciu 0:afcb81077332 128 /**
adisuciu 0:afcb81077332 129 * Computes the weight based on the formula
adisuciu 0:afcb81077332 130 * weight = (data - zeroscale) * weight_units_per_bit
adisuciu 0:afcb81077332 131 * @param data read from the ADC
adisuciu 0:afcb81077332 132 * @return weight based on data
adisuciu 0:afcb81077332 133 */
adisuciu 0:afcb81077332 134 float CN0216::compute_weight(uint32_t data)
adisuciu 0:afcb81077332 135 {
adisuciu 0:afcb81077332 136 // 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);
adisuciu 0:afcb81077332 137 if(data < _zero_scale_value)
adisuciu 0:afcb81077332 138 data = _zero_scale_value; // clamp data to 0
adisuciu 0:afcb81077332 139 float weight_in_grams = (static_cast<float>((data) - _zero_scale_value)) * _weight_units_per_bit; /* Calculate weight */
adisuciu 0:afcb81077332 140 return weight_in_grams;
adisuciu 0:afcb81077332 141 }
adisuciu 0:afcb81077332 142
adisuciu 0:afcb81077332 143 /**
adisuciu 0:afcb81077332 144 * Reads the AD7791
adisuciu 0:afcb81077332 145 * @return value read by the ADC
adisuciu 0:afcb81077332 146 */
adisuciu 0:afcb81077332 147 uint32_t CN0216::read_u32()
adisuciu 0:afcb81077332 148 {
adisuciu 0:afcb81077332 149 return ad7791.read_u32();
adisuciu 0:afcb81077332 150 }
adisuciu 0:afcb81077332 151
adisuciu 0:afcb81077332 152 /**
adisuciu 0:afcb81077332 153 * Reads the ADC and computes the weight based on the formula described above.
adisuciu 0:afcb81077332 154 * @return weight
adisuciu 0:afcb81077332 155 */
adisuciu 0:afcb81077332 156 float CN0216::read_weight()
adisuciu 0:afcb81077332 157 {
adisuciu 0:afcb81077332 158 uint32_t weight = read_u32();
adisuciu 0:afcb81077332 159 return compute_weight(weight);
adisuciu 0:afcb81077332 160 }
adisuciu 0:afcb81077332 161