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

Committer:
Adrian Suciu
Date:
Tue May 17 14:21:17 2016 +0300
Revision:
19:fb92949e59c9
Parent:
17:b8356808e8ad
Child:
22:34a97378aab6
Added ADXL362 driver & reference project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Adrian Suciu 13:66c8e4ce4ff1 1 /**
Adrian Suciu 13:66c8e4ce4ff1 2 * @file main.cpp
Adrian Suciu 13:66c8e4ce4ff1 3 * @brief Main file for the CN0357-example project
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-CN0357-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 #include "mbed.h"
Adrian Suciu 13:66c8e4ce4ff1 48 #include "CN0216.h"
Adrian Suciu 13:66c8e4ce4ff1 49
Adrian Suciu 19:fb92949e59c9 50 const float CAL_WEIGHT = (500.0);
Adrian Suciu 19:fb92949e59c9 51
Adrian Suciu 13:66c8e4ce4ff1 52
Adrian Suciu 13:66c8e4ce4ff1 53 Serial pc(USBTX, USBRX); ///< Serial interface to the pc
Adrian Suciu 13:66c8e4ce4ff1 54
Adrian Suciu 13:66c8e4ce4ff1 55 void flush_serial_buffer(void)
Adrian Suciu 13:66c8e4ce4ff1 56 {
Adrian Suciu 17:b8356808e8ad 57 while (pc.readable()) pc.getc();
Adrian Suciu 17:b8356808e8ad 58 return;
Adrian Suciu 13:66c8e4ce4ff1 59 }
Adrian Suciu 13:66c8e4ce4ff1 60
Adrian Suciu 19:fb92949e59c9 61 void display_data(uint32_t data, float weight, uint64_t timer)
Adrian Suciu 13:66c8e4ce4ff1 62 {
Adrian Suciu 19:fb92949e59c9 63 pc.printf("\r\nADC Input Voltage input = %x V", data); /* Send valid voltage input value */
Adrian Suciu 13:66c8e4ce4ff1 64 pc.printf("\r\nSensor Input Weight = %f grams", weight); /* Send valid grams value */
Adrian Suciu 19:fb92949e59c9 65
Adrian Suciu 19:fb92949e59c9 66
Adrian Suciu 19:fb92949e59c9 67 /* pc.printf("%d ",timer);
Adrian Suciu 19:fb92949e59c9 68 pc.printf("%x ",data);
Adrian Suciu 19:fb92949e59c9 69 pc.printf("%f ",weight);*/
Adrian Suciu 13:66c8e4ce4ff1 70 pc.printf("\r\n");
Adrian Suciu 13:66c8e4ce4ff1 71 }
Adrian Suciu 13:66c8e4ce4ff1 72
Adrian Suciu 13:66c8e4ce4ff1 73 /**
Adrian Suciu 13:66c8e4ce4ff1 74 * Project entry-point - initializes the CN0357 shield, reads the data when the ADC is ready and outputs the sensor
Adrian Suciu 13:66c8e4ce4ff1 75 * value in PPM
Adrian Suciu 13:66c8e4ce4ff1 76 */
Adrian Suciu 13:66c8e4ce4ff1 77
Adrian Suciu 19:fb92949e59c9 78 #define SINGLE_CONVERSION
Adrian Suciu 19:fb92949e59c9 79 //#define CONTINOUS_CONVERSION
Adrian Suciu 13:66c8e4ce4ff1 80
Adrian Suciu 13:66c8e4ce4ff1 81 int main()
Adrian Suciu 13:66c8e4ce4ff1 82 {
Adrian Suciu 13:66c8e4ce4ff1 83 /* Main variables */
Adrian Suciu 19:fb92949e59c9 84 //t.start();
Adrian Suciu 17:b8356808e8ad 85 CN0216 cn0216;
Adrian Suciu 13:66c8e4ce4ff1 86 #ifdef SINGLE_CONVERSION
Adrian Suciu 13:66c8e4ce4ff1 87 cn0216.init(CAL_WEIGHT);
Adrian Suciu 13:66c8e4ce4ff1 88 #elif defined CONTINOUS_CONVERSION
Adrian Suciu 19:fb92949e59c9 89 cn0216.init(CAL_WEIGHT, 0x08, 0x07);
Adrian Suciu 13:66c8e4ce4ff1 90 #else
Adrian Suciu 13:66c8e4ce4ff1 91 #error define SINGLE_CONVERSION or CONTINOUS_CONVERSION, but not both
Adrian Suciu 13:66c8e4ce4ff1 92 #endif
Adrian Suciu 13:66c8e4ce4ff1 93 /* Calibration sequence */
Adrian Suciu 17:b8356808e8ad 94
Adrian Suciu 13:66c8e4ce4ff1 95 pc.printf("\r\n Calibrating zero scale. Remove all weights from scale. Press any key to begin ..");
Adrian Suciu 13:66c8e4ce4ff1 96 while(!pc.readable());
Adrian Suciu 13:66c8e4ce4ff1 97 flush_serial_buffer();
Adrian Suciu 13:66c8e4ce4ff1 98 pc.printf("\r\n Calibrating . . . ");
Adrian Suciu 13:66c8e4ce4ff1 99 cn0216.calibrate(CN0216::ZERO_SCALE_CALIBRATION);
Adrian Suciu 13:66c8e4ce4ff1 100 pc.printf("done ! ");
Adrian Suciu 13:66c8e4ce4ff1 101
Adrian Suciu 13:66c8e4ce4ff1 102 pc.printf("\r\n Calibrating full scale. Put calibration weight on scale. Press any key to begin ..");
Adrian Suciu 13:66c8e4ce4ff1 103 while(!pc.readable());
Adrian Suciu 13:66c8e4ce4ff1 104 flush_serial_buffer();
Adrian Suciu 13:66c8e4ce4ff1 105 pc.printf("\r\n Calibrating . . . ");
Adrian Suciu 13:66c8e4ce4ff1 106 cn0216.calibrate(CN0216::FULL_SCALE_CALIBRATION);
Adrian Suciu 13:66c8e4ce4ff1 107 pc.printf("done ! ");
Adrian Suciu 13:66c8e4ce4ff1 108
Adrian Suciu 13:66c8e4ce4ff1 109 pc.printf("\r\n Calibration successful ");
Adrian Suciu 17:b8356808e8ad 110 cn0216.calibrate(CN0216::COMPUTE_UNITS_PER_BIT);
Adrian Suciu 13:66c8e4ce4ff1 111
Adrian Suciu 13:66c8e4ce4ff1 112 /* Infinite loop */
Adrian Suciu 19:fb92949e59c9 113 uint64_t timer = 0;
Adrian Suciu 13:66c8e4ce4ff1 114 while (1) {
Adrian Suciu 13:66c8e4ce4ff1 115 wait_ms(1000);
Adrian Suciu 19:fb92949e59c9 116
Adrian Suciu 19:fb92949e59c9 117 uint32_t data = cn0216.read_u32();
Adrian Suciu 19:fb92949e59c9 118 float weight = cn0216.compute_weight(data); // Convert ADC data to voltage
Adrian Suciu 19:fb92949e59c9 119 display_data(data, weight, timer); // Display data thru UART
Adrian Suciu 19:fb92949e59c9 120
Adrian Suciu 19:fb92949e59c9 121 timer++;
Adrian Suciu 13:66c8e4ce4ff1 122 }
Adrian Suciu 13:66c8e4ce4ff1 123
Adrian Suciu 13:66c8e4ce4ff1 124
Adrian Suciu 13:66c8e4ce4ff1 125 /* Infinite loop, never returns. */
Adrian Suciu 13:66c8e4ce4ff1 126 }
Adrian Suciu 13:66c8e4ce4ff1 127