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 "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define DHT222_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
55  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
56  cfg.ow = MIKROBUS( mikrobus, MIKROBUS_PWM )
57 
63 #define DHT222_RETVAL uint8_t
64 
65 #define DHT222_OK 0x00
66 #define DHT222_INIT_ERROR 0xFF
67 
73 #define DHT222_I2C_ADDRESS 0x5C
74 
80 #define DHT222_HUMIDITY_DATA_REG 0x00
81 #define DHT222_TEMPERATURE_DATA_REG 0x02
82 #define DHT222_READ_REG 0x03
83 #define DHT222_MODEL_DATA_REG 0x08
84 #define DHT222_USERS_REG_1 0x10
85 #define DHT222_USERS_REG_2 0x12
86 #define DHT222_VERSION_DATA_REG 0x0A
87 #define DHT222_ID_DATA_REG 0x0B
88 
94 #define DHT222_WAKE_DATA_CMD 0x00
95 #define DHT222_READ_REQUEST_4_BYTE_CMD 0x04
96 #define DHT222_READ_REQUEST_2_BYTE_CMD 0x02
97 #define DHT222_READ_REQUEST_1_BYTE_CMD 0x01
98  // End group macro
101 // --------------------------------------------------------------- PUBLIC TYPES
110 typedef struct
111 {
112  // Input pins
113 
114  digital_in_t ow;
115 
116  // Modules
117 
118  i2c_master_t i2c;
119 
120  // ctx variable
121 
122  uint8_t slave_address;
123 
124 } dht222_t;
125 
129 typedef struct
130 {
131  // Communication gpio pins
132 
133  pin_name_t scl;
134  pin_name_t sda;
135 
136  // Additional gpio pins
137 
138  pin_name_t ow;
139 
140  // static variable
141 
142  uint32_t i2c_speed;
143  uint8_t i2c_address;
144 
145 } dht222_cfg_t;
146  // End types group
148 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
149 
155 #ifdef __cplusplus
156 extern "C"{
157 #endif
158 
168 
179 
190 void dht222_generic_write ( dht222_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
191 
202 void dht222_generic_read ( dht222_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
203 
214 uint8_t dht222_read_byte ( dht222_t *ctx, uint8_t address );
215 
226 uint16_t dht222_read_data ( dht222_t *ctx, uint8_t address );
227 
238 uint32_t dht222_read_all_data ( dht222_t *ctx, uint8_t address );
239 
255 
270 uint16_t dht222_get_humidity ( dht222_t *ctx );
271 
280 uint16_t dht222_get_model ( dht222_t *ctx );
281 
292 uint16_t dht222_get_users_reg1 ( dht222_t *ctx );
293 
304 uint16_t dht222_get_users_reg2 ( dht222_t *ctx );
305 
316 uint8_t dht222_get_version ( dht222_t *ctx );
317 
328 uint32_t dht222_get_id ( dht222_t *ctx );
329 
330 #ifdef __cplusplus
331 }
332 #endif
333 #endif // _DHT222_H_
334  // End public_function group
337 
338 // ------------------------------------------------------------------------- END
dht222_cfg_t::sda
pin_name_t sda
Definition: dht222.h:134
dht222_get_model
uint16_t dht222_get_model(dht222_t *ctx)
Get model of AM2322 sensor.
dht222_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: dht222.h:142
dht222_get_temperature
uint16_t dht222_get_temperature(dht222_t *ctx)
Reads temperature from AM2322 sensor.
dht222_generic_write
void dht222_generic_write(dht222_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
dht222_t
Click ctx object definition.
Definition: dht222.h:111
dht222_get_version
uint8_t dht222_get_version(dht222_t *ctx)
Get version of AM2322 sensor.
dht222_t::ow
digital_in_t ow
Definition: dht222.h:114
dht222_read_all_data
uint32_t dht222_read_all_data(dht222_t *ctx, uint8_t address)
Reads 32-bit value from given address.
dht222_read_data
uint16_t dht222_read_data(dht222_t *ctx, uint8_t address)
Generic read 16-bit data function.
dht222_get_users_reg2
uint16_t dht222_get_users_reg2(dht222_t *ctx)
Get users register 2 of AM2322 sensor.
dht222_cfg_t
Click configuration structure definition.
Definition: dht222.h:130
dht222_get_id
uint32_t dht222_get_id(dht222_t *ctx)
Get ID of AM2322 sensor.
DHT222_RETVAL
#define DHT222_RETVAL
Definition: dht222.h:63
dht222_generic_read
void dht222_generic_read(dht222_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
dht222_init
DHT222_RETVAL dht222_init(dht222_t *ctx, dht222_cfg_t *cfg)
Initialization function.
dht222_cfg_setup
void dht222_cfg_setup(dht222_cfg_t *cfg)
Config Object Initialization function.
dht222_read_byte
uint8_t dht222_read_byte(dht222_t *ctx, uint8_t address)
Generic read 8-bit data function.
dht222_get_users_reg1
uint16_t dht222_get_users_reg1(dht222_t *ctx)
Get users register 1 of AM2322 sensor.
dht222_cfg_t::i2c_address
uint8_t i2c_address
Definition: dht222.h:143
dht222_cfg_t::ow
pin_name_t ow
Definition: dht222.h:138
dht222_cfg_t::scl
pin_name_t scl
Definition: dht222.h:133
dht222_t::slave_address
uint8_t slave_address
Definition: dht222.h:122
dht222_t::i2c
i2c_master_t i2c
Definition: dht222.h:118
dht222_get_humidity
uint16_t dht222_get_humidity(dht222_t *ctx)
Reads humidity from AM2322 sensor.