HP206C waterproof high-accuracy barometr ported from arduino
HP20x_dev.cpp@0:573122fe4fd1, 2014-10-30 (annotated)
- Committer:
- Dzhafarkhanov
- Date:
- Thu Oct 30 14:00:11 2014 +0000
- Revision:
- 0:573122fe4fd1
HP206C Waterproof barometr
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dzhafarkhanov | 0:573122fe4fd1 | 1 | /* |
Dzhafarkhanov | 0:573122fe4fd1 | 2 | * File name : HP20x_dev.cpp |
Dzhafarkhanov | 0:573122fe4fd1 | 3 | * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C] |
Dzhafarkhanov | 0:573122fe4fd1 | 4 | * Author : Oliver Wang from Seeed studio |
Dzhafarkhanov | 0:573122fe4fd1 | 5 | * Version : V0.1 |
Dzhafarkhanov | 0:573122fe4fd1 | 6 | * Create Time: 2014/04 |
Dzhafarkhanov | 0:573122fe4fd1 | 7 | * Change Log : |
Dzhafarkhanov | 0:573122fe4fd1 | 8 | */ |
Dzhafarkhanov | 0:573122fe4fd1 | 9 | |
Dzhafarkhanov | 0:573122fe4fd1 | 10 | /****************************************************************************/ |
Dzhafarkhanov | 0:573122fe4fd1 | 11 | /*** Include files ***/ |
Dzhafarkhanov | 0:573122fe4fd1 | 12 | /****************************************************************************/ |
Dzhafarkhanov | 0:573122fe4fd1 | 13 | #include "HP20x_dev.h" |
Dzhafarkhanov | 0:573122fe4fd1 | 14 | #include "mbed.h" |
Dzhafarkhanov | 0:573122fe4fd1 | 15 | //#include "KalmanFilter.h" |
Dzhafarkhanov | 0:573122fe4fd1 | 16 | /****************************************************************************/ |
Dzhafarkhanov | 0:573122fe4fd1 | 17 | /*** Local Variable ***/ |
Dzhafarkhanov | 0:573122fe4fd1 | 18 | /****************************************************************************/ |
Dzhafarkhanov | 0:573122fe4fd1 | 19 | |
Dzhafarkhanov | 0:573122fe4fd1 | 20 | |
Dzhafarkhanov | 0:573122fe4fd1 | 21 | |
Dzhafarkhanov | 0:573122fe4fd1 | 22 | /****************************************************************************/ |
Dzhafarkhanov | 0:573122fe4fd1 | 23 | /*** Class member Functions ***/ |
Dzhafarkhanov | 0:573122fe4fd1 | 24 | /****************************************************************************/ |
Dzhafarkhanov | 0:573122fe4fd1 | 25 | /* |
Dzhafarkhanov | 0:573122fe4fd1 | 26 | **@ Function name: HP20x_dev |
Dzhafarkhanov | 0:573122fe4fd1 | 27 | **@ Description: Constructor |
Dzhafarkhanov | 0:573122fe4fd1 | 28 | **@ Input: none |
Dzhafarkhanov | 0:573122fe4fd1 | 29 | **@ OutPut: none |
Dzhafarkhanov | 0:573122fe4fd1 | 30 | **@ Retval: none |
Dzhafarkhanov | 0:573122fe4fd1 | 31 | */ |
Dzhafarkhanov | 0:573122fe4fd1 | 32 | |
Dzhafarkhanov | 0:573122fe4fd1 | 33 | HP20x_dev::HP20x_dev(PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl) { |
Dzhafarkhanov | 0:573122fe4fd1 | 34 | OSR_CFG = HP20X_CONVERT_OSR1024; |
Dzhafarkhanov | 0:573122fe4fd1 | 35 | OSR_ConvertTime = 25; |
Dzhafarkhanov | 0:573122fe4fd1 | 36 | } |
Dzhafarkhanov | 0:573122fe4fd1 | 37 | |
Dzhafarkhanov | 0:573122fe4fd1 | 38 | |
Dzhafarkhanov | 0:573122fe4fd1 | 39 | /* |
Dzhafarkhanov | 0:573122fe4fd1 | 40 | **@ Function name: begin |
Dzhafarkhanov | 0:573122fe4fd1 | 41 | **@ Description: Initialize HP20x_dev |
Dzhafarkhanov | 0:573122fe4fd1 | 42 | **@ Input: none |
Dzhafarkhanov | 0:573122fe4fd1 | 43 | **@ OutPut: none |
Dzhafarkhanov | 0:573122fe4fd1 | 44 | **@ Retval: none |
Dzhafarkhanov | 0:573122fe4fd1 | 45 | */ |
Dzhafarkhanov | 0:573122fe4fd1 | 46 | void HP20x_dev::reset() |
Dzhafarkhanov | 0:573122fe4fd1 | 47 | { |
Dzhafarkhanov | 0:573122fe4fd1 | 48 | |
Dzhafarkhanov | 0:573122fe4fd1 | 49 | /* Reset HP20x_dev */ |
Dzhafarkhanov | 0:573122fe4fd1 | 50 | char data_write[1]; |
Dzhafarkhanov | 0:573122fe4fd1 | 51 | data_write[0] = HP20X_SOFT_RST; |
Dzhafarkhanov | 0:573122fe4fd1 | 52 | if ( i2c.write( HP20X_ADDRESS, data_write, 1,0 ) ); |
Dzhafarkhanov | 0:573122fe4fd1 | 53 | wait_ms(60); |
Dzhafarkhanov | 0:573122fe4fd1 | 54 | |
Dzhafarkhanov | 0:573122fe4fd1 | 55 | } |
Dzhafarkhanov | 0:573122fe4fd1 | 56 | |
Dzhafarkhanov | 0:573122fe4fd1 | 57 | /* |
Dzhafarkhanov | 0:573122fe4fd1 | 58 | **@ Function name: isAvailable |
Dzhafarkhanov | 0:573122fe4fd1 | 59 | **@ Description: Indicate whether it's available |
Dzhafarkhanov | 0:573122fe4fd1 | 60 | **@ Input: none |
Dzhafarkhanov | 0:573122fe4fd1 | 61 | **@ OutPut: none |
Dzhafarkhanov | 0:573122fe4fd1 | 62 | **@ Retval: uchar |
Dzhafarkhanov | 0:573122fe4fd1 | 63 | */ |
Dzhafarkhanov | 0:573122fe4fd1 | 64 | char HP20x_dev::isAvailable() |
Dzhafarkhanov | 0:573122fe4fd1 | 65 | { |
Dzhafarkhanov | 0:573122fe4fd1 | 66 | char ret = readByte(HP20X_ADDRESS,REG_PARA|HP20X_RD_REG_MODE);//readreg |
Dzhafarkhanov | 0:573122fe4fd1 | 67 | return ret; |
Dzhafarkhanov | 0:573122fe4fd1 | 68 | } |
Dzhafarkhanov | 0:573122fe4fd1 | 69 | |
Dzhafarkhanov | 0:573122fe4fd1 | 70 | |
Dzhafarkhanov | 0:573122fe4fd1 | 71 | |
Dzhafarkhanov | 0:573122fe4fd1 | 72 | long HP20x_dev::ReadPressure(void) |
Dzhafarkhanov | 0:573122fe4fd1 | 73 | { |
Dzhafarkhanov | 0:573122fe4fd1 | 74 | |
Dzhafarkhanov | 0:573122fe4fd1 | 75 | |
Dzhafarkhanov | 0:573122fe4fd1 | 76 | writeByte(HP20X_ADDRESS,HP20X_WR_CONVERT_CMD,OSR_CFG); |
Dzhafarkhanov | 0:573122fe4fd1 | 77 | |
Dzhafarkhanov | 0:573122fe4fd1 | 78 | wait_ms(OSR_ConvertTime); |
Dzhafarkhanov | 0:573122fe4fd1 | 79 | |
Dzhafarkhanov | 0:573122fe4fd1 | 80 | char data_write[1]; |
Dzhafarkhanov | 0:573122fe4fd1 | 81 | data_write[0] = HP20X_READ_P; |
Dzhafarkhanov | 0:573122fe4fd1 | 82 | if ( i2c.write( HP20X_ADDRESS,data_write,1,0 ) ); |
Dzhafarkhanov | 0:573122fe4fd1 | 83 | |
Dzhafarkhanov | 0:573122fe4fd1 | 84 | long TempData = 0; |
Dzhafarkhanov | 0:573122fe4fd1 | 85 | long tmpArray[3]={0}; |
Dzhafarkhanov | 0:573122fe4fd1 | 86 | |
Dzhafarkhanov | 0:573122fe4fd1 | 87 | |
Dzhafarkhanov | 0:573122fe4fd1 | 88 | /* Require three bytes from slave */ |
Dzhafarkhanov | 0:573122fe4fd1 | 89 | |
Dzhafarkhanov | 0:573122fe4fd1 | 90 | char data[3]; |
Dzhafarkhanov | 0:573122fe4fd1 | 91 | |
Dzhafarkhanov | 0:573122fe4fd1 | 92 | data_write[0] = I2C_DID_RD_MASK; |
Dzhafarkhanov | 0:573122fe4fd1 | 93 | i2c.write(HP20X_ADDRESS, data_write, 1, 1); // no stop |
Dzhafarkhanov | 0:573122fe4fd1 | 94 | i2c.read(HP20X_ADDRESS, data, 3, 0); |
Dzhafarkhanov | 0:573122fe4fd1 | 95 | for(int ii = 0; ii < 3; ii++) { |
Dzhafarkhanov | 0:573122fe4fd1 | 96 | tmpArray[ii] = data[ii];} |
Dzhafarkhanov | 0:573122fe4fd1 | 97 | |
Dzhafarkhanov | 0:573122fe4fd1 | 98 | /* MSB */ |
Dzhafarkhanov | 0:573122fe4fd1 | 99 | TempData = tmpArray[0]<<16 | tmpArray[1]<<8 | tmpArray[2]; |
Dzhafarkhanov | 0:573122fe4fd1 | 100 | |
Dzhafarkhanov | 0:573122fe4fd1 | 101 | |
Dzhafarkhanov | 0:573122fe4fd1 | 102 | if(TempData&0x800000) |
Dzhafarkhanov | 0:573122fe4fd1 | 103 | { |
Dzhafarkhanov | 0:573122fe4fd1 | 104 | TempData|=0xff000000; |
Dzhafarkhanov | 0:573122fe4fd1 | 105 | } |
Dzhafarkhanov | 0:573122fe4fd1 | 106 | |
Dzhafarkhanov | 0:573122fe4fd1 | 107 | return TempData; |
Dzhafarkhanov | 0:573122fe4fd1 | 108 | |
Dzhafarkhanov | 0:573122fe4fd1 | 109 | } |
Dzhafarkhanov | 0:573122fe4fd1 | 110 | |
Dzhafarkhanov | 0:573122fe4fd1 | 111 | |
Dzhafarkhanov | 0:573122fe4fd1 | 112 | void HP20x_dev::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) |
Dzhafarkhanov | 0:573122fe4fd1 | 113 | { |
Dzhafarkhanov | 0:573122fe4fd1 | 114 | char data_write[2]; |
Dzhafarkhanov | 0:573122fe4fd1 | 115 | data_write[0] = subAddress; |
Dzhafarkhanov | 0:573122fe4fd1 | 116 | data_write[1] = data; |
Dzhafarkhanov | 0:573122fe4fd1 | 117 | i2c.write(address, data_write, 2, 0); |
Dzhafarkhanov | 0:573122fe4fd1 | 118 | } |
Dzhafarkhanov | 0:573122fe4fd1 | 119 | |
Dzhafarkhanov | 0:573122fe4fd1 | 120 | char HP20x_dev::readByte(uint8_t address, uint8_t subAddress) |
Dzhafarkhanov | 0:573122fe4fd1 | 121 | { |
Dzhafarkhanov | 0:573122fe4fd1 | 122 | char data[1]; // `data` will store the register data |
Dzhafarkhanov | 0:573122fe4fd1 | 123 | char data_write[1]; |
Dzhafarkhanov | 0:573122fe4fd1 | 124 | data_write[0] = subAddress; |
Dzhafarkhanov | 0:573122fe4fd1 | 125 | i2c.write(address, data_write, 1, 1); // no stop |
Dzhafarkhanov | 0:573122fe4fd1 | 126 | i2c.read(address, data, 1, 0); |
Dzhafarkhanov | 0:573122fe4fd1 | 127 | return data[0]; |
Dzhafarkhanov | 0:573122fe4fd1 | 128 | } |
Dzhafarkhanov | 0:573122fe4fd1 | 129 | |
Dzhafarkhanov | 0:573122fe4fd1 | 130 | |
Dzhafarkhanov | 0:573122fe4fd1 | 131 | |
Dzhafarkhanov | 0:573122fe4fd1 | 132 | /**************************************END OF FILE**************************************/ |