staticintdht11_recv_byte(void) { int i; int data = 0; for (i = 0; i < 8; i++) { if (dht11_wait_for_val(1, 1000)) { printf("dht11 wait for high data err!\n\r"); return-1; } udelay(40); data <<= 1; if (dht11_data_get() == 1) data |= 1; if (dht11_wait_for_val(0, 1000)) { printf("dht11 wait for low data err!\n\r"); return-1; } } return data; }
staticintdht11_wait_for_val(int val, int timeout_us) { while (timeout_us--) { if (dht11_data_get() == val) return0; /* ok */ udelay(1); } return-1; /* err */ }
intdht11_read(int *hum, int *temp) { unsignedchar hum_m, hum_n; unsignedchar temp_m, temp_n; unsignedchar check; dht11_start(); if (0 != dht11_wait_ack()) { printf("dht11 not ack, err!\n\r"); return-1; }
if (0 != dht11_wait_for_val(1, 1000)) { /* 等待ACK变为高电平, 超时时间是1000us */ printf("dht11 wait for ack high err!\n\r"); return-1; }
if (0 != dht11_wait_for_val(0, 1000)) { /* 数据阶段: 等待低电平, 超时时间是1000us */ printf("dht11 wait for data low err!\n\r"); return-1; }