CN0216 weight scale sensor demonstration program

Dependencies:   CN0216 mbed

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 May 19 08:55:47 2016 +0000
Revision:
0:af432e8b6c72
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adisuciu 0:af432e8b6c72 1 /**
adisuciu 0:af432e8b6c72 2 * @file main.cpp
adisuciu 0:af432e8b6c72 3 * @brief Main file for the CN0357-example project
adisuciu 0:af432e8b6c72 4 * @author Analog Devices Inc.
adisuciu 0:af432e8b6c72 5 *
adisuciu 0:af432e8b6c72 6 * For support please go to:
adisuciu 0:af432e8b6c72 7 * Github: https://github.com/analogdevicesinc/mbed-adi
adisuciu 0:af432e8b6c72 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
adisuciu 0:af432e8b6c72 9 * Product: www.analog.com/EVAL-CN0357-ARDZ
adisuciu 0:af432e8b6c72 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
adisuciu 0:af432e8b6c72 11
adisuciu 0:af432e8b6c72 12 ********************************************************************************
adisuciu 0:af432e8b6c72 13 * Copyright 2016(c) Analog Devices, Inc.
adisuciu 0:af432e8b6c72 14 *
adisuciu 0:af432e8b6c72 15 * All rights reserved.
adisuciu 0:af432e8b6c72 16 *
adisuciu 0:af432e8b6c72 17 * Redistribution and use in source and binary forms, with or without
adisuciu 0:af432e8b6c72 18 * modification, are permitted provided that the following conditions are met:
adisuciu 0:af432e8b6c72 19 * - Redistributions of source code must retain the above copyright
adisuciu 0:af432e8b6c72 20 * notice, this list of conditions and the following disclaimer.
adisuciu 0:af432e8b6c72 21 * - Redistributions in binary form must reproduce the above copyright
adisuciu 0:af432e8b6c72 22 * notice, this list of conditions and the following disclaimer in
adisuciu 0:af432e8b6c72 23 * the documentation and/or other materials provided with the
adisuciu 0:af432e8b6c72 24 * distribution.
adisuciu 0:af432e8b6c72 25 * - Neither the name of Analog Devices, Inc. nor the names of its
adisuciu 0:af432e8b6c72 26 * contributors may be used to endorse or promote products derived
adisuciu 0:af432e8b6c72 27 * from this software without specific prior written permission.
adisuciu 0:af432e8b6c72 28 * - The use of this software may or may not infringe the patent rights
adisuciu 0:af432e8b6c72 29 * of one or more patent holders. This license does not release you
adisuciu 0:af432e8b6c72 30 * from the requirement that you obtain separate licenses from these
adisuciu 0:af432e8b6c72 31 * patent holders to use this software.
adisuciu 0:af432e8b6c72 32 * - Use of the software either in source or binary form, must be run
adisuciu 0:af432e8b6c72 33 * on or directly connected to an Analog Devices Inc. component.
adisuciu 0:af432e8b6c72 34 *
adisuciu 0:af432e8b6c72 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
adisuciu 0:af432e8b6c72 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
adisuciu 0:af432e8b6c72 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
adisuciu 0:af432e8b6c72 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
adisuciu 0:af432e8b6c72 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
adisuciu 0:af432e8b6c72 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
adisuciu 0:af432e8b6c72 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
adisuciu 0:af432e8b6c72 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
adisuciu 0:af432e8b6c72 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
adisuciu 0:af432e8b6c72 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adisuciu 0:af432e8b6c72 45 *
adisuciu 0:af432e8b6c72 46 ********************************************************************************/
adisuciu 0:af432e8b6c72 47 #include "mbed.h"
adisuciu 0:af432e8b6c72 48 #include "CN0216.h"
adisuciu 0:af432e8b6c72 49
adisuciu 0:af432e8b6c72 50 const float CAL_WEIGHT = (500.0);
adisuciu 0:af432e8b6c72 51
adisuciu 0:af432e8b6c72 52
adisuciu 0:af432e8b6c72 53 Serial pc(USBTX, USBRX); ///< Serial interface to the pc
adisuciu 0:af432e8b6c72 54
adisuciu 0:af432e8b6c72 55 void flush_serial_buffer(void)
adisuciu 0:af432e8b6c72 56 {
adisuciu 0:af432e8b6c72 57 while (pc.readable()) pc.getc();
adisuciu 0:af432e8b6c72 58 return;
adisuciu 0:af432e8b6c72 59 }
adisuciu 0:af432e8b6c72 60
adisuciu 0:af432e8b6c72 61 void display_data(uint32_t data, float weight, uint64_t timer)
adisuciu 0:af432e8b6c72 62 {
adisuciu 0:af432e8b6c72 63 pc.printf("\r\nADC Input Voltage input = %x V", data); /* Send valid voltage input value */
adisuciu 0:af432e8b6c72 64 pc.printf("\r\nSensor Input Weight = %f grams", weight); /* Send valid grams value */
adisuciu 0:af432e8b6c72 65
adisuciu 0:af432e8b6c72 66
adisuciu 0:af432e8b6c72 67 /* pc.printf("%d ",timer);
adisuciu 0:af432e8b6c72 68 pc.printf("%x ",data);
adisuciu 0:af432e8b6c72 69 pc.printf("%f ",weight);*/
adisuciu 0:af432e8b6c72 70 pc.printf("\r\n");
adisuciu 0:af432e8b6c72 71 }
adisuciu 0:af432e8b6c72 72
adisuciu 0:af432e8b6c72 73 /**
adisuciu 0:af432e8b6c72 74 * Project entry-point - initializes the CN0216 shield, reads the data when the ADC is ready and outputs the sensor value
adisuciu 0:af432e8b6c72 75 */
adisuciu 0:af432e8b6c72 76
adisuciu 0:af432e8b6c72 77 #define SINGLE_CONVERSION
adisuciu 0:af432e8b6c72 78 //#define CONTINOUS_CONVERSION
adisuciu 0:af432e8b6c72 79
adisuciu 0:af432e8b6c72 80 int main()
adisuciu 0:af432e8b6c72 81 {
adisuciu 0:af432e8b6c72 82 /* Main variables */
adisuciu 0:af432e8b6c72 83 //t.start();
adisuciu 0:af432e8b6c72 84 CN0216 cn0216;
adisuciu 0:af432e8b6c72 85 #ifdef SINGLE_CONVERSION
adisuciu 0:af432e8b6c72 86 cn0216.init(CAL_WEIGHT);
adisuciu 0:af432e8b6c72 87 #elif defined CONTINOUS_CONVERSION
adisuciu 0:af432e8b6c72 88 cn0216.init(CAL_WEIGHT, 0x08, 0x07);
adisuciu 0:af432e8b6c72 89 #else
adisuciu 0:af432e8b6c72 90 #error define SINGLE_CONVERSION or CONTINOUS_CONVERSION, but not both
adisuciu 0:af432e8b6c72 91 #endif
adisuciu 0:af432e8b6c72 92 /* Calibration sequence */
adisuciu 0:af432e8b6c72 93
adisuciu 0:af432e8b6c72 94 pc.printf("\r\n Calibrating zero scale. Remove all weights from scale. Press any key to begin ..");
adisuciu 0:af432e8b6c72 95 while(!pc.readable());
adisuciu 0:af432e8b6c72 96 flush_serial_buffer();
adisuciu 0:af432e8b6c72 97 pc.printf("\r\n Calibrating . . . ");
adisuciu 0:af432e8b6c72 98 cn0216.calibrate(CN0216::ZERO_SCALE_CALIBRATION);
adisuciu 0:af432e8b6c72 99 pc.printf("done ! ");
adisuciu 0:af432e8b6c72 100
adisuciu 0:af432e8b6c72 101 pc.printf("\r\n Calibrating full scale. Put calibration weight on scale. Press any key to begin ..");
adisuciu 0:af432e8b6c72 102 while(!pc.readable());
adisuciu 0:af432e8b6c72 103 flush_serial_buffer();
adisuciu 0:af432e8b6c72 104 pc.printf("\r\n Calibrating . . . ");
adisuciu 0:af432e8b6c72 105 cn0216.calibrate(CN0216::FULL_SCALE_CALIBRATION);
adisuciu 0:af432e8b6c72 106 pc.printf("done ! ");
adisuciu 0:af432e8b6c72 107
adisuciu 0:af432e8b6c72 108 pc.printf("\r\n Calibration successful ");
adisuciu 0:af432e8b6c72 109 cn0216.calibrate(CN0216::COMPUTE_UNITS_PER_BIT);
adisuciu 0:af432e8b6c72 110
adisuciu 0:af432e8b6c72 111 /* Infinite loop */
adisuciu 0:af432e8b6c72 112 uint64_t timer = 0;
adisuciu 0:af432e8b6c72 113 while (1) {
adisuciu 0:af432e8b6c72 114 wait_ms(1000);
adisuciu 0:af432e8b6c72 115
adisuciu 0:af432e8b6c72 116 uint32_t data = cn0216.read_u32();
adisuciu 0:af432e8b6c72 117 float weight = cn0216.compute_weight(data); // Convert ADC data to voltage
adisuciu 0:af432e8b6c72 118 display_data(data, weight, timer); // Display data thru UART
adisuciu 0:af432e8b6c72 119
adisuciu 0:af432e8b6c72 120 timer++;
adisuciu 0:af432e8b6c72 121 }
adisuciu 0:af432e8b6c72 122
adisuciu 0:af432e8b6c72 123
adisuciu 0:af432e8b6c72 124 /* Infinite loop, never returns. */
adisuciu 0:af432e8b6c72 125 }
adisuciu 0:af432e8b6c72 126