Home > other >  Msp430 DHT11
Msp430 DHT11

Time:10-04

Using DHt11 temperature and humidity measurement, not an error, but the temperature can't read it out, please everyone a great god and see what is wrong
#include

# define DHT11_OUTPUT P2DIR |=BIT0//P2.0
# define DHT11_INPUT P2DIR & amp;=~ BIT0//P2.0
# define DHT11_H P2OUT |=BIT0//P2.0
# define DHT11_L P2OUT & amp;=~ BIT0//P2.0
# define DHT11_IN (P2IN & amp; BIT0)

Unsigned char shuzi []={" 0123456789 "};
Unsigned char tem [2].
///////////////DHT11 output data//////////////////
Unsigned char DHT11T_Data_H;//high temperature 8
Unsigned char DHT11T_Data_L;//low temperature 8
Unsigned char DHT11RH_Data_H;//high humidity eight
Unsigned char DHT11RH_Data_L;//moderately low 8

/////////////function declaration///////////////////////
Unsigned char start_DHT11 (void);
Unsigned char DHT11_ReadChar (void);
Void dht11 (void);
Void Delayus (int n);
////////////////////////////////////////////
//function name: Delayus
//function: delay (us), the timer query methods
//call: no
//return: no
///////////////////////////////////////////
Void Delayus (int n)
{
TA0CCR0=n;
TA0CTL |=TACLR;
TA0CTL |=MC_1;
while(! (TA0CTL & amp; BIT0));//wait for
TA0CTL & amp;=~ MC_1;//stop counting
TA0CTL & amp;=~ BIT0;//mark clear interrupt
}

///////////the main function///////////////////
Void main (void)
{
Unsigned int readflag=0;
WDTCTL=WDTPW + WDTHOLD;
TA0CTL |=TASSEL_2 + ID_3;//the clock select SMCLK, 1/8 frequency, frequency counter 1 m
Unsigned char humidity=0;
Unsigned char temperature=0;
While (1)
{
Readflag=start_DHT11 ();
If (1)
{
Temperature=DHT11T_Data_H;//temperature value, without considering the decimal
Humidity=DHT11RH_Data_H;//humidity value, without considering the decimal
Tem [0]=temperature/10;//verify execution to the
Tem [1]=temperature % 10;
}
}
}
////////////////////////////////////////////
//function name: start_DHT11
//function: initialization DHT11 temperature and humidity value and
//call: DHT11_ReadChar ()
//return: no
///////////////////////////////////////////
Unsigned char start_DHT11 (void)
{
Unsigned char TData_H_temp=0;//high temperature 8 intermediate variable
Unsigned char TData_L_temp=0;//low temperature 8 intermediate variable
Unsigned char RHData_H_temp=0;//high humidity eight intermediate variable
Unsigned char RHData_L_temp=0;//low humidity eight intermediate variable
Unsigned char CheckData_temp=0;//check digit 8 intermediate variable
Char checktemp=0;//check values

Unsigned char flag=0;
Unsigned int count=0;

DHT11_OUTPUT;//set P2.0 output # define DHT11_OUTPUT P2DIR |=BIT0
DHT11_H;//output high # define DHT11_H P2OUT |=BIT0
Delayus (10);//delay 10 us
DHT11_L;//output low # define DHT11_L P2OUT & amp;=~ BIT0
//lower more than 18 ms
Delayus (30000);
DHT11_H;//output high
DHT11_INPUT;//set P2.0 input # define DHT11_INPUT P2DIR & amp;=~ BIT0
Delayus (60);//delay 60 us
if(! (DHT11_IN))//P2. If O input level and low level for
{
Count=2;//count value is set to 2
While ((! DHT11_IN) & amp; & Count++);
Count=2;
While ((DHT11_IN) & amp; & Count++);
RHData_H_temp=DHT11_ReadChar ();
RHData_L_temp=DHT11_ReadChar ();
TData_H_temp=DHT11_ReadChar ();
TData_L_temp=DHT11_ReadChar ();
CheckData_temp=DHT11_ReadChar ();
DHT11_OUTPUT;//set P2.0 output
DHT11_H;//P2.0 output is high, the release of bus
Checktemp=(RHData_H_temp + RHData_L_temp + + TData_L_temp TData_H_temp);//determine whether check digit correct
If (checktemp==CheckData_temp)
{
DHT11RH_Data_H=RHData_H_temp;
DHT11RH_Data_L=RHData_L_temp;
DHT11T_Data_H=TData_H_temp;
DHT11T_Data_L=TData_L_temp;
flag=1;
}
}
Return flag;//returns the flag, if return 1 read correct
}
////////////////////////////////////////////
//function name: DHT11_ReadChar
//function: 1 byte data read DHT11
//call:
//return: no
///////////////////////////////////////////
Unsigned char DHT11_ReadChar (void)
{
Unsigned char dat=0;
Unsigned int count;//count prevent death
Unsigned char I;
for(i=0; i<8; I++)
{
Count=2;
While ((! DHT11_IN) & amp; & Count++);//wait 50 us end of the low level, the program into the infinite loop exit # define DHT11_IN (P2IN & amp; BIT0)
Delayus (35);
Dat & lt; <=1;
If (DHT11_IN)//50 us said low level + us high level '0', said us low level 50 + 70 us high level '1'
{
Dat |=1.
}
Count=2;
While ((DHT11_IN) & amp; & Count++);//program into the infinite loop exit
/* while (DHT11_IN);
If (count==1)//timeout is out of the for loop
{
break;
} */
}
Return dat.
}

CodePudding user response:

You can change from several aspects: (1), the connection error (2), IIC part delay time is too short (SCM delay may not be accurate, had better extension) (3), IIC read data section may have a problem

CodePudding user response:

DHT11, single line, which come of I2C, a lot of routine the star sensor, can be baidu to casually, can't read it out of what you say is what do you mean not understand
  • Related