//led
void led_set(void)//off
{
GPIO_WriteHigh(GPIOE,GPIO_PIN_5);
}
void led_reset(void)//on
{
GPIO_WriteLow(GPIOE,GPIO_PIN_5);
}
//1-wire
void data_set(void)//high
{
GPIO_WriteHigh(GPIOC,GPIO_PIN_4);
}
void data_reset(void)//low
{
GPIO_WriteLow(GPIOC,GPIO_PIN_4);
}
void Uart2_SendString(char *s)
{
while(*s)
{
UART2_SendByte(*s++);
}
}
void Read_DHT11_Byte(void)
{
u8 i;
u8 U8temp = 0;
for(i = 0; i < 8; i++)
{
U8temp = U8temp << 1;
//接收到响应后会出现50us的低电平表示发送数据的开始,所以这里只要延时不超过50us即可
delay_nus(10);
//等50ms低电平结束
while(!GPIO_ReadInputPin(GPIOC, GPIO_PIN_4));
//延时50us采样数据,因为26-28us表示0,70us表示1
//led_reset();delay_us();led_set();
delay_nus(50);
//下图中黄色波形就是此处LED的波形,用于展示读取数据的时机
//蓝色波形是DHT11输出
led_reset();delay_us();led_set();
if(GPIO_ReadInputPin(GPIOC, GPIO_PIN_4))
{
U8temp |= 0x01;
delay_nus(40);
}
else
{
U8temp &= 0xfe;
}
}
U8comdata = U8temp;
}
unsigned char Start_DHT11(void)
{
data_reset();
delay_ms(180);
data_set();
delay_nus(25);
if(!GPIO_ReadInputPin(GPIOC, GPIO_PIN_4))
{
//等低电平响应信号结束
while(!GPIO_ReadInputPin(GPIOC, GPIO_PIN_4));
//等高电平数据开始准备信号结束
while(GPIO_ReadInputPin(GPIOC, GPIO_PIN_4));
return 1;
}
return 0;
}
void Read_DHT11(void)
{
if(Start_DHT11())
{
Read_DHT11_Byte();
U8RH_data_H_temp = U8comdata;
Read_DHT11_Byte();
U8RH_data_L_temp = U8comdata;
Read_DHT11_Byte();
U8T_data_H_temp = U8comdata;
Read_DHT11_Byte();
U8T_data_L_temp = U8comdata;
Read_DHT11_Byte();
U8checkdata_temp = U8comdata;
data_set();
}
//要等到读完40个字节才能打印,否则读到的数据会出错,如果要打印输出将if(0)改为if(1)
if(0)
{
UART2_SendByte(U8RH_data_H_temp);
UART2_SendByte(U8RH_data_L_temp);
UART2_SendByte(U8T_data_H_temp);
UART2_SendByte(U8T_data_L_temp);
UART2_SendByte(U8checkdata_temp);
}
//用于校验读到的数
if(U8RH_data_H_temp + U8RH_data_L_temp + U8T_data_H_temp + U8T_data_L_temp == U8checkdata_temp)
{
U8RH_data_H = U8RH_data_H_temp;
U8T_data_H = U8T_data_H_temp;
}
}
|