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