AM2321 library
Dependents: mbed_AM2321_copy mbed_AM2321
See http://developer.mbed.org/users/yasuyuki/notebook/AM2321/
Diff: AM2321.cpp
- Revision:
- 0:3656aea4e6f6
- Child:
- 1:39f20504d5c5
diff -r 000000000000 -r 3656aea4e6f6 AM2321.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AM2321.cpp Wed Oct 08 02:48:36 2014 +0000 @@ -0,0 +1,61 @@ +//********************** +// AM2321.cpp for mbed +// +// AM2321 am2321(P0_5,P0_4); +// or +// I2C i2c(P0_5,P0_4); +// AM2321 am2321(i2c); +// +// (C)Copyright 2014 All rights reserved by Y.Onodera +// http://einstlab.web.fc2.com +//********************** + +#include "mbed.h" +#include "AM2321.h" + +AM2321::AM2321 (PinName sda, PinName scl) : _i2c(sda, scl) { +} +AM2321::AM2321 (I2C& p_i2c) : _i2c(p_i2c) { +} + + +void AM2321::get() +{ + // step 1:wakeup + _i2c.write(AM2321_ADDR, NULL, 0); + wait_us(800); // 800us - 3ms + + // step 2:command + buf[0] = 0x03; // Get Humidity and Temperature + buf[1] = 0x00; // Start address + buf[2] = 0x04; // Length + _i2c.write(AM2321_ADDR, buf, 3); + wait_us(1500); // 1.5ms + + // step 3:data + _i2c.read( AM2321_ADDR, buf, 8); + +} + +int AM2321::humidity() +{ + + // get hum + get(); + hum.byte.HB=buf[2]; + hum.byte.LB=buf[3]; + return hum.Val; + +} + +int AM2321::temperature() +{ + + // get temp + get(); + temp.byte.HB=buf[4]; + temp.byte.LB=buf[5]; + return temp.Val; + +} +