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
- Committer:
- Adrian Suciu
- Date:
- 2016-05-18
- Revision:
- 24:dae7123d432a
File content as of revision 24:dae7123d432a:
/**
* @file cn0216.cpp
* @brief Source file for CN0216
* @author Analog Devices Inc.
*
* For support please go to:
* Github: https://github.com/analogdevicesinc/mbed-adi
* Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
* Product: www.analog.com/EVAL-CN0216-ARDZ
* More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
********************************************************************************
* Copyright 2016(c) Analog Devices, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of Analog Devices, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* - The use of this software may or may not infringe the patent rights
* of one or more patent holders. This license does not release you
* from the requirement that you obtain separate licenses from these
* patent holders to use this software.
* - Use of the software either in source or binary form, must be run
* on or directly connected to an Analog Devices Inc. component.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************************/
#include "mbed.h"
#include "AD7791.h"
#include "CN0216.h"
extern Serial pc;
/**
* CN0216 constructor
* @param CSAD7791 - Chipselect of the AD7791
* @param MOSI - MOSI line of the SPI bus
* @param MISO - MISO line of the SPI bus
* @param SCK - SCK line of the SPI bus
*/
CN0216::CN0216(PinName CSAD7791, PinName MOSI, PinName MISO, PinName SCK) : ad7791(1.2, CSAD7791, MOSI, MISO, SCK)
{
_cal_weight = 0;
_zero_scale_value = 0;
_full_scale_value = 0;
_weight_units_per_bit = 0;
}
/**
* Initializes the mode and filter values of the AD7791 and sets the weight to be used in calibration
* @param cal_weight - weight used in calibration
* @param mode_val - value of the mode register
* @param filter_val - value of the filter register
*/
void CN0216::init(float cal_weight, uint8_t mode_val, uint8_t filter_val)
{
_cal_weight = cal_weight;
ad7791.frequency(500000);
wait_ms(50);
ad7791.reset();
wait_ms(50);
ad7791.write_mode_reg(mode_val);
wait_us(2);
ad7791.write_filter_reg(filter_val);
wait_ms(50);
}
/**
* Calibrates the CN0216 weigh scale
* @param cal - calibration step.
* Step CN0216::ZERO_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the minimum as value for the zero scale
* Step CN0216::FULL_SCALE_CALIBRATION will take CN0216::_NUMBER_OF_SAMPLES samples and use the average as value for the full scale
* Step COMPUTE_UNITS_PER_BIT will compute the grams per bit used in weight computation.
*/
void CN0216::calibrate(CalibrationStep_t cal)
{
uint64_t sum = 0;
uint32_t min = 0xFFFFFFFF;
uint32_t sample = 0;
switch(cal) {
case ZERO_SCALE_CALIBRATION:
case FULL_SCALE_CALIBRATION:
for(int i = 0; i < _NUMBER_OF_SAMPLES; i++) {
sample = ad7791.read_u32();
min = (min < sample) ? min : sample;
sum += ad7791.read_u32();
wait_us(5);
}
if(cal == ZERO_SCALE_CALIBRATION) {
// pc.printf("ZERO SCALE VALUE = %x",sum);
_zero_scale_value = min;
} else {
// pc.printf("FULL SCALE VALUE = %x",sum);
sum = sum / _NUMBER_OF_SAMPLES;
_full_scale_value = sum;
}
break;
case COMPUTE_UNITS_PER_BIT:
_weight_units_per_bit = _cal_weight / (static_cast<float> (_full_scale_value - _zero_scale_value)); /* Calculate number of grams per LSB */
// pc.printf("GRAMS/LSB = %f", _grams_per_bit);
break;
default:
break;
}
}
/**
* Computes the weight based on the formula
* weight = (data - zeroscale) * weight_units_per_bit
* @param data read from the ADC
* @return weight based on data
*/
float CN0216::compute_weight(uint32_t data)
{
// 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);
if(data < _zero_scale_value)
data = _zero_scale_value; // clamp data to 0
float weight_in_grams = (static_cast<float>((data) - _zero_scale_value)) * _weight_units_per_bit; /* Calculate weight */
return weight_in_grams;
}
/**
* Reads the AD7791
* @return value read by the ADC
*/
uint32_t CN0216::read_u32()
{
return ad7791.read_u32();
}
/**
* Reads the ADC and computes the weight based on the formula described above.
* @return weight
*/
float CN0216::read_weight()
{
uint32_t weight = read_u32();
return compute_weight(weight);
}
CN0357 - Toxic gas measurement
CN0216 - Weight Scale