|
首先感谢进来看的朋友,表示感谢。请问我这个DHT11数码管显示,为什么数码管只显示最后一位?
主函数:
#include<reg52.h>
#include"dht11.h"
#include"delay.h"
#include"uart.h"
sbit B1 = P2^2;
sbit B2 = P2^1;
sbit B3 = P2^0;
unsigned char tempH,tempL,humiH,humiL;
unsigned char Temp_Humi[4];
unsigned char code Temp_Table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void Show_temp(void)
{
uint i=0;
Temp_Humi[0] = tempH/10;
Temp_Humi[1] = tempH%10;
Temp_Humi[2] = humiH/10;
Temp_Humi[3] = humiH%10;
for(i=0;i<4;i++)
{
switch(i)
{
case (0) : B1 = 0 ;B2 = 0 ;B3 = 0 ; break;
case (1) : B1 = 0 ;B2 = 0 ;B3 = 1 ; break;
case (2) : B1 = 0 ;B2 = 1 ;B3 = 0 ; break;
case (3) : B1 = 0 ;B2 = 1 ;B3 = 1 ; break;
}
P0 = Temp_Table[Temp_Humi[i]];
Delay40us();
}
}
int main(void)
{
Timer0Init();
UartInit();
Dht11_Star();
if(Dht11_Check()<0)
printf("error! \r\n");
else
printf("ok! \r\n");
while(1)
{
Dht11_Read_Dat(&tempL,&tempH,&humiH,&humiL);
Delay4ms();
Show_temp();
printf("temp: %bu%bu humi: %bu%bu\r\n",Temp_Humi[0],Temp_Humi[1],Temp_Humi[2],Temp_Humi[3]);
}
}
DHT11时序读取
#include<intrins.h>
#include"dht11.h"
#include"delay.h"
void Dht11_Star(void)
{
DATE = 0;
Delay20ms();
DATE = 1;
Delay30us();
}
char Dht11_Check(void)
{
uint t = 0;
while(DATE)
{
t++;
_nop_();
if(t>4000)
{
return -1;
}
}
t=0;
while(!DATE)
{
t++;
_nop_();
if(t>100)
return -2;
}
t=0;
while(DATE)
{
t++;
_nop_();
if(t>100)
return -3;
}
return 0;
}
uchar Dht11_Read(void)
{
uint i,t=0;
uchar temp=0;
for(i=0;i<8;i++)
{
while(!DATE)
{
t++;
_nop_();
if(t>60)
return -1;
}
Delay40us();
temp <<= 1;
if(DATE)
{
temp |= 1;
t=0;
while(DATE)
{
t++;
_nop_();
if(t>80)
return -1;
}
}
}
return temp;
}
short Dht11_Read_Dat(char *tempL,char *tempH,char *humiH,char *humiL)
{
uchar i;
uchar buff[5];
Dht11_Star();
if(Dht11_Check() == 0)
{
for(i=0;i<5;i++)
{
buff[i]= Dht11_Read();
}
Delay40us();
if((buff[0]+buff[1]+buff[2]+buff[3]) == buff[4])
{
*humiH=buff[0];
*humiL=buff[1];
*tempH=buff[2];
*tempL=buff[3];
return 0;
}
else
return -1;
}
else
return -2;
}
|
|