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.
Revision 2:f574cd898cba, committed 2017-04-18
- Comitter:
- Rhyme
- Date:
- Tue Apr 18 02:44:57 2017 +0000
- Parent:
- 1:f2c04c5b28ab
- Commit message:
- Documentation writing started
Changed in this revision
| HDC1000.cpp | Show annotated file Show diff for this revision Revisions of this file |
| HDC1000.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r f2c04c5b28ab -r f574cd898cba HDC1000.cpp
--- a/HDC1000.cpp Mon Apr 17 07:37:19 2017 +0000
+++ b/HDC1000.cpp Tue Apr 18 02:44:57 2017 +0000
@@ -27,10 +27,12 @@
#define BIT_HRES11 0x0100
#define BIT_HRES08 0x0200
+#if USE_READY_PIN
HDC1000::HDC1000(PinName sda, PinName scl, PinName rdy, int addr) : m_i2c(sda, scl), m_rdy(rdy), m_addr(addr<<1) {
-// HDC1000::HDC1000(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) {
+#else
+HDC1000::HDC1000(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) {
+#endif
// activate the peripheral
-
}
HDC1000::~HDC1000(void)
@@ -68,7 +70,6 @@
float HDC1000::readTemperature(void)
{
uint16_t utemp, uhum ;
- int32_t ltemp ;
int mode ;
float ftemp ;
@@ -85,15 +86,8 @@
break ;
}
- printf("utemp = 0x%04X ", utemp) ;
+// printf("utemp = 0x%04X ", utemp) ;
ftemp = u2f_temp(utemp) ;
- /*
- ltemp = (utemp >> 2) & 0x3FFF ;
-
- // note: the data sheet suggests to use 0x10000 for denominator
- // but to allow 100% I chose 0xFFFF instead, I may be wrong
- ftemp = ((float)(ltemp)/ (float)0x3FFF)*165.0 - 40.0 ;
- */
return( ftemp ) ;
}
@@ -101,7 +95,6 @@
{
uint16_t utemp, uhume ;
int mode ;
- int32_t lhume ;
float fhume ;
mode = getMode() ;
@@ -117,15 +110,8 @@
break ;
}
- printf("uhume = 0x%04X\n", uhume) ;
+// printf("uhume = 0x%04X\n", uhume) ;
fhume = u2f_hume(uhume) ;
- /*
- lhume = (uhume>>2) & 0x3FFF ;
-
- // note: the data sheet suggests to use 0x10000 for denominator
- // but to allow 100% I chose 0xFFFF instead, I may be wrong
- fhum = ((float)(lhume) / (float)0x3FFF) * 100.0 ;
- */
return( fhume ) ;
}
@@ -133,29 +119,30 @@
{
uint16_t utemp, uhume ;
getData(&utemp, &uhume) ;
- printf("utemp: 0x%04X, uhume: 0x%04X\n", utemp, uhume) ;
+// printf("utemp: 0x%04X, uhume: 0x%04X\n", utemp, uhume) ;
*ftemp = u2f_temp(utemp) ;
*fhume = u2f_hume(uhume) ;
-#if 0
- utemp = (utemp >> 2) & 0x3FFF ;
- uhume = (uhume >> 2) & 0x3FFF ;
- *ftemp = ((float)(utemp)/ (float)0x3FFF)*165.0 - 40.0 ;
- *fhume = ((float)(uhume) / (float)0x3FFF) * 100.0 ;
-#endif
}
+
/* for mode 0 */
uint16_t HDC1000::getTemperature(void)
{
uint16_t temp ;
uint8_t data[2] ;
+#if USE_READY_PIN
float delay ;
+#endif
data[0] = REG_TEMPERATURE ;
data[1] = (m_addr << 1) | 0x01 ;
m_i2c.write(m_addr, (const char*)data, 2, true);
+
+#if USE_READY_PIN
while(m_rdy == 1) { } /* wait for rdy */
-
-// delay = getTDelay() ;
-// wait_us(1000 * delay) ;
+#else
+ delay = getTDelay() ;
+ printf("Temp Delay = %.2f\n", delay) ;
+ wait_us(1000 * delay) ;
+#endif
m_i2c.read(m_addr, (char *)data, 2);
temp = (data[0] << 8) | data[1] ;
@@ -166,16 +153,21 @@
{
uint16_t hume ;
uint8_t data[2] ;
+ #if USE_READY_PIN
float delay ;
-// readRegs(REG_HUMIDITY, data, 2) ;
+#endif
+
data[0] = REG_HUMIDITY ;
data[1] = (m_addr << 1) | 0x01 ;
m_i2c.write(m_addr, (const char*)data, 2, true);
+#if USE_READY_PIN
while(m_rdy == 1) { } /* wait for rdy */
-// delay = getHDelay() ;
-// wait_us(1000 * delay) ;
-// wait(0.1) ;
+#else
+ delay = getHDelay() ;
+printf("Hume Delay = %.2f\n", delay) ;
+ wait_us(1000 * delay) ;
+#endif
m_i2c.read(m_addr, (char *)data, 2);
hume = (data[0] << 8) | data[1] ;
@@ -185,9 +177,11 @@
/* for mode 1 */
void HDC1000::getData(uint16_t *temp, uint16_t *hume)
{
- float delay ;
uint8_t data[4] = { 0, 0, 0, 0 } ;
int mode ;
+#if USE_READY_PIN
+ float delay ;
+#endif
mode = getMode() ;
if (mode == 0) {
@@ -197,13 +191,13 @@
data[0] = REG_TEMPERATURE ;
data[1] = (m_addr << 1) | 0x01 ;
m_i2c.write(m_addr,(const char *)data, 2, false);
-
+#if USE_READY_PIN
while(m_rdy == 1) { } /* wait for rdy */
-// delay = getTDelay() + getHDelay() ;
-// wait_us(1000 * delay) ;
-
-// m_i2c.write(m_addr,(const char *)data, 1, true);
-// while(m_rdy == 1) { } /* wait for rdy */
+#else
+ delay = getTDelay() + getHDelay() ;
+ printf("Delay = %.2f ms\n", delay) ;
+ wait( delay / 1000.0 ) ;
+#endif
m_i2c.read(m_addr, (char *)data, 4);
*temp = (data[0] << 8) | data[1] ;
*hume = (data[2] << 8) | data[3] ;
diff -r f2c04c5b28ab -r f574cd898cba HDC1000.h
--- a/HDC1000.h Mon Apr 17 07:37:19 2017 +0000
+++ b/HDC1000.h Tue Apr 18 02:44:57 2017 +0000
@@ -5,6 +5,8 @@
* HDC1000 Integrated Low Power Humidity and Temperature Digital Sensor
* I2C address: 0x40
*/
+
+#define USE_READY_PIN 1
class HDC1000 {
public:
@@ -13,23 +15,59 @@
*
* @param sda SDA pin
* @param scl SCL pin
+ * @param rdy Ready pin
* @param addr address of the I2C peripheral
*/
-// HDC1000(PinName sda, PinName scl, int addr=0x40) ;
+#if USE_READY_PIN
HDC1000(PinName sda, PinName scl, PinName rdy, int addr=0x40) ;
+#else
+HDC1000(PinName sda, PinName scl, int addr=0x40) ;
+#endif
~HDC1000() ;
+/**
+ * read Temperature
+ * @param NONE
+ * @returns float Temperature in Celsius degree
+ */
float readTemperature(void) ;
+
+/**
+ * read Humidity
+ * @param NONE
+ * @returns float Humidity in percentage
+ */
float readHumidity(void) ;
+/**
+ * reset the module
+ */
void reset(void) ;
float u2f_temp(uint16_t utemp) ;
float u2f_hume(uint16_t uhume) ;
+
/* for mode 0 */
+/**
+ * get Raw Temperature value
+ * @param NONE
+ * @reurns Raw Temperature value
+ */
uint16_t getTemperature(void) ;
+
+/**
+ * get Raw Humidity value
+ * @param NONE
+ * @returns Raw Humidity value
+ */
uint16_t getHumidity(void) ;
+
+/**
+ * read Temperature and Humidity together (mode==0)
+ * @param float *temp returns temperature
+ * @param float *hume returns humidity
+ */
void readData(float *temp, float *hume) ;
/* for mode 1 */
@@ -53,7 +91,9 @@
private:
I2C m_i2c;
+#if USE_READY_PIN
DigitalIn m_rdy ;
+#endif
int m_addr;
void readRegs(int addr, uint8_t * data, int len);
void writeRegs(uint8_t * data, int len);