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.
HP20x_dev.cpp
00001 /* 00002 * File name : HP20x_dev.cpp 00003 * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C] 00004 * Author : Oliver Wang from Seeed studio 00005 * Version : V0.1 00006 * Create Time: 2014/04 00007 * Change Log : 00008 */ 00009 00010 /****************************************************************************/ 00011 /*** Include files ***/ 00012 /****************************************************************************/ 00013 #include "HP20x_dev.h" 00014 #include "mbed.h" 00015 //#include "KalmanFilter.h" 00016 /****************************************************************************/ 00017 /*** Local Variable ***/ 00018 /****************************************************************************/ 00019 00020 00021 00022 /****************************************************************************/ 00023 /*** Class member Functions ***/ 00024 /****************************************************************************/ 00025 /* 00026 **@ Function name: HP20x_dev 00027 **@ Description: Constructor 00028 **@ Input: none 00029 **@ OutPut: none 00030 **@ Retval: none 00031 */ 00032 00033 HP20x_dev::HP20x_dev(PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl) { 00034 OSR_CFG = HP20X_CONVERT_OSR1024; 00035 OSR_ConvertTime = 25; 00036 } 00037 00038 00039 /* 00040 **@ Function name: begin 00041 **@ Description: Initialize HP20x_dev 00042 **@ Input: none 00043 **@ OutPut: none 00044 **@ Retval: none 00045 */ 00046 void HP20x_dev::reset() 00047 { 00048 00049 /* Reset HP20x_dev */ 00050 char data_write[1]; 00051 data_write[0] = HP20X_SOFT_RST; 00052 if ( i2c.write( HP20X_ADDRESS, data_write, 1,0 ) ); 00053 wait_ms(60); 00054 00055 } 00056 00057 /* 00058 **@ Function name: isAvailable 00059 **@ Description: Indicate whether it's available 00060 **@ Input: none 00061 **@ OutPut: none 00062 **@ Retval: uchar 00063 */ 00064 char HP20x_dev::isAvailable() 00065 { 00066 char ret = readByte(HP20X_ADDRESS,REG_PARA|HP20X_RD_REG_MODE);//readreg 00067 return ret; 00068 } 00069 00070 00071 00072 long HP20x_dev::ReadPressure(void) 00073 { 00074 00075 00076 writeByte(HP20X_ADDRESS,HP20X_WR_CONVERT_CMD,OSR_CFG); 00077 00078 wait_ms(OSR_ConvertTime); 00079 00080 char data_write[1]; 00081 data_write[0] = HP20X_READ_P; 00082 if ( i2c.write( HP20X_ADDRESS,data_write,1,0 ) ); 00083 00084 long TempData = 0; 00085 long tmpArray[3]={0}; 00086 00087 00088 /* Require three bytes from slave */ 00089 00090 char data[3]; 00091 00092 data_write[0] = I2C_DID_RD_MASK; 00093 i2c.write(HP20X_ADDRESS, data_write, 1, 1); // no stop 00094 i2c.read(HP20X_ADDRESS, data, 3, 0); 00095 for(int ii = 0; ii < 3; ii++) { 00096 tmpArray[ii] = data[ii];} 00097 00098 /* MSB */ 00099 TempData = tmpArray[0]<<16 | tmpArray[1]<<8 | tmpArray[2]; 00100 00101 00102 if(TempData&0x800000) 00103 { 00104 TempData|=0xff000000; 00105 } 00106 00107 return TempData; 00108 00109 } 00110 00111 00112 void HP20x_dev::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) 00113 { 00114 char data_write[2]; 00115 data_write[0] = subAddress; 00116 data_write[1] = data; 00117 i2c.write(address, data_write, 2, 0); 00118 } 00119 00120 char HP20x_dev::readByte(uint8_t address, uint8_t subAddress) 00121 { 00122 char data[1]; // `data` will store the register data 00123 char data_write[1]; 00124 data_write[0] = subAddress; 00125 i2c.write(address, data_write, 1, 1); // no stop 00126 i2c.read(address, data, 1, 0); 00127 return data[0]; 00128 } 00129 00130 00131 00132 /**************************************END OF FILE**************************************/
Generated on Sat Jul 16 2022 09:38:04 by
1.7.2