LibDriver AM2320
Loading...
Searching...
No Matches
driver_am2320_read_test.c
Go to the documentation of this file.
1
36
38#include <stdlib.h>
39
40static am2320_handle_t gs_handle;
41
51uint8_t am2320_read_test(am2320_interface_t interface, uint32_t times)
52{
53 uint8_t res;
54 uint32_t i;
55 uint16_t temperature_raw;
56 uint16_t humidity_raw;
57 float temperature;
58 float humidity;
59 am2320_info_t info;
60
61 /* link interface function */
76
77 /* get am2320 information */
78 res = am2320_info(&info);
79 if (res != 0)
80 {
81 am2320_interface_debug_print("am2320: get info failed.\n");
82
83 return 1;
84 }
85 else
86 {
87 /* print am2320 information */
88 am2320_interface_debug_print("am2320: chip is %s.\n", info.chip_name);
89 am2320_interface_debug_print("am2320: manufacturer is %s.\n", info.manufacturer_name);
90 am2320_interface_debug_print("am2320: interface is %s.\n", info.interface);
91 am2320_interface_debug_print("am2320: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
92 am2320_interface_debug_print("am2320: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
93 am2320_interface_debug_print("am2320: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
94 am2320_interface_debug_print("am2320: max current is %0.2fmA.\n", info.max_current_ma);
95 am2320_interface_debug_print("am2320: max temperature is %0.1fC.\n", info.temperature_max);
96 am2320_interface_debug_print("am2320: min temperature is %0.1fC.\n", info.temperature_min);
97 }
98
99 /* start basic read test */
100 am2320_interface_debug_print("am2320: start read test.\n");
101
102 /* set interface */
103 res = am2320_set_interface(&gs_handle, interface);
104 if (res != 0)
105 {
106 am2320_interface_debug_print("am2320: set interface.\n");
107
108 return 1;
109 }
110
111 /* am2320 init */
112 res = am2320_init(&gs_handle);
113 if (res != 0)
114 {
115 am2320_interface_debug_print("am2320: init failed.\n");
116
117 return 1;
118 }
119
120 if (interface == AM2320_INTERFACE_IIC)
121 {
122 uint8_t status;
123 uint8_t version;
124 uint16_t type;
125 uint16_t reg;
126 uint16_t reg_check;
127 uint32_t id;
128
129 /* get device type */
130 res = am2320_get_device_type(&gs_handle, &type);
131 if (res != 0)
132 {
133 am2320_interface_debug_print("am2320: get device type failed.\n");
134 (void)am2320_deinit(&gs_handle);
135
136 return 1;
137 }
138
139 /* print result */
140 am2320_interface_debug_print("am2320: type is 0x%04X.\n", type);
141
142 /* delay 2000 ms*/
144
145 /* get version */
146 res = am2320_get_version(&gs_handle, &version);
147 if (res != 0)
148 {
149 am2320_interface_debug_print("am2320: get version failed.\n");
150 (void)am2320_deinit(&gs_handle);
151
152 return 1;
153 }
154
155 /* print result */
156 am2320_interface_debug_print("am2320: version is 0x%02X.\n", version);
157
158 /* delay 2000 ms*/
160
161 /* get status */
162 res = am2320_get_status(&gs_handle, &status);
163 if (res != 0)
164 {
165 am2320_interface_debug_print("am2320: get status failed.\n");
166 (void)am2320_deinit(&gs_handle);
167
168 return 1;
169 }
170
171 /* print result */
172 am2320_interface_debug_print("am2320: status is 0x%02X.\n", status);
173
174 /* delay 2000 ms*/
176
177 /* get device id */
178 res = am2320_get_device_id(&gs_handle, &id);
179 if (res != 0)
180 {
181 am2320_interface_debug_print("am2320: get device id failed.\n");
182 (void)am2320_deinit(&gs_handle);
183
184 return 1;
185 }
186
187 /* print result */
188 am2320_interface_debug_print("am2320: device id is 0x%08X.\n", id);
189
190 /* delay 2000 ms*/
192
193 reg = rand() % 0xFFFFUL;
194 res = am2320_set_user_reg1(&gs_handle, reg);
195 if (res != 0)
196 {
197 am2320_interface_debug_print("am2320: set user reg1 failed.\n");
198 (void)am2320_deinit(&gs_handle);
199
200 return 1;
201 }
202 am2320_interface_debug_print("am2320: set user reg1 0x%04X.\n", reg);
203
204 /* delay 2000 ms*/
206
207 /* get user reg1 */
208 res = am2320_get_user_reg1(&gs_handle, &reg_check);
209 if (res != 0)
210 {
211 am2320_interface_debug_print("am2320: get user reg1 failed.\n");
212 (void)am2320_deinit(&gs_handle);
213
214 return 1;
215 }
216
217 am2320_interface_debug_print("am2320: check reg1 %s.\n", reg == reg_check ? "ok" : "error");
218
219 /* delay 2000 ms*/
221
222 reg = rand() % 0xFFFFUL;
223 res = am2320_set_user_reg2(&gs_handle, reg);
224 if (res != 0)
225 {
226 am2320_interface_debug_print("am2320: set user reg2 failed.\n");
227 (void)am2320_deinit(&gs_handle);
228
229 return 1;
230 }
231 am2320_interface_debug_print("am2320: set user reg2 0x%04X.\n", reg);
232
233 /* delay 2000 ms*/
235
236 /* get user reg2 */
237 res = am2320_get_user_reg2(&gs_handle, &reg_check);
238 if (res != 0)
239 {
240 am2320_interface_debug_print("am2320: get user reg2 failed.\n");
241 (void)am2320_deinit(&gs_handle);
242
243 return 1;
244 }
245
246 am2320_interface_debug_print("am2320: check reg2 %s.\n", reg == reg_check ? "ok" : "error");
247 }
248
249 /* output */
250 am2320_interface_debug_print("am2320: read temperature and humidity.\n");
251
252 /* delay 2000 ms for read */
254
255 for (i = 0; i < times; i++)
256 {
257 /* read temperature and humidity */
258 res = am2320_read_temperature_humidity(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature,
259 (uint16_t *)&humidity_raw, (float *)&humidity);
260 if (res != 0)
261 {
262 am2320_interface_debug_print("am2320: read failed.\n");
263 (void)am2320_deinit(&gs_handle);
264
265 return 1;
266 }
267
268 /* print result */
269 am2320_interface_debug_print("am2320: temperature: %.01fC.\n", temperature);
270 am2320_interface_debug_print("am2320: humidity: %.01f%%.\n", humidity);
271
272 /* delay 2000 ms*/
274 }
275
276 /* output */
277 am2320_interface_debug_print("am2320: read temperature.\n");
278
279 /* delay 2000 ms for read */
281
282 for (i = 0; i < times; i++)
283 {
284 /* read temperature */
285 res = am2320_read_temperature(&gs_handle, (uint16_t *)&temperature_raw, (float *)&temperature);
286 if (res != 0)
287 {
288 am2320_interface_debug_print("am2320: read failed.\n");
289 (void)am2320_deinit(&gs_handle);
290
291 return 1;
292 }
293
294 /* print result */
295 am2320_interface_debug_print("am2320: temperature: %.01fC.\n", temperature);
296
297 /* delay 2000 ms*/
299 }
300
301 /* output */
302 am2320_interface_debug_print("am2320: read humidity.\n");
303
304 /* delay 2000 ms for read */
306
307 for (i = 0; i < times; i++)
308 {
309 /* read humidity */
310 res = am2320_read_humidity(&gs_handle, (uint16_t *)&humidity_raw, (float *)&humidity);
311 if (res != 0)
312 {
313 am2320_interface_debug_print("am2320: read failed.\n");
314 (void)am2320_deinit(&gs_handle);
315
316 return 1;
317 }
318
319 /* print result */
320 am2320_interface_debug_print("am2320: humidity: %.01f%%.\n", humidity);
321
322 /* delay 2000 ms*/
324 }
325
326 /* finish read test and exit */
327 am2320_interface_debug_print("am2320: finish read test.\n");
328 (void)am2320_deinit(&gs_handle);
329
330 return 0;
331}
driver am2320 read test header file
uint8_t am2320_set_user_reg1(am2320_handle_t *handle, uint16_t reg)
set user reg1
uint8_t am2320_get_version(am2320_handle_t *handle, uint8_t *version)
get version
uint8_t am2320_get_device_type(am2320_handle_t *handle, uint16_t *type)
get device type
struct am2320_handle_s am2320_handle_t
am2320 handle structure definition
uint8_t am2320_init(am2320_handle_t *handle)
initialize the chip
uint8_t am2320_read_temperature(am2320_handle_t *handle, uint16_t *raw, float *s)
read the temperature data
uint8_t am2320_read_temperature_humidity(am2320_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read the temperature and humidity data
am2320_interface_t
am2320 interface enumeration definition
uint8_t am2320_set_user_reg2(am2320_handle_t *handle, uint16_t reg)
set user reg2
uint8_t am2320_get_device_id(am2320_handle_t *handle, uint32_t *id)
get device id
uint8_t am2320_get_status(am2320_handle_t *handle, uint8_t *status)
get status
uint8_t am2320_get_user_reg1(am2320_handle_t *handle, uint16_t *reg)
get user reg1
uint8_t am2320_read_humidity(am2320_handle_t *handle, uint16_t *raw, float *s)
read the humidity data
uint8_t am2320_deinit(am2320_handle_t *handle)
close the chip
uint8_t am2320_info(am2320_info_t *info)
get chip's information
uint8_t am2320_set_interface(am2320_handle_t *handle, am2320_interface_t interface)
set the chip interface
struct am2320_info_s am2320_info_t
am2320 info structure definition
uint8_t am2320_get_user_reg2(am2320_handle_t *handle, uint16_t *reg)
get user reg2
@ AM2320_INTERFACE_IIC
uint8_t am2320_interface_iic_deinit(void)
interface iic deinit
void am2320_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t am2320_interface_bus_read(uint8_t *value)
interface bus read
uint8_t am2320_interface_iic_read_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic read command
uint8_t am2320_interface_iic_write_cmd(uint8_t addr, uint8_t *buf, uint16_t len)
interface iic write command
void am2320_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t am2320_interface_bus_deinit(void)
interface bus deinit
void am2320_interface_delay_us(uint32_t us)
interface delay us
void am2320_interface_disable_irq(void)
interface disable the interrupt
uint8_t am2320_interface_iic_init(void)
interface iic init
void am2320_interface_enable_irq(void)
interface enable the interrupt
uint8_t am2320_interface_bus_init(void)
interface bus init
uint8_t am2320_interface_bus_write(uint8_t value)
interface bus write
uint8_t am2320_read_test(am2320_interface_t interface, uint32_t times)
read test
float supply_voltage_max_v
uint32_t driver_version
char interface[16]
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]