Library for the use of the atmospheric pressure sensor SCP1000

Committer:
s_inoue_mbed
Date:
Thu Sep 18 13:09:39 2014 +0000
Revision:
1:1b2027cbe629
Parent:
0:a224293d7af4
Bug fix: below-zero temperature is sufficiently treated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s_inoue_mbed 0:a224293d7af4 1 /* Copyright (c) 2014 Shigenori Inoue, MIT License
s_inoue_mbed 0:a224293d7af4 2 *
s_inoue_mbed 0:a224293d7af4 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
s_inoue_mbed 0:a224293d7af4 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
s_inoue_mbed 0:a224293d7af4 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
s_inoue_mbed 0:a224293d7af4 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
s_inoue_mbed 0:a224293d7af4 7 * furnished to do so, subject to the following conditions:
s_inoue_mbed 0:a224293d7af4 8 *
s_inoue_mbed 0:a224293d7af4 9 * The above copyright notice and this permission notice shall be included in all copies or
s_inoue_mbed 0:a224293d7af4 10 * substantial portions of the Software.
s_inoue_mbed 0:a224293d7af4 11 *
s_inoue_mbed 0:a224293d7af4 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
s_inoue_mbed 0:a224293d7af4 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
s_inoue_mbed 0:a224293d7af4 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
s_inoue_mbed 0:a224293d7af4 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
s_inoue_mbed 0:a224293d7af4 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
s_inoue_mbed 0:a224293d7af4 17 */
s_inoue_mbed 0:a224293d7af4 18
s_inoue_mbed 0:a224293d7af4 19 #ifndef __SCP1000__
s_inoue_mbed 0:a224293d7af4 20 #define __SCP1000__
s_inoue_mbed 0:a224293d7af4 21 #include "mbed.h"
s_inoue_mbed 0:a224293d7af4 22
s_inoue_mbed 0:a224293d7af4 23 /** Example:
s_inoue_mbed 0:a224293d7af4 24 * @code
s_inoue_mbed 0:a224293d7af4 25 * #include "mbed.h"
s_inoue_mbed 0:a224293d7af4 26 * #include "SCP1000.h"
s_inoue_mbed 0:a224293d7af4 27 *
s_inoue_mbed 0:a224293d7af4 28 * SCP1000 scp(p5, p6, p7, p18, p21, p19);
s_inoue_mbed 0:a224293d7af4 29 *
s_inoue_mbed 0:a224293d7af4 30 * int main()
s_inoue_mbed 0:a224293d7af4 31 * {
s_inoue_mbed 0:a224293d7af4 32 * // Variables
s_inoue_mbed 0:a224293d7af4 33 * float temp, press;
s_inoue_mbed 0:a224293d7af4 34 *
s_inoue_mbed 0:a224293d7af4 35 * // SCP1000 Initialization
s_inoue_mbed 0:a224293d7af4 36 * err = scp.init();
s_inoue_mbed 0:a224293d7af4 37 * if (err == SCP1000::ERR) {
s_inoue_mbed 0:a224293d7af4 38 * printf("Error!");
s_inoue_mbed 0:a224293d7af4 39 * return 1;
s_inoue_mbed 0:a224293d7af4 40 * }
s_inoue_mbed 0:a224293d7af4 41 *
s_inoue_mbed 0:a224293d7af4 42 * while(true) {
s_inoue_mbed 0:a224293d7af4 43 * do {} while (scp.IsReady() == false);
s_inoue_mbed 0:a224293d7af4 44 * temp = scp.readTemperature();
s_inoue_mbed 0:a224293d7af4 45 * press = scp.readPressure();
s_inoue_mbed 0:a224293d7af4 46 * printf("Temperature: %2.2f degC\r\n", temp);
s_inoue_mbed 0:a224293d7af4 47 * printf("Atmospheric Pressure: %2.2f hPa", press);
s_inoue_mbed 0:a224293d7af4 48 * }
s_inoue_mbed 0:a224293d7af4 49 * }
s_inoue_mbed 0:a224293d7af4 50 * @endcode
s_inoue_mbed 0:a224293d7af4 51 */
s_inoue_mbed 0:a224293d7af4 52
s_inoue_mbed 0:a224293d7af4 53 class SCP1000
s_inoue_mbed 0:a224293d7af4 54 {
s_inoue_mbed 0:a224293d7af4 55 public:
s_inoue_mbed 0:a224293d7af4 56
s_inoue_mbed 0:a224293d7af4 57 /** Create an SCP1000 interface
s_inoue_mbed 0:a224293d7af4 58 * @param mosi Master-Output-Slave-Input line of SPI
s_inoue_mbed 0:a224293d7af4 59 * @param miso Master-Input-Slave-Output line of SPI
s_inoue_mbed 0:a224293d7af4 60 * @param sclk Serial Clock of SPI
s_inoue_mbed 0:a224293d7af4 61 * @param cs Chip Select signal to SCP1000
s_inoue_mbed 0:a224293d7af4 62 * @param trig Measuremnt Trigger (TRIG) to SCP1000
s_inoue_mbed 0:a224293d7af4 63 * @param drdy Data Ready (DRDY) signal from SCP1000
s_inoue_mbed 0:a224293d7af4 64 */
s_inoue_mbed 0:a224293d7af4 65 SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName trig, PinName drdy);
s_inoue_mbed 0:a224293d7af4 66 ~SCP1000();
s_inoue_mbed 0:a224293d7af4 67
s_inoue_mbed 0:a224293d7af4 68 /** Initialize the SCP1000 */
s_inoue_mbed 0:a224293d7af4 69 int init(void);
s_inoue_mbed 0:a224293d7af4 70
s_inoue_mbed 0:a224293d7af4 71 /** Set the measurement mode of the SCP1000
s_inoue_mbed 0:a224293d7af4 72 * @param mode Measurement mode:
s_inoue_mbed 0:a224293d7af4 73 * 0-HIGH_RESOLUTION,
s_inoue_mbed 0:a224293d7af4 74 * 1-HIGH_SPEED,
s_inoue_mbed 0:a224293d7af4 75 * 2-ULTRA_LOW_POWER,
s_inoue_mbed 0:a224293d7af4 76 * 3-LOW_POWER_17BIT,
s_inoue_mbed 0:a224293d7af4 77 * 4-LOW_POWER_15BIT
s_inoue_mbed 0:a224293d7af4 78 */
s_inoue_mbed 0:a224293d7af4 79 void setMode(int mode);
s_inoue_mbed 0:a224293d7af4 80
s_inoue_mbed 1:1b2027cbe629 81 /** Trigger the measurement by TRIG wire in Low Power Mode
s_inoue_mbed 1:1b2027cbe629 82 * NOTE: IT CURRENTLY SEEMS NOT WORKING.
s_inoue_mbed 1:1b2027cbe629 83 * PLEASE USE triggerSoft() INSTEAD.
s_inoue_mbed 1:1b2027cbe629 84 */
s_inoue_mbed 0:a224293d7af4 85 void triggerHard(void);
s_inoue_mbed 0:a224293d7af4 86
s_inoue_mbed 0:a224293d7af4 87 /** Trigger the measurement by register writing in Low Power Mode */
s_inoue_mbed 0:a224293d7af4 88 void triggerSoft(void);
s_inoue_mbed 0:a224293d7af4 89
s_inoue_mbed 0:a224293d7af4 90 /** Check the data readiness
s_inoue_mbed 0:a224293d7af4 91 * @return DRDY pin status: false-Not Ready, true-Ready
s_inoue_mbed 0:a224293d7af4 92 */
s_inoue_mbed 0:a224293d7af4 93 bool IsReady(void);
s_inoue_mbed 0:a224293d7af4 94
s_inoue_mbed 0:a224293d7af4 95 /** Read temperature measurement result in degrees Celcius
s_inoue_mbed 0:a224293d7af4 96 * @return Temperature in degrees Celcius
s_inoue_mbed 0:a224293d7af4 97 */
s_inoue_mbed 0:a224293d7af4 98 float readTemperature(void);
s_inoue_mbed 0:a224293d7af4 99
s_inoue_mbed 0:a224293d7af4 100 /** Read atmospheric pressure measurement result in hectopascal
s_inoue_mbed 0:a224293d7af4 101 * @return Atmospheric pressure in hPa
s_inoue_mbed 0:a224293d7af4 102 */
s_inoue_mbed 0:a224293d7af4 103 float readPressure(void);
s_inoue_mbed 0:a224293d7af4 104
s_inoue_mbed 0:a224293d7af4 105 enum SCP1000_MODE {
s_inoue_mbed 0:a224293d7af4 106 HIGH_RESOLUTION = 0,
s_inoue_mbed 0:a224293d7af4 107 HIGH_SPEED = 1,
s_inoue_mbed 0:a224293d7af4 108 ULTRA_LOW_POWER = 2,
s_inoue_mbed 0:a224293d7af4 109 LOW_POWER_17BIT = 3,
s_inoue_mbed 0:a224293d7af4 110 LOW_POWER_15BIT = 4
s_inoue_mbed 0:a224293d7af4 111 };
s_inoue_mbed 0:a224293d7af4 112
s_inoue_mbed 0:a224293d7af4 113 enum SCP1000_ERROR {
s_inoue_mbed 0:a224293d7af4 114 OK = 0,
s_inoue_mbed 0:a224293d7af4 115 ERR = 1
s_inoue_mbed 0:a224293d7af4 116 };
s_inoue_mbed 0:a224293d7af4 117
s_inoue_mbed 0:a224293d7af4 118 private:
s_inoue_mbed 0:a224293d7af4 119 SPI _s;
s_inoue_mbed 0:a224293d7af4 120 DigitalOut _cs;
s_inoue_mbed 0:a224293d7af4 121 DigitalOut _trig;
s_inoue_mbed 0:a224293d7af4 122 DigitalIn _drdy;
s_inoue_mbed 0:a224293d7af4 123 void write(int addr, int data);
s_inoue_mbed 0:a224293d7af4 124 int read8(int addr);
s_inoue_mbed 0:a224293d7af4 125 int read16(int addr);
s_inoue_mbed 0:a224293d7af4 126
s_inoue_mbed 0:a224293d7af4 127 enum SCP1000_ADDRESS {
s_inoue_mbed 0:a224293d7af4 128 REVID = 0x00,
s_inoue_mbed 0:a224293d7af4 129 DATAWR = 0x01,
s_inoue_mbed 0:a224293d7af4 130 ADDPTR = 0x02,
s_inoue_mbed 0:a224293d7af4 131 OPERATIONS = 0x03,
s_inoue_mbed 0:a224293d7af4 132 OPSTATUS = 0x04,
s_inoue_mbed 0:a224293d7af4 133 RSTR = 0x06,
s_inoue_mbed 0:a224293d7af4 134 STATUS = 0x07,
s_inoue_mbed 0:a224293d7af4 135 DATARD8 = 0x1F,
s_inoue_mbed 0:a224293d7af4 136 DATARD16 = 0x20,
s_inoue_mbed 0:a224293d7af4 137 TEMPOUT = 0x21,
s_inoue_mbed 0:a224293d7af4 138 CFG = 0x00,
s_inoue_mbed 0:a224293d7af4 139 TWIADD = 0x05,
s_inoue_mbed 0:a224293d7af4 140 USERDATA1 = 0x29,
s_inoue_mbed 0:a224293d7af4 141 USERDATA2 = 0x2A,
s_inoue_mbed 0:a224293d7af4 142 USERDATA3 = 0x2B,
s_inoue_mbed 0:a224293d7af4 143 USERDATA4 = 0x2C
s_inoue_mbed 0:a224293d7af4 144 };
s_inoue_mbed 0:a224293d7af4 145 };
s_inoue_mbed 0:a224293d7af4 146
s_inoue_mbed 0:a224293d7af4 147 #endif