HP206C waterproof high-accuracy barometr ported from arduino

Dependents:   projetS1

Files at this revision

API Documentation at this revision

Comitter:
Dzhafarkhanov
Date:
Thu Oct 30 14:00:11 2014 +0000
Commit message:
HP206C Waterproof barometr

Changed in this revision

HP20x_dev.cpp Show annotated file Show diff for this revision Revisions of this file
HP20x_dev.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 573122fe4fd1 HP20x_dev.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HP20x_dev.cpp	Thu Oct 30 14:00:11 2014 +0000
@@ -0,0 +1,132 @@
+/*
+ * File name  : HP20x_dev.cpp
+ * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C]
+ * Author     : Oliver Wang from Seeed studio
+ * Version    : V0.1
+ * Create Time: 2014/04
+ * Change Log :
+*/
+
+/****************************************************************************/
+/***        Include files                                                 ***/
+/****************************************************************************/
+#include "HP20x_dev.h"
+#include "mbed.h"
+//#include "KalmanFilter.h"
+/****************************************************************************/
+/***       Local Variable                                                 ***/
+/****************************************************************************/
+
+
+
+/****************************************************************************/
+/***       Class member Functions                                         ***/
+/****************************************************************************/
+/*
+ **@ Function name: HP20x_dev
+ **@ Description: Constructor
+ **@ Input: none
+ **@ OutPut: none
+ **@ Retval: none
+*/
+
+HP20x_dev::HP20x_dev(PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl) {
+   OSR_CFG = HP20X_CONVERT_OSR1024;
+   OSR_ConvertTime = 25; 
+}
+
+
+/*
+ **@ Function name: begin
+ **@ Description: Initialize HP20x_dev
+ **@ Input: none
+ **@ OutPut: none
+ **@ Retval: none
+*/
+void HP20x_dev::reset()
+{
+ 
+  /* Reset HP20x_dev */
+  char data_write[1];
+  data_write[0] = HP20X_SOFT_RST;
+  if ( i2c.write( HP20X_ADDRESS,  data_write, 1,0 ) );
+   wait_ms(60);
+
+}
+
+/*
+ **@ Function name: isAvailable
+ **@ Description: Indicate whether it's available
+ **@ Input: none
+ **@ OutPut: none
+ **@ Retval: uchar 
+*/
+char HP20x_dev::isAvailable()
+{
+  char ret = readByte(HP20X_ADDRESS,REG_PARA|HP20X_RD_REG_MODE);//readreg
+  return ret;
+}
+
+
+
+long HP20x_dev::ReadPressure(void)
+{
+ 
+  
+  writeByte(HP20X_ADDRESS,HP20X_WR_CONVERT_CMD,OSR_CFG);
+   
+wait_ms(OSR_ConvertTime);
+
+    char data_write[1];
+    data_write[0] =  HP20X_READ_P;
+  if ( i2c.write( HP20X_ADDRESS,data_write,1,0 ) );
+ 
+    long TempData = 0;
+    long tmpArray[3]={0};
+    
+    
+    /* Require three bytes from slave */
+   
+    char data[3];
+   
+    data_write[0] = I2C_DID_RD_MASK;
+    i2c.write(HP20X_ADDRESS, data_write, 1, 1); // no stop
+    i2c.read(HP20X_ADDRESS, data, 3, 0); 
+    for(int ii = 0; ii < 3; ii++) {
+     tmpArray[ii] = data[ii];}
+    
+    /* MSB */
+    TempData = tmpArray[0]<<16 | tmpArray[1]<<8 | tmpArray[2];
+
+    
+    if(TempData&0x800000)
+    {
+        TempData|=0xff000000;
+    }
+
+    return TempData;        
+   
+} 
+
+
+void HP20x_dev::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
+    {
+       char data_write[2];
+       data_write[0] = subAddress;
+       data_write[1] = data;
+       i2c.write(address, data_write, 2, 0);
+    }
+
+char HP20x_dev::readByte(uint8_t address, uint8_t subAddress)
+    {
+        char data[1]; // `data` will store the register data     
+        char data_write[1];
+        data_write[0] = subAddress;
+        i2c.write(address, data_write, 1, 1); // no stop
+        i2c.read(address, data, 1, 0); 
+        return data[0]; 
+    }
+
+
+
+/**************************************END OF FILE**************************************/
\ No newline at end of file
diff -r 000000000000 -r 573122fe4fd1 HP20x_dev.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HP20x_dev.h	Thu Oct 30 14:00:11 2014 +0000
@@ -0,0 +1,101 @@
+/*Portet from arduino
+
+
+ * File name  : HP20x_dev.h
+ * Description: Driver for I2C PRECISION BAROMETER AND ALTIMETER [HP206C]
+ * Author     : Oliver Wang from Seeed studio
+ * Version    : V0.1
+ * Create Time: 2014/04
+ * Change Log :
+*/
+#ifndef _HP20X_DEV_H
+#define _HP20X_DEV_H
+/****************************************************************************/
+/***        Including Files                                               ***/
+/****************************************************************************/
+
+#include "mbed.h"
+/****************************************************************************/
+/***        Macro Definitions                                             ***/
+/****************************************************************************/
+
+
+#define HP20X_ADDRESS          0xEC
+//#define HP20X_I2C_DEV_ID       (0xEC)>>1    //CSB PIN is VDD level(address is 0x76)
+//#define HP20X_I2C_DEV_ID2      (0XEE)>>1    //CSB PIN is GND level(address is 0x77)
+#define HP20X_SOFT_RST         0x06 //Soft reset the device
+#define HP20X_WR_CONVERT_CMD   0x40
+#define HP20X_CONVERT_OSR4096  0<<2
+#define HP20X_CONVERT_OSR2048  1<<2
+#define HP20X_CONVERT_OSR1024  2<<2
+#define HP20X_CONVERT_OSR512   3<<2
+#define HP20X_CONVERT_OSR256   4<<2
+#define HP20X_CONVERT_OSR128   5<<2
+
+#define HP20X_READ_P           0x30   //Read the pressure value only
+#define HP20X_READ_A           0x31   //Read the altitude value only
+#define HP20X_READ_T           0x32   //Read the temperature value only
+#define HP20X_READ_PT          0x10   //Read the temperature and pressure values
+#define HP20X_READ_AT          0x11   //Read the temperature and altitude values
+#define HP20X_READ_CAL         0X28   //Re-calibrate the internal analog blocks
+
+#define HP20X_WR_REG_MODE      0xC0
+#define HP20X_RD_REG_MODE      0x80
+
+#define ERR_WR_DEVID_NACK       0x01    
+#define ERR_RD_DEVID_NACK       0x02    
+#define ERR_WR_REGADD_NACK      0x04   
+#define ERR_WR_REGCMD_NACK      0x08   
+#define ERR_WR_DATA_NACK        0x10     
+#define ERR_RD_DATA_MISMATCH    0x20 
+
+#define I2C_DID_WR_MASK         0xFE
+#define I2C_DID_RD_MASK         0x01
+
+#define T_WIN_EN                0X01
+#define PA_WIN_EN               0X02
+#define T_TRAV_EN               0X04
+#define PA_TRAV_EN              0X08
+#define PA_RDY_EN               0X20
+#define T_RDY_EN                0X10
+
+#define T_WIN_CFG               0X01
+#define PA_WIN_CFG              0X02
+#define PA_MODE_P               0X00
+#define PA_MODE_A               0X40
+
+#define T_TRAV_CFG              0X04
+
+#define OK_HP20X_DEV            0X80        //HP20x_dev successfully initialized
+#define REG_PARA                0X0F        //Status register
+
+/****************************************************************************/
+/***        Class Definitions                                             ***/
+/****************************************************************************/
+class HP20x_dev
+{
+  /* Public variables and functions */
+  public:
+  
+  HP20x_dev(PinName p_sda, PinName p_scl);
+  
+    char OSR_CFG;
+    int  OSR_ConvertTime;
+    /* Constructor */ 
+    void reset();
+    char isAvailable();
+    
+    /* Read sensor data */
+    //long ReadTemperature(void);
+    long ReadPressure(void);
+   // long ReadAltitude(void);
+   protected:
+    I2C i2c;
+    void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
+    char readByte(uint8_t address, uint8_t subAddress);
+   
+    
+  
+};
+//extern HP20x_dev HP20x;
+#endif
\ No newline at end of file