AM2321 Temperature and Humidity Sensor mbed library

Dependents:   AM2321_Example mbed_vfd_thermometer

/media/uploads/tomozh/am2321_1.png/media/uploads/tomozh/p5061476.jpg

Import programAM2321_Example

AM2321 Temperature and Humidity Sensor mbed library Example

Import library

Public Member Functions

AM2321 (PinName sda, PinName scl)
Constructor.
bool poll ()
Read current temperature and humidity from AM2321 .
float getTemperature (void) const
Get last read temperature value.
float getHumidity (void) const
Get last read humidity value.

AM2321.h

Committer:
tomozh
Date:
2014-05-06
Revision:
0:d1c0dbf5e5a6

File content as of revision 0:d1c0dbf5e5a6:

/**
 *  AM2321 (Aosong Guangzhou Electronics)
 *  Temperature and Humidity Sensor mbed library
 *  Last update : 2014/05/06
 */

#ifndef __AM2321_H__
#define __AM2321_H__

/** AM2321 (Aosong Guangzhou Electronics)
 * Temperature and Humidity Sensor mbed library
 * 
 * Example:
 * @code
 * #include "mbed.h"
 * #include "AM2321.h"
 *
 * Serial pc(USBTX, USBRX);    // Tx, Rx
 * AM2321 am2321(p28, p27);    // SDA, SCL
 *
 * int main()
 * {
 *     while(1)
 *     {
 *        if(am2321.poll())
 *         {
 *             pc.printf(":%05u,%.1f,%.1f\n"
 *                 , count++
 *                 , am2321.getTemperature()
 *                 , am2321.getHumidity()
 *             );
 *         }
 * 
 *         wait(0.5);
 *     }
 * }
 * @endcode
 */
class AM2321
{
private:
    typedef struct tagRESULT
    {
        float temperature;
        float humidity;
    }RESULT;

public:
    /** Constructor
     * @param   sda  [in]    I2C Pin name (SDA)
     * @param   scl  [in]    I2C Pin name (SCL)
     */
    AM2321(PinName sda, PinName scl);
    
    /** Read current temperature and humidity from AM2321
     * @return  result (true=success)
     */
    bool poll();

    /** Get last read temperature value
     * @return  temperature value (degress)
     */
    float getTemperature(void) const;
    
    /** Get last read humidity value
     * @return  humidity value (%RH)
     */ 
    float getHumidity(void) const;

private:
    float getLogicalValue(uint16_t regVal) const;
    uint16_t calcCRC16(const uint8_t* src, int len) const;

    I2C    _i2c;
    RESULT _result;
};


#endif // __AM2321_H__