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 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
61 #define DHT222_MAP_MIKROBUS( cfg, mikrobus ) \
62  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
63  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA );
64 
70 #define DHT222_OK 0
71 #define DHT222_ERROR -1
72 
78 #define DHT222_I2C_ADDRESS_WRITE 0xB8
79 #define DHT222_I2C_ADDRESS_READ 0xB9
80 
86 #define DHT222_REG_HUMIDITY_MSB 0x00
87 #define DHT222_REG_HUMIDITY_LSB 0x01
88 #define DHT222_REG_TEMPERATURE_MSB 0x02
89 #define DHT222_REG_TEMPERATURE_LSB 0x03
90 #define DHT222_REG_MODEL_MSB 0x08
91 #define DHT222_REG_MODEL_LSB 0x09
92 #define DHT222_REG_VERSION 0x0A
93 #define DHT222_REG_DEVICE_ID_3 0x0B
94 #define DHT222_REG_DEVICE_ID_2 0x0C
95 #define DHT222_REG_DEVICE_ID_1 0x0D
96 #define DHT222_REG_DEVICE_ID_0 0x0E
97 #define DHT222_REG_STATE 0x0F
98 #define DHT222_REG_USER_REG_1_MSB 0x10
99 #define DHT222_REG_USER_REG_1_LSB 0x11
100 #define DHT222_REG_USER_REG_2_MSB 0x12
101 #define DHT222_REG_USER_REG_2_LSB 0x13
102 
108 #define DHT222_CMD_READ 0x03
109 #define DHT222_CMD_WRITE 0x10
110 
116 #define DHT222_TIMEOUT 100000ul
117  // End group macro
120 // --------------------------------------------------------------- PUBLIC TYPES
129 typedef struct
130 {
131  pin_name_t scl;
132  pin_name_t sda;
133 
134  uint8_t i2c_started;
135 
136 } dht222_t;
137 
141 typedef struct
142 {
143  pin_name_t scl;
144  pin_name_t sda;
145 
146 } dht222_cfg_t;
147  // End types group
149 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
150 
156 #ifdef __cplusplus
157 extern "C"{
158 #endif
159 
169 
181 err_t dht222_init ( dht222_t *ctx, dht222_cfg_t *cfg );
182 
196 err_t dht222_read_reg ( dht222_t *ctx, uint8_t address, uint8_t *data_out, uint8_t len );
197 
211 err_t dht222_write_reg ( dht222_t *ctx, uint8_t address, uint8_t *data_in, uint8_t len );
212 
227 err_t dht222_get_temperature ( dht222_t *ctx, uint16_t *temperature );
228 
243 err_t dht222_get_humidity ( dht222_t *ctx, uint16_t *humidity );
244 
260 err_t dht222_get_temp_hum ( dht222_t *ctx, uint16_t *temperature, uint16_t *humidity );
261 
262 #ifdef __cplusplus
263 }
264 #endif
265 #endif // _DHT222_H_
266  // End public_function group
269 
270 // ------------------------------------------------------------------------- END
dht222_cfg_t::sda
pin_name_t sda
Definition: dht222.h:144
dht222_t
Click ctx object definition.
Definition: dht222.h:130
dht222_get_temperature
err_t dht222_get_temperature(dht222_t *ctx, uint16_t *temperature)
Reads temperature from AM2322 sensor.
dht222_get_humidity
err_t dht222_get_humidity(dht222_t *ctx, uint16_t *humidity)
Reads humidity from AM2322 sensor.
dht222_read_reg
err_t dht222_read_reg(dht222_t *ctx, uint8_t address, uint8_t *data_out, uint8_t len)
Generic read function.
dht222_cfg_t
Click configuration structure definition.
Definition: dht222.h:142
dht222_write_reg
err_t dht222_write_reg(dht222_t *ctx, uint8_t address, uint8_t *data_in, uint8_t len)
Generic write function.
dht222_t::i2c_started
uint8_t i2c_started
Definition: dht222.h:134
dht222_cfg_setup
void dht222_cfg_setup(dht222_cfg_t *cfg)
Config Object Initialization function.
dht222_get_temp_hum
err_t dht222_get_temp_hum(dht222_t *ctx, uint16_t *temperature, uint16_t *humidity)
Reads temperature and humidity from AM2322 sensor.
dht222_init
err_t dht222_init(dht222_t *ctx, dht222_cfg_t *cfg)
Initialization function.
dht222_t::sda
pin_name_t sda
Definition: dht222.h:132
dht222_cfg_t::scl
pin_name_t scl
Definition: dht222.h:143
dht222_t::scl
pin_name_t scl
Definition: dht222.h:131