下面的代码已详细注释DHT11的通信过程,想了解更多可看其数据手册
#include <iocc2530.h>
#include <stdio.h>
#include <string.h>
#define DATA_PIN P0_0 // DHT11 的数据引脚
typedef unsigned char uchar;
/* 定义一个结构体,用来存储DHT11传出来的40位数据 */
typedef struct{
uchar temp_H;
uchar temp_L;
uchar humi_H;
uchar humi_L;
uchar crc;
}DHT;
/* 微秒延时 */
void Delay_us(int time)
{
while(time--){
asm("NOP");
}
}
/* 毫秒延时 */
void Delay_ms(int Time)
{
while(Time--){
Delay_us(1000);
}
}
/* 定义DHT11的数据引脚为 输出 状态 */
void DHT_OUT(void){
P0SEL &= ~0x01;
P0DIR |= 0x01;
}
/* 定义DHT11的数据引脚为 输入 状态 */
void DHT_IN(void){
P0SEL &= ~0x01;
P0DIR &= ~0x01;
P0INP &= ~0x01;
P2INP &= ~0x20;
}
/* 读取一个字节的数据(在DHT11响应主机之后调用该函数) */
uchar