AM2321 library
Dependents: mbed_AM2321_copy mbed_AM2321
See http://developer.mbed.org/users/yasuyuki/notebook/AM2321/
AM2321.cpp
- Committer:
- yasuyuki
- Date:
- 2015-07-10
- Revision:
- 1:39f20504d5c5
- Parent:
- 0:3656aea4e6f6
File content as of revision 1:39f20504d5c5:
//**********************
// 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);
}
unsigned short AM2321::humidity()
{
// get hum
get();
hum.byte.HB=buf[2];
hum.byte.LB=buf[3];
return hum.Val;
}
signed short AM2321::temperature()
{
// get temp
get();
temp.byte.HB=buf[4];
temp.byte.LB=buf[5];
if(temp.Val&0x8000){
temp.Val&=0x7FFF;
temp.Val=0xFFFF-temp.Val+1;
}
return temp.S;
}