Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Grove-Barometric_Pressure_Sensor_Example
HP206C.h@4:a2b79f08ad14, 2019-06-25 (annotated)
- Committer:
- pmic
- Date:
- Tue Jun 25 14:20:18 2019 +0000
- Revision:
- 4:a2b79f08ad14
- Parent:
- 3:dd1c93488020
- Child:
- 6:470f31efa042
enhance more stuff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 4:a2b79f08ad14 | 1 | // source: https://os.mbed.com/users/ElectronicsSanta/code/HP206C_barometer/file/0a071ea90726/main.cpp/ |
pmic | 4:a2b79f08ad14 | 2 | |
pmic | 1:c5247b2ef5af | 3 | #ifndef _HP206C |
pmic | 1:c5247b2ef5af | 4 | #define _HP206C |
pmic | 1:c5247b2ef5af | 5 | |
pmic | 1:c5247b2ef5af | 6 | #include "mbed.h" |
pmic | 1:c5247b2ef5af | 7 | |
pmic | 1:c5247b2ef5af | 8 | #define I2C_BAROMETER_ADDRESS 0x76 << 1 // 0xEC, device adress |
pmic | 1:c5247b2ef5af | 9 | |
pmic | 1:c5247b2ef5af | 10 | #define HP20X_SOFT_RST 0x06 // for initializing, takes longer than next |
pmic | 1:c5247b2ef5af | 11 | #define HP20X_ANA_CAL 0x28 // recalibrate analog internal circuitries, for unstable environment |
pmic | 1:c5247b2ef5af | 12 | |
pmic | 1:c5247b2ef5af | 13 | #define HP20X_WR_CONVERT_CMD 0x40 |
pmic | 1:c5247b2ef5af | 14 | |
pmic | 1:c5247b2ef5af | 15 | #define HP20X_READ_PT 0x10 // read pressure and altitude command |
pmic | 1:c5247b2ef5af | 16 | #define HP20X_READ_A 0x31 // read altitude command |
pmic | 1:c5247b2ef5af | 17 | |
pmic | 1:c5247b2ef5af | 18 | #define HP20X_WR_REG_MODE 0xC0 |
pmic | 1:c5247b2ef5af | 19 | #define HP20X_RD_REG_MODE 0x80 |
pmic | 1:c5247b2ef5af | 20 | |
pmic | 1:c5247b2ef5af | 21 | #define HP20X_OK_DEV 0X80 // successfully initialized |
pmic | 1:c5247b2ef5af | 22 | #define HP20X_REG_PARA 0X0F // status register |
pmic | 1:c5247b2ef5af | 23 | |
pmic | 1:c5247b2ef5af | 24 | #define HP20X_CONVERT_OSR4096 0 << 2 |
pmic | 1:c5247b2ef5af | 25 | #define HP20X_CONVERT_OSR2048 1 << 2 |
pmic | 1:c5247b2ef5af | 26 | #define HP20X_CONVERT_OSR1024 2 << 2 |
pmic | 1:c5247b2ef5af | 27 | #define HP20X_CONVERT_OSR512 3 << 2 |
pmic | 1:c5247b2ef5af | 28 | #define HP20X_CONVERT_OSR256 4 << 2 |
pmic | 1:c5247b2ef5af | 29 | #define HP20X_CONVERT_OSR128 5 << 2 |
pmic | 4:a2b79f08ad14 | 30 | /* |
pmic | 4:a2b79f08ad14 | 31 | ADC is parametrizised through OSR_CFG |
pmic | 4:a2b79f08ad14 | 32 | HP20X_CONVERT_OSR128 -> OSR_ConvertTime = 5; // 4.1 ms -> ~200 Hz |
pmic | 4:a2b79f08ad14 | 33 | HP20X_CONVERT_OSR256 -> OSR_ConvertTime = 9; // 8.2 ms -> ~100 Hz |
pmic | 4:a2b79f08ad14 | 34 | HP20X_CONVERT_OSR512 -> OSR_ConvertTime = 17; // 16.4 ms -> ~ 50 Hz |
pmic | 4:a2b79f08ad14 | 35 | HP20X_CONVERT_OSR1024 -> OSR_ConvertTime = 34; // 32.8 ms -> ~ 20 Hz |
pmic | 4:a2b79f08ad14 | 36 | HP20X_CONVERT_OSR2048 -> OSR_ConvertTime = 67; // 65.6 ms -> ~ 10 Hz |
pmic | 4:a2b79f08ad14 | 37 | HP20X_CONVERT_OSR4096 -> OSR_ConvertTime = 132; // 131.1 ms -> ~ 5 Hz |
pmic | 4:a2b79f08ad14 | 38 | */ |
pmic | 4:a2b79f08ad14 | 39 | #define OSR_CFG HP20X_CONVERT_OSR1024 |
pmic | 1:c5247b2ef5af | 40 | |
pmic | 1:c5247b2ef5af | 41 | class HP206C |
pmic | 1:c5247b2ef5af | 42 | { |
pmic | 1:c5247b2ef5af | 43 | public: |
pmic | 1:c5247b2ef5af | 44 | |
pmic | 1:c5247b2ef5af | 45 | HP206C(PinName p_sda, PinName p_scl); |
pmic | 3:dd1c93488020 | 46 | |
pmic | 3:dd1c93488020 | 47 | float operator()() { |
pmic | 3:dd1c93488020 | 48 | return readAltitude(); |
pmic | 1:c5247b2ef5af | 49 | } |
pmic | 3:dd1c93488020 | 50 | |
pmic | 1:c5247b2ef5af | 51 | virtual ~HP206C(); |
pmic | 1:c5247b2ef5af | 52 | |
pmic | 1:c5247b2ef5af | 53 | void reset(); |
pmic | 3:dd1c93488020 | 54 | float readAltitude(); |
pmic | 1:c5247b2ef5af | 55 | |
pmic | 1:c5247b2ef5af | 56 | private: |
pmic | 1:c5247b2ef5af | 57 | |
pmic | 1:c5247b2ef5af | 58 | I2C i2c; |
pmic | 1:c5247b2ef5af | 59 | |
pmic | 4:a2b79f08ad14 | 60 | float altitude; |
pmic | 4:a2b79f08ad14 | 61 | bool baro_found; |
pmic | 4:a2b79f08ad14 | 62 | uint8_t OSR_ConvertTime; |
pmic | 1:c5247b2ef5af | 63 | |
pmic | 4:a2b79f08ad14 | 64 | void setConversionTime(); |
pmic | 1:c5247b2ef5af | 65 | unsigned char readRegister(unsigned char reg); |
pmic | 1:c5247b2ef5af | 66 | void writeRegister(unsigned char reg, unsigned char data); |
pmic | 1:c5247b2ef5af | 67 | void enableCompensation(); |
pmic | 1:c5247b2ef5af | 68 | void disableCompensation(); |
pmic | 1:c5247b2ef5af | 69 | void softReset(); |
pmic | 1:c5247b2ef5af | 70 | void recalibrate(); |
pmic | 1:c5247b2ef5af | 71 | uint32_t readData3Bytes(); |
pmic | 3:dd1c93488020 | 72 | void readTemperatureAndPressure(); |
pmic | 3:dd1c93488020 | 73 | void updateAltitude(); |
pmic | 1:c5247b2ef5af | 74 | |
pmic | 1:c5247b2ef5af | 75 | }; |
pmic | 1:c5247b2ef5af | 76 | |
pmic | 1:c5247b2ef5af | 77 | #endif |