test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Fri Oct 28 13:04:10 2016 +0000
Revision:
1:b0d4fbbdb244
Parent:
0:1ad0e04b1bc5
ghhbfdd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
youkee 0:1ad0e04b1bc5 1 /*******************************************************************************
youkee 0:1ad0e04b1bc5 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
youkee 0:1ad0e04b1bc5 3 *
youkee 0:1ad0e04b1bc5 4 * Permission is hereby granted, free of charge, to any person obtaining a
youkee 0:1ad0e04b1bc5 5 * copy of this software and associated documentation files (the "Software"),
youkee 0:1ad0e04b1bc5 6 * to deal in the Software without restriction, including without limitation
youkee 0:1ad0e04b1bc5 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
youkee 0:1ad0e04b1bc5 8 * and/or sell copies of the Software, and to permit persons to whom the
youkee 0:1ad0e04b1bc5 9 * Software is furnished to do so, subject to the following conditions:
youkee 0:1ad0e04b1bc5 10 *
youkee 0:1ad0e04b1bc5 11 * The above copyright notice and this permission notice shall be included
youkee 0:1ad0e04b1bc5 12 * in all copies or substantial portions of the Software.
youkee 0:1ad0e04b1bc5 13 *
youkee 0:1ad0e04b1bc5 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
youkee 0:1ad0e04b1bc5 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
youkee 0:1ad0e04b1bc5 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
youkee 0:1ad0e04b1bc5 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
youkee 0:1ad0e04b1bc5 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
youkee 0:1ad0e04b1bc5 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
youkee 0:1ad0e04b1bc5 20 * OTHER DEALINGS IN THE SOFTWARE.
youkee 0:1ad0e04b1bc5 21 *
youkee 0:1ad0e04b1bc5 22 * Except as contained in this notice, the name of Maxim Integrated
youkee 0:1ad0e04b1bc5 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
youkee 0:1ad0e04b1bc5 24 * Products, Inc. Branding Policy.
youkee 0:1ad0e04b1bc5 25 *
youkee 0:1ad0e04b1bc5 26 * The mere transfer of this software does not imply any licenses
youkee 0:1ad0e04b1bc5 27 * of trade secrets, proprietary technology, copyrights, patents,
youkee 0:1ad0e04b1bc5 28 * trademarks, maskwork rights, or any other form of intellectual
youkee 0:1ad0e04b1bc5 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
youkee 0:1ad0e04b1bc5 30 * ownership rights.
youkee 0:1ad0e04b1bc5 31 *******************************************************************************
youkee 0:1ad0e04b1bc5 32 */
youkee 0:1ad0e04b1bc5 33
youkee 0:1ad0e04b1bc5 34 #ifndef _BMP180_H_
youkee 0:1ad0e04b1bc5 35 #define _BMP180_H_
youkee 0:1ad0e04b1bc5 36
youkee 0:1ad0e04b1bc5 37 #include "mbed.h"
youkee 0:1ad0e04b1bc5 38
youkee 0:1ad0e04b1bc5 39 /**
youkee 0:1ad0e04b1bc5 40 * Bosch BMP180 Digital Pressure Sensor
youkee 0:1ad0e04b1bc5 41 *
youkee 0:1ad0e04b1bc5 42 * @code
youkee 0:1ad0e04b1bc5 43 * #include <stdio.h>
youkee 0:1ad0e04b1bc5 44 * #include "mbed.h"
youkee 0:1ad0e04b1bc5 45 * #include "BMP180.h"
youkee 0:1ad0e04b1bc5 46 *
youkee 0:1ad0e04b1bc5 47 * I2C i2c(I2C_SDA, I2C_SCL);
youkee 0:1ad0e04b1bc5 48 * BMP180 bmp180(&i2c);
youkee 0:1ad0e04b1bc5 49 *
youkee 0:1ad0e04b1bc5 50 * int main(void) {
youkee 0:1ad0e04b1bc5 51 *
youkee 0:1ad0e04b1bc5 52 * while(1) {
youkee 0:1ad0e04b1bc5 53 * if (bmp180.init() != 0) {
youkee 0:1ad0e04b1bc5 54 * printf("Error communicating with BMP180\n");
youkee 0:1ad0e04b1bc5 55 * } else {
youkee 0:1ad0e04b1bc5 56 * printf("Initialized BMP180\n");
youkee 0:1ad0e04b1bc5 57 * break;
youkee 0:1ad0e04b1bc5 58 * }
youkee 0:1ad0e04b1bc5 59 * wait(1);
youkee 0:1ad0e04b1bc5 60 * }
youkee 0:1ad0e04b1bc5 61 *
youkee 0:1ad0e04b1bc5 62 * while(1) {
youkee 0:1ad0e04b1bc5 63 * bmp180.startTemperature();
youkee 0:1ad0e04b1bc5 64 * wait_ms(5); // Wait for conversion to complete
youkee 0:1ad0e04b1bc5 65 * float temp;
youkee 0:1ad0e04b1bc5 66 * if(bmp180.getTemperature(&temp) != 0) {
youkee 0:1ad0e04b1bc5 67 * printf("Error getting temperature\n");
youkee 0:1ad0e04b1bc5 68 * continue;
youkee 0:1ad0e04b1bc5 69 * }
youkee 0:1ad0e04b1bc5 70 *
youkee 0:1ad0e04b1bc5 71 * bmp180.startPressure(BMP180::ULTRA_LOW_POWER);
youkee 0:1ad0e04b1bc5 72 * wait_ms(10); // Wait for conversion to complete
youkee 0:1ad0e04b1bc5 73 * int pressure;
youkee 0:1ad0e04b1bc5 74 * if(bmp180.getPressure(&pressure) != 0) {
youkee 0:1ad0e04b1bc5 75 * printf("Error getting pressure\n");
youkee 0:1ad0e04b1bc5 76 * continue;
youkee 0:1ad0e04b1bc5 77 * }
youkee 0:1ad0e04b1bc5 78 *
youkee 0:1ad0e04b1bc5 79 * printf("Pressure = %d Pa Temperature = %f C\n", pressure, temp);
youkee 0:1ad0e04b1bc5 80 * wait(1);
youkee 0:1ad0e04b1bc5 81 * }
youkee 0:1ad0e04b1bc5 82 * }
youkee 0:1ad0e04b1bc5 83 * @endcode
youkee 0:1ad0e04b1bc5 84 */
youkee 0:1ad0e04b1bc5 85 class BMP180
youkee 0:1ad0e04b1bc5 86 {
youkee 0:1ad0e04b1bc5 87
youkee 0:1ad0e04b1bc5 88 public:
youkee 0:1ad0e04b1bc5 89
youkee 0:1ad0e04b1bc5 90 /**
youkee 0:1ad0e04b1bc5 91 * @brief Oversampling ratio.
youkee 0:1ad0e04b1bc5 92 * @details Dictates how many pressure samples to take. Conversion time varies
youkee 0:1ad0e04b1bc5 93 * depending on the number of samples taken. Refer to data sheet
youkee 0:1ad0e04b1bc5 94 * for timing specifications.
youkee 0:1ad0e04b1bc5 95 */
youkee 0:1ad0e04b1bc5 96 typedef enum {
youkee 0:1ad0e04b1bc5 97 ULTRA_LOW_POWER = 0, ///< 1 pressure sample
youkee 0:1ad0e04b1bc5 98 STANDARD = 1, ///< 2 pressure samples
youkee 0:1ad0e04b1bc5 99 HIGH_RESOLUTION = 2, ///< 4 pressure samples
youkee 0:1ad0e04b1bc5 100 ULTRA_HIGH_RESOLUTION = 3, ///< 8 pressure samples
youkee 0:1ad0e04b1bc5 101 } oversampling_t;
youkee 0:1ad0e04b1bc5 102
youkee 0:1ad0e04b1bc5 103 /**
youkee 0:1ad0e04b1bc5 104 * BMP180 constructor.
youkee 0:1ad0e04b1bc5 105 *
youkee 0:1ad0e04b1bc5 106 * @param sda mbed pin to use for SDA line of I2C interface.
youkee 0:1ad0e04b1bc5 107 * @param scl mbed pin to use for SCL line of I2C interface.
youkee 0:1ad0e04b1bc5 108 */
youkee 0:1ad0e04b1bc5 109 BMP180(PinName sda, PinName scl);
youkee 0:1ad0e04b1bc5 110
youkee 0:1ad0e04b1bc5 111 /**
youkee 0:1ad0e04b1bc5 112 * BMP180 constructor.
youkee 0:1ad0e04b1bc5 113 *
youkee 0:1ad0e04b1bc5 114 * @param i2c I2C object to use.
youkee 0:1ad0e04b1bc5 115 */
youkee 0:1ad0e04b1bc5 116 BMP180(I2C *i2c);
youkee 0:1ad0e04b1bc5 117
youkee 0:1ad0e04b1bc5 118 /**
youkee 0:1ad0e04b1bc5 119 * BMP180 destructor.
youkee 0:1ad0e04b1bc5 120 */
youkee 0:1ad0e04b1bc5 121 ~BMP180();
youkee 0:1ad0e04b1bc5 122
youkee 0:1ad0e04b1bc5 123 /**
youkee 0:1ad0e04b1bc5 124 * @brief Initialize BMP180.
youkee 0:1ad0e04b1bc5 125 * @details Gets the device ID and saves the calibration values.
youkee 0:1ad0e04b1bc5 126 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 127 */
youkee 0:1ad0e04b1bc5 128 int init(void);
youkee 0:1ad0e04b1bc5 129
youkee 0:1ad0e04b1bc5 130 /**
youkee 0:1ad0e04b1bc5 131 * @brief Reset BMP180.
youkee 0:1ad0e04b1bc5 132 * @details Performs a soft reset of the device. Same sequence as power on reset.
youkee 0:1ad0e04b1bc5 133 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 134 */
youkee 0:1ad0e04b1bc5 135 int reset(void);
youkee 0:1ad0e04b1bc5 136
youkee 0:1ad0e04b1bc5 137 /**
youkee 0:1ad0e04b1bc5 138 * @brief Check ID.
youkee 0:1ad0e04b1bc5 139 * @details Checks the device ID, should be 0x55 on reset.
youkee 0:1ad0e04b1bc5 140 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 141 */
youkee 0:1ad0e04b1bc5 142 int checkId(void);
youkee 0:1ad0e04b1bc5 143
youkee 0:1ad0e04b1bc5 144 /**
youkee 0:1ad0e04b1bc5 145 * @brief Start pressure conversion.
youkee 0:1ad0e04b1bc5 146 * @details Initiates the pressure conversion sequence. Refer to data sheet
youkee 0:1ad0e04b1bc5 147 * for timing specifications.
youkee 0:1ad0e04b1bc5 148 *
youkee 0:1ad0e04b1bc5 149 * @param oss Number of samples to take.
youkee 0:1ad0e04b1bc5 150 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 151 */
youkee 0:1ad0e04b1bc5 152 int startPressure(BMP180::oversampling_t oss);
youkee 0:1ad0e04b1bc5 153
youkee 0:1ad0e04b1bc5 154 /**
youkee 0:1ad0e04b1bc5 155 * @brief Get pressure reading.
youkee 0:1ad0e04b1bc5 156 * @details Calculates the pressure using the data calibration data and formula.
youkee 0:1ad0e04b1bc5 157 * Pressure is reported in Pascals.
youkee 0:1ad0e04b1bc5 158 * @note This function should be called after calling startPressure().
youkee 0:1ad0e04b1bc5 159 * Refer to the data sheet for the timing requirements. Calling this
youkee 0:1ad0e04b1bc5 160 * function too soon can result in oversampling.
youkee 0:1ad0e04b1bc5 161 *
youkee 0:1ad0e04b1bc5 162 * @param pressure Pointer to store pressure reading.
youkee 0:1ad0e04b1bc5 163 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 164 */
youkee 0:1ad0e04b1bc5 165 int getPressure(int *pressure);
youkee 0:1ad0e04b1bc5 166
youkee 0:1ad0e04b1bc5 167 /**
youkee 0:1ad0e04b1bc5 168 * @brief Start temperature conversion.
youkee 0:1ad0e04b1bc5 169 * @details Initiates the temperature conversion sequence. Refer to data
youkee 0:1ad0e04b1bc5 170 * sheet for timing specifications.
youkee 0:1ad0e04b1bc5 171 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 172 */
youkee 0:1ad0e04b1bc5 173 int startTemperature(void);
youkee 0:1ad0e04b1bc5 174
youkee 0:1ad0e04b1bc5 175 /**
youkee 0:1ad0e04b1bc5 176 * @brief Get temperature reading.
youkee 0:1ad0e04b1bc5 177 * @details Calculates the temperature using the data calibration data and formula.
youkee 0:1ad0e04b1bc5 178 * Temperature is reported in degrees Celcius.
youkee 0:1ad0e04b1bc5 179 *
youkee 0:1ad0e04b1bc5 180 * @note This function should be called after calling startTemperature().
youkee 0:1ad0e04b1bc5 181 * Refer to the data sheet for the timing requirements. Calling this
youkee 0:1ad0e04b1bc5 182 * function too soon can result in oversampling.
youkee 0:1ad0e04b1bc5 183 *
youkee 0:1ad0e04b1bc5 184 * @param tempC Pointer to store temperature reading.
youkee 0:1ad0e04b1bc5 185 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 186 */
youkee 0:1ad0e04b1bc5 187 int getTemperature(float *tempC);
youkee 0:1ad0e04b1bc5 188
youkee 0:1ad0e04b1bc5 189 /**
youkee 0:1ad0e04b1bc5 190 * @brief Get temperature reading.
youkee 0:1ad0e04b1bc5 191 * @details Calculates the temperature using the data calibration data and formula.
youkee 0:1ad0e04b1bc5 192 * Temperature is reported in 1/10ths degrees Celcius.
youkee 0:1ad0e04b1bc5 193 *
youkee 0:1ad0e04b1bc5 194 * @note This function should be called after calling startTemperature().
youkee 0:1ad0e04b1bc5 195 * Refer to the data sheet for the timing requirements. Calling this
youkee 0:1ad0e04b1bc5 196 * function too soon can result in oversampling.
youkee 0:1ad0e04b1bc5 197 *
youkee 0:1ad0e04b1bc5 198 * @param tempCx10 Pointer to store temperature reading.
youkee 0:1ad0e04b1bc5 199 * @returns 0 if no errors, -1 if error.
youkee 0:1ad0e04b1bc5 200 */
youkee 0:1ad0e04b1bc5 201 int getTemperature(int16_t *tempCx10);
youkee 0:1ad0e04b1bc5 202
youkee 0:1ad0e04b1bc5 203 private:
youkee 0:1ad0e04b1bc5 204
youkee 0:1ad0e04b1bc5 205 typedef union {
youkee 0:1ad0e04b1bc5 206 uint16_t value[11];
youkee 0:1ad0e04b1bc5 207 struct {
youkee 0:1ad0e04b1bc5 208 int16_t ac1;
youkee 0:1ad0e04b1bc5 209 int16_t ac2;
youkee 0:1ad0e04b1bc5 210 int16_t ac3;
youkee 0:1ad0e04b1bc5 211 uint16_t ac4;
youkee 0:1ad0e04b1bc5 212 uint16_t ac5;
youkee 0:1ad0e04b1bc5 213 uint16_t ac6;
youkee 0:1ad0e04b1bc5 214 int16_t b1;
youkee 0:1ad0e04b1bc5 215 int16_t b2;
youkee 0:1ad0e04b1bc5 216 int16_t mb;
youkee 0:1ad0e04b1bc5 217 int16_t mc;
youkee 0:1ad0e04b1bc5 218 int16_t md;
youkee 0:1ad0e04b1bc5 219 };
youkee 0:1ad0e04b1bc5 220 } calibration_t;
youkee 0:1ad0e04b1bc5 221
youkee 0:1ad0e04b1bc5 222 I2C *i2c_;
youkee 0:1ad0e04b1bc5 223 bool i2c_owner;
youkee 0:1ad0e04b1bc5 224
youkee 0:1ad0e04b1bc5 225 BMP180::calibration_t calib;
youkee 0:1ad0e04b1bc5 226 int32_t b5;
youkee 0:1ad0e04b1bc5 227 BMP180::oversampling_t oss_;
youkee 0:1ad0e04b1bc5 228 };
youkee 0:1ad0e04b1bc5 229
youkee 0:1ad0e04b1bc5 230 #endif /* _BMP180_H_ */