准备资料
部分代码
read_dht.c
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "dht/DHT.h"
static void DHT22_InitX(void)
{
float temperature = 0;
float humidity = 0;
// GPIO 初始化
GpioInit();
// 初始化 DHT 引脚(设置引脚功能为GPIO:有些引脚是复合功能)
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_GPIO);
usleep(1000000);
while(DHT22_Init()){
usleep(500000);
printf("DHT22_Check Error !\r\n");
}
DHT22_Read_Data(&temperature, &humidity);
while (1)
{
usleep(1000000);
usleep(1000000);
DHT22_Read_Data(&temperature, &humidity);
printf("\r\ntemperrature = %.2f\r\n", temperature);
printf("humidity = %.2f\r\n", humidity);
}
}
APP_FEATURE_INIT(DHT22_InitX);
DHT.c
#include "DHT.h"
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include <stdbool.h>
int pinNo = WIFI_IOT_IO_NAME_GPIO_14;
//复位DHT22
void DHT22_Rst(void)
{
GpioSetDir(pinNo, WIFI_IOT_GPIO_DIR_OUT);
GpioSetOutputVal(pinNo, 0);
//拉低至少18ms
usleep(30000);
//DQ=1
GpioSetOutputVal(pinNo, 1);
usleep(30); //主机拉高20~40us
}
//等待DHT22的回应
//返回0:未检测到DHT22的存在
//返回1:存在
uint8_t DHT22_Check(void)
{
uint8_t retry = 0;
WifiIotGpioValue inputVal = {0};
GpioSetDir(pinNo, WIFI_IOT_GPIO_DIR_IN);
while (!inputVal&&retry < 100) //DHT22会拉低40~80us
{
retry++;
usleep(1);
GpioGetInputVal(pinNo, &inputVal);
};
if (retry >= 100)
return 0; // check error
else
retry = 0;
while (inputVal && retry < 100) //DHT22拉低后会再次拉高40~80us
{
retry++;
usleep(1);
GpioGetInputVal(pinNo, &inputVal);
};
if (retry >= 100)
return 0; // check error
return 1;
}
//从DHT22读取一个位
//返回值:1/0
uint8_t DHT22_Read_Bit(void)
{
uint8_t retry = 0;
WifiIotGpioValue val = {0};
while (val && retry < 100) //等待变为低电平
{
retry++;
usleep(1);
GpioGetInputVal(pinNo, &val);
}
retry = 0;
while (!val && retry < 100) //等待变高电平
{
retry++;
usleep(1);
GpioGetInputVal(pinNo, &val);
}
usleep(40); //等待40us
GpioGetInputVal(pinNo, &val);
if (val)
{
return 1;
}
else
{
return 0;
}
}
//从DHT22读取一个字节
//返回值:读到的数据
uint8_t DHT22_Read_Byte(void)
{
uint8_t i, dat;
dat = 0;
for (i = 0; i < 8; i++)
{
dat<<=1;
dat|=DHT22_Read_Bit();
}
return dat;
}
//从DHT22读取一次数据
//temp:温度值(范围:0~50°)
//humi:湿度值(范围:20%~90%)
//返回值:0,正常;1,读取失败
void DHT22_Read_Data(float *temperature, float *humidity)
{
uint8_t buf[5];
uint8_t i;
uint8_t sum;
*humidity=0;
*temperature=0;
DHT22_Rst();
uint8_t check = DHT22_Check();
printf("check %d",check);
if (check)
{
for (i = 0; i < 5; i++) //读取40位数据
{
buf[i] = DHT22_Read_Byte();
}
//校验和累加时存在相当概率会超出8位(即值大于1个char,故做强制类型转换,只考虑低8位)
sum = buf[0] + buf[1] + buf[2] + buf[3];
if (sum == buf[4])
{
*humidity = (float)((buf[0] << 8) + buf[1]) / 10;
*temperature = (float)((buf[2] << 8) + buf[3]) / 10;
}
else
{
*humidity = (float)((buf[0] << 8) + buf[1]) / 10;
*temperature = (float)((buf[2] << 8) + buf[3]) / 10;
}
}
}
//初始化DHT22的IO口 DQ 同时检测DHT22的存在
//返回0:未检测到DHT22的存在
//返回1:存在
uint8_t DHT22_Init(void)
{
GpioSetDir(pinNo, WIFI_IOT_GPIO_DIR_OUT);
GpioSetOutputVal(pinNo, 0);
DHT22_Rst();
return DHT22_Check();
}
DHT.h
#ifndef __DHT22_H
#define __DHT22_H
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
uint8_t DHT22_Init(void); //初始化DHT22
void DHT22_Read_Data(float *temperature, float *humidity); //读取温湿度
uint8_t DHT22_Read_Byte(void); //读出一个字节
uint8_t DHT22_Read_Bit(void); //读出一个位
uint8_t DHT22_Check(void); //检测是否存在DHT22
void DHT22_Rst(void); //复位DHT22
#endif