Files

87 lines
1.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "dht10.h"
uint16_t dht10_data_temperature; // 温度 100 倍
uint16_t dht10_data_humidity; // 湿度 100 倍
void
DHT10_Init ()
{
DelayUs (1);
}
void
DHT10_ReadData ()
{
uint8_t i;
uint8_t buffer[6] = { 0 };
uint32_t hum_or_temp_raw;
// 发送启动测量命令
IIC_Start ();
IIC_SendByte (DHT10_ADDR << 1); // 发送设备地址+写
// 发送命令字节
IIC_SendByte (0xAC);
IIC_SendByte (0x33);
IIC_SendByte (0x00);
IIC_Stop ();
// 等待测量完成DHT10需至少75ms
DelayMs (80);
// 读取6字节数据
IIC_Start ();
IIC_SendByte ((DHT10_ADDR << 1) | 1);
for (i = 0; i < 6; i++)
{
buffer[i] = IIC_RecvByte ();
// 前5字节发送Ack第6字节发送NAck
if (i < 5)
IIC_SendAck (1);
else
IIC_SendAck (0);
}
IIC_Stop ();
// 校验和前5字节之和的低8位
i = buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4];
if (i != buffer[5])
{
return;
}
// 解析20位湿度数据buffer[2]高4位为湿度数据
hum_or_temp_raw = ((uint32_t)buffer[0] << 12) | ((uint32_t)buffer[1] << 4)
| (buffer[2] >> 4);
// 转换为实际值
// dht10_data->humidity = (float)hum_raw / 10.0f; // 湿度 实际值
dht10_data_humidity = (uint16_t)(hum_or_temp_raw * 10.0f); // 湿度 100倍
// 解析20位温度数据buffer[5]高4位为温度数据
hum_or_temp_raw = ((uint32_t)buffer[3] << 12) | ((uint32_t)buffer[4] << 4)
| (buffer[5] >> 4);
dht10_data_temperature
= (uint16_t)(hum_or_temp_raw * 10.0f) - 4000; // 温度 100倍
// dht10_data->temperature = (float)temp_raw / 10.0f - 40.0f; // 温度
// 实际值
return;
}
uint16_t
DHT10_GetTemperature ()
{
return dht10_data_temperature;
}
uint16_t
DHT10_GetHumidity ()
{
return dht10_data_humidity;
}