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

2019-03-24 13:23发布

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] = '';
    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] = '';}
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); //相对湿度值(%)        }}  
此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
3条回答
larrybirdkobe
2019-03-24 21:46
 精彩回答 2  元偷偷看……0人看过

一周热门 更多>

相关问题

    相关文章