HP20x_dev.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * File name : HP20x_dev.cpp
  3. * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C]
  4. * Author : Oliver Wang from Seeed studio
  5. * Version : V0.1
  6. * Create Time: 2014/04
  7. * Change Log :
  8. */
  9. /****************************************************************************/
  10. /*** Include files ***/
  11. /****************************************************************************/
  12. #include "HP20x_dev.h"
  13. #include <Wire.h>
  14. #include <Arduino.h>
  15. #include "KalmanFilter.h"
  16. /****************************************************************************/
  17. /*** Local Variable ***/
  18. /****************************************************************************/
  19. HP20x_dev HP20x;
  20. /****************************************************************************/
  21. /*** Class member Functions ***/
  22. /****************************************************************************/
  23. /*
  24. **@ Function name: HP20x_dev
  25. **@ Description: Constructor
  26. **@ Input: none
  27. **@ OutPut: none
  28. **@ Retval: none
  29. */
  30. HP20x_dev::HP20x_dev()
  31. {
  32. OSR_CFG = HP20X_CONVERT_OSR1024;
  33. OSR_ConvertTime = 25;
  34. }
  35. /*
  36. **@ Function name: begin
  37. **@ Description: Initialize HP20x_dev
  38. **@ Input: none
  39. **@ OutPut: none
  40. **@ Retval: none
  41. */
  42. void HP20x_dev::begin()
  43. {
  44. Wire.begin();
  45. /* Reset HP20x_dev */
  46. HP20x.HP20X_IIC_WriteCmd(HP20X_SOFT_RST);
  47. }
  48. /*
  49. **@ Function name: isAvailable
  50. **@ Description: Indicate whether it's available
  51. **@ Input: none
  52. **@ OutPut: none
  53. **@ Retval: uchar
  54. */
  55. uchar HP20x_dev::isAvailable()
  56. {
  57. uchar ret = HP20x.HP20X_IIC_ReadReg(REG_PARA);
  58. return ret;
  59. }
  60. /*
  61. **@ Function name: ReadTemperature
  62. **@ Description: Read Temperature from HP20x_dev
  63. **@ Input:
  64. **@ OutPut:
  65. **@ Retval:
  66. */
  67. ulong HP20x_dev::ReadTemperature(void)
  68. {
  69. uchar Temp;
  70. uchar Temp0;
  71. HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG); //ADC convert
  72. delay(OSR_ConvertTime); //difference OSR_CFG will be difference OSR_ConvertTime
  73. HP20X_IIC_WriteCmd(HP20X_READ_T);
  74. ulong Temperature = HP20X_IIC_ReadData();
  75. return Temperature;
  76. }
  77. /*
  78. **@ Function name: ReadPressure
  79. **@ Description: Read Pressure value
  80. **@ Input:
  81. **@ OutPut:
  82. **@ Retval: value
  83. */
  84. ulong HP20x_dev::ReadPressure(void)
  85. {
  86. HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG);
  87. delay(OSR_ConvertTime);
  88. HP20X_IIC_WriteCmd(HP20X_READ_P);
  89. ulong Pressure = HP20X_IIC_ReadData();
  90. return Pressure;
  91. }
  92. /*
  93. **@ Function name: ReadAltitude
  94. **@ Description: Read Pressure value
  95. **@ Input:
  96. **@ OutPut:
  97. **@ Retval: value
  98. */
  99. ulong HP20x_dev::ReadAltitude(void)
  100. {
  101. HP20X_IIC_WriteCmd(HP20X_READ_A);
  102. ulong Altitude = HP20X_IIC_ReadData();
  103. return Altitude;
  104. }
  105. /*
  106. void ReadPressureAndTemperature(void)
  107. {
  108. HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG);
  109. Timer_Delayxms(OSR_ConvertTime*2);
  110. HP20X_IIC_WriteCmd(HP20X_READ_PT);
  111. Temperature=HP20X_IIC_ReadData();
  112. Pressure=HP20X_IIC_ReadData3byte();
  113. }
  114. void IIC_ReadAltitudeAndTemperature(void)
  115. {
  116. HP20X_IIC_WriteCmd(HP20X_WR_CONVERT_CMD|OSR_CFG);
  117. Timer_Delayxms(OSR_ConvertTime*2);
  118. HP20X_IIC_WriteCmd(HP20X_READ_AT);
  119. Temperature=HP20X_IIC_ReadData();
  120. IIC_ACK();
  121. Altitude=HP20X_IIC_ReadData3byte();
  122. IIC_NoAck();
  123. IIC_Stop();
  124. }*/
  125. /****************************************************************************/
  126. /*** Local Functions ***/
  127. /****************************************************************************/
  128. /*
  129. **@ Function name: HP20X_IIC_WriteCmd
  130. **@ Description:
  131. **@ Input:
  132. **@ OutPut:
  133. **@ Retval:
  134. */
  135. void HP20x_dev::HP20X_IIC_WriteCmd(uchar uCmd)
  136. {
  137. /* Port to arduino */
  138. Wire.beginTransmission(HP20X_I2C_DEV_ID);
  139. Wire.write(uCmd);
  140. Wire.endTransmission();
  141. }
  142. /*
  143. **@ Function name: HP20X_IIC_ReadReg
  144. **@ Description:
  145. **@ Input:
  146. **@ OutPut:
  147. **@ Retval:
  148. */
  149. uchar HP20x_dev::HP20X_IIC_ReadReg(uchar bReg)
  150. {
  151. /* Port to arduino */
  152. uchar Temp = 0;
  153. /* Send a register reading command */
  154. HP20X_IIC_WriteCmd(bReg|HP20X_RD_REG_MODE);
  155. Wire.requestFrom(HP20X_I2C_DEV_ID, 1);
  156. while(Wire.available())
  157. {
  158. Temp = Wire.read();
  159. }
  160. return Temp;
  161. }
  162. /*
  163. **@ Function name: HP20X_IIC_WriteReg
  164. **@ Description:
  165. **@ Input:
  166. **@ OutPut:
  167. **@ Retval:
  168. */
  169. void HP20x_dev::HP20X_IIC_WriteReg(uchar bReg,uchar bData)
  170. {
  171. Wire.beginTransmission(HP20X_I2C_DEV_ID);
  172. Wire.write(bReg|HP20X_WR_REG_MODE);
  173. Wire.write(bData);
  174. Wire.endTransmission();
  175. }
  176. /*
  177. **@ Function name: HP20X_IIC_ReadData
  178. **@ Description:
  179. **@ Input:
  180. **@ OutPut:
  181. **@ Retval:
  182. */
  183. ulong HP20x_dev::HP20X_IIC_ReadData(void)
  184. {
  185. /* Port to arduino */
  186. ulong Temp = HP20X_IIC_ReadData3byte();
  187. return Temp;
  188. }
  189. /*
  190. **@ Function name: HP20X_IIC_ReadData3byte
  191. **@ Description:
  192. **@ Input:
  193. **@ OutPut:
  194. **@ Retval:
  195. */
  196. ulong HP20x_dev::HP20X_IIC_ReadData3byte(void)
  197. {
  198. ulong TempData = 0;
  199. ulong tmpArray[3]={0};
  200. int cnt = 0;
  201. /* Require three bytes from slave */
  202. Wire.requestFrom(HP20X_I2C_DEV_ID, 3);
  203. while(Wire.available()) // slave may send less than requested
  204. {
  205. uchar c = Wire.read(); // receive a byte as character
  206. tmpArray[cnt] = (ulong)c;
  207. cnt++;
  208. }
  209. /* MSB */
  210. TempData = tmpArray[0]<<16 | tmpArray[1]<<8 | tmpArray[2];
  211. if(TempData&0x800000)
  212. {
  213. TempData|=0xff000000;
  214. }
  215. /* // 24 bit to 32 bit
  216. if(TempData&0x800000)
  217. {
  218. // 1:minus
  219. TempData |= 0x80000000;
  220. TempData &= 0xff7fffff;
  221. }
  222. else
  223. {
  224. // 0:plus
  225. //do noting
  226. } */
  227. return TempData;
  228. }
  229. /**************************************END OF FILE**************************************/