新手求助,关于DHT11,有经验的看一下

2019-03-23 22:28发布

最近在做MSP430F149驱动DHT11采集温湿度然后在LCD1602上显示的实验,但硬件连接好后总是显示00.0,好像DHT11没工作,看了好久找不出程序问题,希望有经验的你帮下忙,谢谢。。。原程序如下: #include "msp430x14x.h" // 包含头文件
#define uchar unsigned char
#define uint unsigned int
#define DHT11_OUTPUT    P2DIR|=BIT0    
#define DHT11_INPUT     P2DIR&=~BIT0   
#define DHT11_H         P2OUT|=BIT0   
#define DHT11_L         P2OUT&=~BIT0   #define DHT11_IN        (P2IN&BIT0)

uchar str1[]={"        "};
uchar str2[]={"        "};
uchar  dis1[] = {"TRH RH: "}; //定义数组
uchar  dis2[] = {"TRH  T: "};
uchar TH_data,TL_data,RH_data,RL_data,CK_data;
uchar TH_temp,TL_temp,RH_temp,RL_temp,CK_temp;
uchar com_data,untemp,temp;
uchar respond;
void Delayus(int n)
{
    CCR0 = n;
    TACTL |= (MC_1 + TACLR + TAIE);                 //增计数到CCR0,计数器清零,定时器A中断打开
    TACTL |= TACLR;
    TACTL |= MC_1;   
    while(!(TACTL & BIT0));                         //等待
    TACTL &= ~(MC_1 + TAIE + BIT0);                 //停止计数,关中断,清除中断标志
    TACTL &= ~MC_1;               //停止计数
    TACTL &= ~BIT0;              //清除中断标志
}

void delay(uint  z)
{
  uint t,y;
  for(t=z;t>0;t--)
  {
    for(y=110;y>0;y--);
  }
}
void write_com(uchar com)
{
  P3OUT&=~BIT0;//RS
  P3OUT&=~BIT1; //RW
  P5OUT=com;
  delay(5);
  P3OUT|=BIT2; //E
  delay(5);
  P3OUT&=~BIT2;
}
void write_data(uchar date)
{
  P3OUT|=BIT0;
  P3OUT&=~BIT1;
  P5OUT=date;
  delay(5);
  P3OUT|=BIT2;
  delay(5);
  P3OUT&=~BIT2;
}
void LCD_set_xy( uchar x, uchar y ) 
{
     uchar address;
     if (y == 0)
      address = 0x80 + x;
     else
         address = 0xc0 + x;
     write_com(address);
}
void LCD_write_char( uchar x,uchar y,uchar dat)
{
 LCD_set_xy( x, y );
 write_data(dat);
}
void init()
{
  P3DIR=0xff;
  P3SEL=0;
  P3OUT=0x00;
  P3OUT&=~BIT2;
  P3OUT&=~BIT0;
  P5DIR=0XFF;
  P5SEL=0;
  P5OUT=0X00;
  write_com(0x38);
  write_com(0x0c);
  write_com(0x06);
  write_com(0x01);
 
}
//设定LCD显示位置
void lcd_dis_pos(uchar pos)
{      
          write_com(pos | 0x80); //数据指针=80+地址变量
}
    //收发信号检测,数据读取
char receive()
{   
          uchar i;           com_data=0;
          for(i=0;i<=7;i++)   
         {
                    respond=2;
                    while((!DHT11_IN)&&respond++);
                    Delayus(40);
                   Delayus(40);
                
                    if(DHT11_IN)
                    {
                              temp=1;
                              respond=2;
                                while((DHT11_IN)&&respond++);
                    }
                    else
                              temp=0;
                    com_data<<=1;
                    com_data|=temp;   
          }
          return(com_data); 
}   //湿度读取子程序
//温度高8位== TL_data
//温度低8位== TH_data
//湿度高8位== RH_data
//湿度低8位== RH_data
//校验 8位 == CK_data
void read_TRH()
{        
          DHT11_OUTPUT;           //设置P2.0输出
          DHT11_H;                ////输出高
         Delayus(10);
         DHT11_L;
        Delayus(20000);
         DHT11_H;
          //DATA总线由上拉电阻拉高 主机延时20us
         Delayus(40);
         DHT11_INPUT;
         Delayus(60);
         Delayus(60);           
          if(!(DHT11_IN))  
         {
                  respond=2;
                  //判断DHT11发出 80us 的低电平响应信号是否结束
                   while((!DHT11_IN)&&respond++);
                 respond=2;
                 //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
                while((DHT11_IN)&&respond++);
               //数据接收状态  
                RH_temp = receive();
                RL_temp = receive();
                TH_temp = receive();
                TL_temp = receive();
                CK_temp = receive();
                DHT11_OUTPUT;                       //设置P2.0输出
                DHT11_H;                            //P2.0输出高,释放总线
                 untemp=(RH_temp+RL_temp+TH_temp+TL_temp);
                 if(untemp==CK_temp)
                 {
                          RH_data = RH_temp;
                          RL_data = RL_temp;
                         TH_data = TH_temp;
                          TL_data = TL_temp;
                          CK_data = CK_temp;
                }
        }
                          // RH_data = 21;
                         // RL_data = 13;
                        // TH_data =36;
                         // TL_data = 40;
         //湿度整数部分
         str1[0] =(char)0X30+RH_data/10;
         str1[1] =(char) 0X30+RH_data%10;
         str1[2] =0x2e; //小数点
          //湿度小数部分
         str1[3] =(char) 0X30+RL_data/10;
          str1[5] = 0X25; //"%"
         str1[6] = 0X52; //"R"
         str1[7] = 0X48; //"H"
         //温度整数部分
         str2[0] =(char) 0X30+TH_data/10;
         str2[1] =(char) 0X30+TH_data%10;
         str2[2] =(char)  0x2e; //小数点
         //温度小数部分
          str2[3] = 0X30+TL_data/10;
          str2[5] = 0X27;  //"'"
          str2[6] = 0X43;  //"C"
}   void main (void)
{
          uchar i,n=0x40,m;
          WDTCTL = WDTPW + WDTHOLD;      
          TACTL|= TASSEL_2 + ID_3;
          init(); 
          
          while(1)
          {
                    read_TRH();
                    //写字符
                     for(i=0;i<=7;i++)
                    {
                            lcd_dis_pos(i);    //显示字符
                            write_data(dis1);
                            lcd_dis_pos(n+i);    //显示字符
                            write_data(dis2);
                    }
                   //写湿度数据
                   m=0x08;
                   for(i=0;i<=7;i++)     
                   {
                           lcd_dis_pos(m);
                           write_data(str1);
                           m++;
                  }
                  //写温度数据
                  m=0x48;
                  for(i=0;i<=7;i++)     
                  {
                           lcd_dis_pos(m);
                           write_data(str2);
                           m++;                    }

        }     
}
此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
5条回答
兜揣阳光
2019-03-24 09:44
 精彩回答 2  元偷偷看……0人看过

一周热门 更多>

相关问题

    相关文章