dht222 2.0.0.0
dht222.h
Go to the documentation of this file.
1/*
2 * MikroSDK - MikroE Software Development Kit
3 * Copyright© 2020 MikroElektronika d.o.o.
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 * OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
33// ----------------------------------------------------------------------------
34
35#ifndef DHT222_H
36#define DHT222_H
37
42#ifdef PREINIT_SUPPORTED
43#include "preinit.h"
44#endif
45
46#ifdef MikroCCoreVersion
47 #if MikroCCoreVersion >= 1
48 #include "delays.h"
49 #endif
50#endif
51
52#include "drv_digital_out.h"
53#include "drv_digital_in.h"
54
55// -------------------------------------------------------------- PUBLIC MACROS
65#define DHT222_MAP_MIKROBUS( cfg, mikrobus ) \
66 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
67 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA );
74#define DHT222_OK 0
75#define DHT222_ERROR -1
82#define DHT222_I2C_ADDRESS_WRITE 0xB8
83#define DHT222_I2C_ADDRESS_READ 0xB9
90#define DHT222_REG_HUMIDITY_MSB 0x00
91#define DHT222_REG_HUMIDITY_LSB 0x01
92#define DHT222_REG_TEMPERATURE_MSB 0x02
93#define DHT222_REG_TEMPERATURE_LSB 0x03
94#define DHT222_REG_MODEL_MSB 0x08
95#define DHT222_REG_MODEL_LSB 0x09
96#define DHT222_REG_VERSION 0x0A
97#define DHT222_REG_DEVICE_ID_3 0x0B
98#define DHT222_REG_DEVICE_ID_2 0x0C
99#define DHT222_REG_DEVICE_ID_1 0x0D
100#define DHT222_REG_DEVICE_ID_0 0x0E
101#define DHT222_REG_STATE 0x0F
102#define DHT222_REG_USER_REG_1_MSB 0x10
103#define DHT222_REG_USER_REG_1_LSB 0x11
104#define DHT222_REG_USER_REG_2_MSB 0x12
105#define DHT222_REG_USER_REG_2_LSB 0x13
112#define DHT222_CMD_READ 0x03
113#define DHT222_CMD_WRITE 0x10
120#define DHT222_TIMEOUT 100000ul
123 // End group macro
124// --------------------------------------------------------------- PUBLIC TYPES
133typedef struct
134{
135 pin_name_t scl;
136 pin_name_t sda;
137
138 uint8_t i2c_started;
139
140} dht222_t;
141
145typedef struct
146{
147 pin_name_t scl;
148 pin_name_t sda;
149
151
152 // End types group
153// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
154
160#ifdef __cplusplus
161extern "C"{
162#endif
163
173
185err_t dht222_init ( dht222_t *ctx, dht222_cfg_t *cfg );
186
200err_t dht222_read_reg ( dht222_t *ctx, uint8_t address, uint8_t *data_out, uint8_t len );
201
215err_t dht222_write_reg ( dht222_t *ctx, uint8_t address, uint8_t *data_in, uint8_t len );
216
231err_t dht222_get_temperature ( dht222_t *ctx, uint16_t *temperature );
232
247err_t dht222_get_humidity ( dht222_t *ctx, uint16_t *humidity );
248
264err_t dht222_get_temp_hum ( dht222_t *ctx, uint16_t *temperature, uint16_t *humidity );
265
266#ifdef __cplusplus
267}
268#endif
269#endif // _DHT222_H_
270
271 // End public_function group
273
274// ------------------------------------------------------------------------- END
void dht222_cfg_setup(dht222_cfg_t *cfg)
Config Object Initialization function.
err_t dht222_init(dht222_t *ctx, dht222_cfg_t *cfg)
Initialization function.
err_t dht222_get_temp_hum(dht222_t *ctx, uint16_t *temperature, uint16_t *humidity)
Reads temperature and humidity from AM2322 sensor.
err_t dht222_get_humidity(dht222_t *ctx, uint16_t *humidity)
Reads humidity from AM2322 sensor.
err_t dht222_write_reg(dht222_t *ctx, uint8_t address, uint8_t *data_in, uint8_t len)
Generic write function.
err_t dht222_read_reg(dht222_t *ctx, uint8_t address, uint8_t *data_out, uint8_t len)
Generic read function.
err_t dht222_get_temperature(dht222_t *ctx, uint16_t *temperature)
Reads temperature from AM2322 sensor.
Click configuration structure definition.
Definition dht222.h:146
pin_name_t scl
Definition dht222.h:147
pin_name_t sda
Definition dht222.h:148
Click ctx object definition.
Definition dht222.h:134
uint8_t i2c_started
Definition dht222.h:138
pin_name_t scl
Definition dht222.h:135
pin_name_t sda
Definition dht222.h:136