AM2320 library

Dependents:   mbed_AM2320 Fungi Fungi Nucleo_SSD1306_DS1302_ESP8266_AM2320_BME280 ... more

See http://developer.mbed.org/users/yasuyuki/notebook/AM2320/

AM2320.cpp

Committer:
yasuyuki
Date:
2015-03-14
Revision:
0:2cb35c995095
Child:
1:766868b34d56

File content as of revision 0:2cb35c995095:

//**********************
// AM2320.cpp for mbed
//
// AM2320 am2320(P0_5,P0_4);
// or
// I2C i2c(P0_5,P0_4);
// AM2320 am2320(i2c);
//
// (C)Copyright 2015 All rights reserved by Y.Onodera
// http://einstlab.web.fc2.com
//**********************

#include "mbed.h"
#include "AM2320.h"

AM2320::AM2320 (PinName sda, PinName scl) : _i2c(sda, scl) {
}
AM2320::AM2320 (I2C& p_i2c) : _i2c(p_i2c) {
}


void AM2320::get()
{
    // step 1:wakeup
    _i2c.write(AM2320_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(AM2320_ADDR, buf, 3);
    wait_us(1500);  // 1.5ms

    // step 3:data
    _i2c.read( AM2320_ADDR, buf, 8);

}

int AM2320::humidity()
{

    // get hum
    get();
    hum.byte.HB=buf[2];
    hum.byte.LB=buf[3];
    return hum.Val;
    
}

int AM2320::temperature()
{

    // get temp
    get();
    temp.byte.HB=buf[4];
    temp.byte.LB=buf[5];
    return temp.Val;
    
}