A collection of Analog Devices drivers for the mbed platform

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 *   @file     main.cpp
00003 *   @brief    Main file for the CN0357-example project
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-CN0357-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 #include "mbed.h"
00048 #include "CN0216.h"
00049 
00050 const float CAL_WEIGHT = (500.0);
00051 
00052 
00053 Serial pc(USBTX, USBRX); ///< Serial interface to the pc
00054 
00055 void flush_serial_buffer(void)
00056 {
00057     while (pc.readable()) pc.getc();
00058     return;
00059 }
00060 
00061 void display_data(uint32_t data, float weight, uint64_t timer)
00062 {
00063     pc.printf("\r\nADC Input Voltage input = %x V", data);           /* Send valid voltage input value */
00064     pc.printf("\r\nSensor Input Weight = %f grams", weight);         /* Send valid grams value */
00065 
00066 
00067     /*  pc.printf("%d ",timer);
00068         pc.printf("%x ",data);
00069         pc.printf("%f ",weight);*/
00070     pc.printf("\r\n");
00071 }
00072 
00073 /**
00074  * Project entry-point - initializes the CN0216 shield, reads the data when the ADC is ready and outputs the sensor value
00075  */
00076 
00077 #define SINGLE_CONVERSION
00078 //#define CONTINOUS_CONVERSION
00079 
00080 int main()
00081 {
00082     /* Main variables */
00083     //t.start();
00084     CN0216 cn0216;
00085 #ifdef SINGLE_CONVERSION
00086     cn0216.init(CAL_WEIGHT);
00087 #elif defined CONTINOUS_CONVERSION
00088     cn0216.init(CAL_WEIGHT, 0x08, 0x07);
00089 #else
00090 #error define SINGLE_CONVERSION or CONTINOUS_CONVERSION, but not both
00091 #endif
00092     /* Calibration sequence */
00093 
00094     pc.printf("\r\n Calibrating zero scale. Remove all weights from scale. Press any key to begin ..");
00095     while(!pc.readable());
00096     flush_serial_buffer();
00097     pc.printf("\r\n Calibrating . . . ");
00098     cn0216.calibrate(CN0216::ZERO_SCALE_CALIBRATION);
00099     pc.printf("done ! ");
00100 
00101     pc.printf("\r\n Calibrating full scale. Put calibration weight on scale. Press any key to begin ..");
00102     while(!pc.readable());
00103     flush_serial_buffer();
00104     pc.printf("\r\n Calibrating . . . ");
00105     cn0216.calibrate(CN0216::FULL_SCALE_CALIBRATION);
00106     pc.printf("done ! ");
00107 
00108     pc.printf("\r\n Calibration successful ");
00109     cn0216.calibrate(CN0216::COMPUTE_UNITS_PER_BIT);
00110 
00111     /* Infinite loop */
00112     uint64_t timer = 0;
00113     while (1) {
00114         wait_ms(1000);
00115 
00116         uint32_t data = cn0216.read_u32();
00117         float weight    = cn0216.compute_weight(data); //  Convert ADC data to voltage
00118         display_data(data, weight, timer); //  Display data thru UART
00119 
00120         timer++;
00121     }
00122 
00123 
00124     /* Infinite loop, never returns. */
00125 }
00126