你好,我正努力让DH11工作正常。我在代码中尽可能多地固定,但是我一直都有奇偶校验错误,我尝试从在线代码中遵循不同的风格,我也从以前的NEC解码策略中得到了很多。但是这里我需要一些帮助。它提到了传感器需要一个上拉电阻,但是我读了一个SMD的传感器,所以我没有连接任何一个,即使我连接了一个,结果也不会有太大的变化。这是我的代码:这是一个读取每一个8位数据:这是主要功能:注:我是Coun。
tiN奇偶错误部分,因为我一直都有错误,函数返回1。
以上来自于百度翻译
以下为原文
Hello,
I'm trying to get the DH11 working precisely. I fixed as much as I could in the code, but I still get parity error all the time.
I tried to follow different styles from online codes, I also got a lot from my previous NEC decoding strategy. But here I need some help.
It's mentioned that the sensor need a pull-up resistor but I read the breakout sensor has an SMD one, so I'm not connecting any, even if I connect one the result doesn't change much.
Here's my code:
This one to read each 8-bits data:
uint8_t DH11_read_8bits(void)
{
DH11_code = 0;
for (i=7;i>=0;i--)
{
while(!PORTDbits.RD1); // wait for 0 leading bit
TMR2 = 0;
while(PORTDbits.RD1); // high bit 0 or 1
TMR2_temp = TMR2;
if((TMR2_temp < 40) && (TMR2_temp > 20))// 0 bit is 26-28us
DH11_code &= ~(1<<(i));
else
DH11_code |= (1<<(i));
}
return DH11_code;
}
This is the main function:
Note: I'm commenting the parity error part because I get error all the time and the function returns 1.
uint8_t DH11_read(void)
{
TRISDbits.RD1=0;
LATDbits.LATD1=0;
__delay_ms(18); // user 18ms low
LATDbits.LATD1=1;
__delay_us(40); // user 40us high
TRISDbits.RD1=1;
while(!PORTDbits.RD1);// sensor response 80us low
while(PORTDbits.RD1);// sensor response 80us high
HumHigh = DH11_read_8bits();
HumDeci = DH11_read_8bits();
TempHigh = DH11_read_8bits();
TempDeci = DH11_read_8bits();
parity_sum = DH11_read_8bits();
parity_check = HumHigh+HumDeci+TempHigh+TempDeci;
/*if (parity_check != parity_sum)
{
LCD_string("parity error");
__delay_ms(500);
sendCMD(clear_display);
return 1;
}*/
LCD_string("Humi: ");
move_cursor(1,6);
sprintf(val_arr,"%.2d",HumHigh);
LCD_string(val_arr);
move_cursor(1,8);
sprintf(val_arr,".%.2d",HumDeci);
LCD_string(val_arr);
LCD_string("%");
move_cursor(2,0);
LCD_string("Temp: ");
move_cursor(2,6);
sprintf(val_arr,"%.2d",TempHigh);
LCD_string(val_arr);
move_cursor(2,8);
sprintf(val_arr,".%.2d",TempDeci);
LCD_string(val_arr);
move_cursor(2,11);sendData(0xDF);
move_cursor(2,12);sendData(0x43);
move_cursor(2,13);
__delay_ms(1000);
sendCMD(clear_display);
}