打印
[MCU]

MSP5438驱动温湿度传感器SHT11,debug有数据,而连续运行得不到数据

[复制链接]
351|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
两只袜子|  楼主 | 2020-12-3 09:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
430系列5438A驱动温湿度传感器SHT11,单次debug可以测得温度和湿度值,而连续运行得不到结果。求问原因。
以下为部分代码:

void delay_1us(void)
{
  int sec;
  for(sec=0;sec<8;sec++)
  {
    _NOP();
  }
}
void Transmit_Start(void)
{
  P9DIR|=0x02;
  SDA_1(); SCL_0();
  delay_1us();
  SCL_1();
  delay_1us();
  SDA_0();
  delay_1us();
  SCL_0();
  delay_1us();  
  SCL_1();
  delay_1us();
  SDA_1();
  delay_1us();
  SCL_0();
}
void Connection_Reset(void)
{
  unsigned int i;
  delay_1us();
  SDA_1();
  delay_1us();
  SCL_0();
  delay_1us();
  for(i = 0; i < 9 ; i++)
  {
    SCL_1();
    delay_1us();
    SCL_0();
    delay_1us();
}
  Transmit_Start();
}
char Write_Byte (unsigned char value)       
{
  unsigned char k,error=0;
  P9DIR|=0X02;P9REN&=0xFD;//1OUT
  delay_1us();
  for(k=0;k<8;k++,value<<=1)
  {       
    if(value & 0x80)
    {
     SDA_1();
    }
    else
    {
      SDA_0();
    }
    delay_1us();
    SCL_1();
    delay_1us();
    SCL_0();
    delay_1us();
  }
  P9DIR&=0XFD;P9REN|=0x02;P9OUT|=0x02;//9.1Input with pullup resistor
  
  SCL_1();
  delay_1us();
  error =P9IN&0X02;
  SCL_0();
  delay_1us();
  
  P9DIR|=0x02;
  delay_1us();
  SDA_1();SDA_1();
  return error;
}

char Read_Byte(unsigned char ack)
{
  unsigned char i,val =0x00;
  P9DIR&=0XFD;P9REN|=0x02;P9OUT|=0x02;//9.1输入模式,选择上拉电阻
  for(i=0;i<8;i++)
  {
    val<<=1;
    SCL_1();
    delay_1us();
    if(P9IN&0X02) val =(val|0x1);
    SCL_1();
    delay_1us();
  }
  P9DIR|=0X02;P9REN&=0xFD;//9.1 OUT
  delay_1us();
  if(ack==1)
  {
    SDA_0();
  }
  else
  {
    SDA_1();
  }
  _NOP();
  SCL_1();
  delay_1us();
  SCL_0();
  delay_1us();
  P9DIR&=0XFD;P9REN|=0x02;P9OUT|=0x02;//9.1输入模式,选择上拉电阻
  delay_1us();
  return val;
}       

char Measure_Mode(unsigned char *P_Value,unsigned char *P_Checksum,unsigned char Mode)
{
        unsigned char error=0;
        unsigned int i,j;
        Transmit_Start();
        switch(Mode)
        {
                case 0: error+= Write_Byte(measure_temp); break;
                case 1: error+= Write_Byte(measure_humi); break;
                default: break;
        }
        for(j=125;j>0;j--)
        {
          for(i=65535;i>0;i--)
          {
                if(P9IN&0X02==0) break;
          }
          if(P9IN&0X02==0) break;
        }
        if(P9IN&0X02) error+=1;
        *P_Value =Read_Byte(ACK);
        *(P_Value+1) =Read_Byte(ACK);
        *P_Checksum =Read_Byte(noACK);
        P9DIR|=0x02;
        return error;
}

void Cal_SHT11(float *P_Temp,float *P_Humi)
{
        const float C1 =-2.0468;
        const float C2 =+0.0367;
        const float C3 =-0.0000015955;
        const float T1 =+0.01;
        const float T2 =0.00008;
        float rh =*P_Humi;
        float t =*P_Temp;
        float rh_near,rh_true,t_c;
        t_c =t*0.01-40.1;
        rh_near =C1+C2*rh+C3*rh*rh;
        rh_true =(t_c-25)*(T1+T2*rh)+rh_near;
        if(rh_true>100) rh_true=100;       //cut if the value is outside of
        if(rh_true<0.1) rh_true=0.1;       //the physical possible range
        
        *P_Temp =t_c;                //返回温度
        *P_Humi =rh_true;        //返回相对湿度(rh%)
}

void Data_To_String(SHTscatter *Time,float DATA,float DATA1)
{
        unsigned int i;char gewei,shiwei;
           i=(int)DATA;
        shiwei=i/10;
        gewei =i%10;
           Time->DATAstrings1[0] = shiwei + '0';
        Time->DATAstrings1[1] = gewei + '0';
        i=(int)((DATA-i)*10);
        Time->DATAstrings1[2] ='.';
        Time->DATAstrings1[3] = i + '0';
        Time->DATAstrings1[4] = '\0';

           i=(int)DATA1;
        shiwei=i/10;
        gewei =i%10;
           Time->DATAstrings2[0] = shiwei + '0';
        Time->DATAstrings2[1] = gewei + '0';   
        i=(int)((DATA1-i)*10);
        Time->DATAstrings2[2] ='.';
        Time->DATAstrings2[3] = i + '0';
        Time->DATAstrings2[4] = '\0';
}

void Show_Tehu(void)
{
        SHTscatter S;
        SHTValue Temp_Value,Humi_Value;
        unsigned char error,checksum;
        
        error =0;      
        error =Measure_Mode((unsigned char*) &Humi_Value.i,&checksum,1)+error;  //湿度
        error =Measure_Mode((unsigned char*) &Temp_Value.i,&checksum,0)+error;  //温度
        if(error!=0)
         {
            Connection_Reset();
          }
         else
        {
          Humi_Value.f =(float)Humi_Value.i;
          Temp_Value.f =(float)Temp_Value.i;
          Cal_SHT11(&Humi_Value.f,&Temp_Value.f);
          Data_To_String(&S,Temp_Value.f,Humi_Value.f);
          PutStringEN24(188,200,S.DATAstrings1);        //温度值
          PutStringEN24(140,200,S.DATAstrings2);        //相对湿度值(%)
        }
}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1933

主题

6698

帖子

8

粉丝