搜索
bottom↓
回复: 3

sht10写错误(数据选线在写完成后不拉低)【恢复】

[复制链接]

出0入0汤圆

发表于 2008-10-16 15:07:59 | 显示全部楼层 |阅读模式
sht10写数据老是错误  数据选线在写完成后不拉低(我单步调试发现的)   造成后面的显示没有办法运行  请用过该传感器的大侠们指点指点!

用的是430的单片机

8M的晶振

请高手不吝指教!

小弟在这里谢谢了!!





/****************************************************/

#include  <msp430x14x.h>

#include  "cry1602.h"



//uchar *s1 = "temp:  00.0  'C";

//uchar *s2 = "humi:  00.0 %RH";



#define SCK_L         P6DIR|=BIT0;P6OUT&=~BIT0;_NOP();_NOP();_NOP();

#define SCK_H         P6DIR|=BIT0;P6OUT|=BIT0;_NOP();_NOP();_NOP();

#define DATA_L         P6DIR|=BIT1;P6OUT&=~BIT1;_NOP();_NOP();_NOP();

#define DATA_H         P6DIR|=BIT1;P6OUT|=BIT1;_NOP();_NOP();_NOP();

        

#define DATA_in         P6DIR&=~BIT1;_NOP();_NOP();_NOP();_NOP(); //设数据位输入

#define DATA_out P6DIR|=BIT1;_NOP();_NOP();_NOP();_NOP();  //设数据位输出



#define DATA_if         P6IN&BIT1    //判断SDA高还是低电平



#define ACK_no   0

#define ACK_yes  1



#define sht10_w         0x06  //000 0011 0写状态寄存器

#define sht10_r         0x07  //000 0011 1读状态寄存器

#define get_temperature 0x03  //000 0001 1获取温度

#define get_humidity    0x05  //000 0010 1获取湿度

#define sht10_rst       0x1e  //000 1111 0复位



static void InitOsc(void);

static void InitPorts(void);



union 

{

 unsigned int i;

 //unsigned char i;

 float f;

}

humi_val,temp_val;



/****************启动传输****************/

void sht10_start(void)

//       _____         ________

// DATA:      |_______|

//           ___     ___

// SCK : ___|   |___|   |______

{

  P6DIR=0x03;

  _NOP();_NOP();_NOP();

  DATA_H; //P2DIR|=BIT1;P2OUT|=BIT1

  _NOP();_NOP();_NOP();

  SCK_L;  //P2DIR|=BIT0;P2OUT&=~BIT0

  _NOP();_NOP();

  SCK_H;  //P2DIR|=BIT0;P2OUT|=BIT0

  _NOP();_NOP();_NOP();

  DATA_L; //P2DIR|=BIT1;P2OUT&=~BIT1

  _NOP();_NOP();_NOP();

  SCK_L;  //P2DIR|=BIT0;P2OUT&=~BIT0

  _NOP();_NOP();

  SCK_H;  //P2DIR|=BIT0;P2OUT|=BIT0

  _NOP();_NOP();_NOP();

  DATA_H; //P2DIR|=BIT1;P2OUT|=BIT1

  _NOP();_NOP();_NOP();

  SCK_L;  //P2DIR|=BIT0;P2OUT&=~BIT0

  _NOP();_NOP();_NOP();

}

/****************写状态寄存器****************/

char sht10_write(unsigned char value)

{

  unsigned char i;

  unsigned char error;   //P2IN&BIT1    //判断SDA高还是低电平

  P6DIR=0x03;

  _NOP();_NOP();_NOP();

  //DATA_H;

  //for(i=0x80;i>0;i/=2)

  for(i=0x80;i>0;i=i>>1)

   {

   if(i&value)

   {

   DATA_H;

   }

   else 

   {

    DATA_L;

   }

   SCK_H;

   _NOP();_NOP();_NOP();

   SCK_L;    //产生一下降沿

   }



   DATA_H;

   _NOP();_NOP();_NOP();

   SCK_H;     //数据送完的结束标志.

    _NOP();_NOP();

   DATA_in;   //P2DIR&=~BIT1;_NOP() //设数据位输入

   _NOP();_NOP();

   if(DATA_if==0x02)                //错误就是在这里它老是执行error=1;郁闷啊!



   {

     error=1;

   }

   else

   {

   error=0;

   }

   SCK_L;

   DATA_out;   

   _NOP();_NOP();

   DATA_H;

   return error;    //P2IN&BIT1    //判断SDA高还是低电平

}

/****************读状态寄存器****************/



char sht10_read(unsigned char ack)

{

  unsigned char i,value=0;

  DATA_out;   

   _NOP();_NOP();_NOP();

  DATA_H;

   _NOP();_NOP();_NOP();

  DATA_in;   

   _NOP();_NOP();_NOP();

  //for(i=0x80;i>0;i/=2)

  for(i=0x80;i>0;i=i>>1)

   {

   SCK_H;

   DATA_in;

   _NOP(); _NOP(); _NOP(); 

   if(DATA_if==0x02)

   {

    value=(value|i);

   _NOP(); _NOP(); _NOP();

   }

   SCK_L;

   }

   DATA_out;

   _NOP(); _NOP(); _NOP(); 

   if(ack)

   {

   DATA_L;

   }

   else

   {

     DATA_H;

   }

   _NOP(); _NOP(); _NOP(); _NOP();

   SCK_H;

   _NOP();_NOP();_NOP(); 

   SCK_L;

   DATA_H;

   return value;  

}

/****************通讯连接复位****************/

void sht10_con_rst(void)

//       _____________________________________________________         ________

// DATA:                                                      |_______|

//          _    _    _    _    _    _    _    _    _        ___     ___

// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______

{

  unsigned char i;

  DATA_out;

  _NOP();_NOP();

  DATA_H;

  _NOP();_NOP();_NOP();_NOP();_NOP();

  SCK_L;

  for(i=0;i<9;i++)

 {

   SCK_H;

   _NOP();_NOP();

   SCK_L;

   _NOP();_NOP();

 }

  sht10_start(); 

}

/****************软复位****************/

char sht10_soft_rst(void)

{

  unsigned char error=0;

  sht10_con_rst();

  error+=sht10_write(sht10_rst);

  return error;

}

/****************温度湿度测量****************/

 char get(unsigned char *value,unsigned char *check_sum,unsigned char mode)

{

  unsigned char error=0;

  //unsigned int i;

  sht10_start();

 switch(mode)

 {

   case 1:error=sht10_write(get_humidity);

     //error=sht10_write(0x05);

   break;

   case 2:error=sht10_write(get_temperature); //#define get_temperature 0x03  //000 0001 1

    // error=sht10_write(0x03);              //#define get_humidity    0x05  //000 0010 1

   break;

   default:break;

 }

/* DATA_in;

 _NOP();_NOP(); _NOP();_NOP();

 for (i=0;i<30000;i++) 

 {if(DATA_if==0){ break;}}

 if(DATA_if==0x02) 

 {

   error+=1;

 }*/

  DATA_out;

  _NOP();_NOP(); _NOP();_NOP();

  *value=sht10_read(ACK_yes);

  *(value+1)=sht10_read(ACK_yes);

  *check_sum=sht10_read(ACK_no);

  return error;

}





/****************补偿及输出温度和相对湿度计算****************/

void calculate_sht10(float *p_humidity ,float *p_temperature)

{

  const float C1=-4.0;              // for 12 Bit 湿度修正公式

  const float C2=0.0405;           // for 12 Bit 湿度修正公式

  const float C3=-0.0000028;        // for 12 Bit 湿度修正公式

  const float T1=0.01;             // for 14 Bit @ 5V 温度修正公式

  const float T2=0.00008;          // for 14 Bit @ 5V 温度修正公式

 

  float rh=*p_humidity;            

  float t=*p_temperature;          

  float rh_lin;                    

  float rh_true;                   

  float t_C;                       



  t_C=t*0.01-40;                      //补偿温度

  rh_lin=C3*rh*rh+C2*rh+C1;           //相对湿度非线性补偿

  rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //相对湿度对于温度依赖性补偿

  if(rh_true>100)rh_true=100;         //湿度最大修正

  if(rh_true<0.1)rh_true=0.1;         //湿度最小修正



  *p_temperature=t_C;                 //返回温度结果

  *p_humidity=rh_true;                //返回湿度结果  

}







static void InitOsc(void)

{

  WDTCTL = WDTPW + WDTHOLD;                      // stop watchdog timer

  BCSCTL1 |= XTS;                                // XT1 as high-frequency

  __bic_SR_register(OSCOFF);                     // turn on XT1 oscillator

  do{

    IFG1 &= ~OFIFG;                              // Clear OFIFG

    delay();                                  // Wait ~130us

  } while (IFG1 & OFIFG);                        // Test oscillator fault flag

  BCSCTL2 = SELM_3;                              // set XT1 as MCLK  SELM_3(0xc0,11000000)

}  











//------------------------------------------------------------------------------

// Local function prototypes

//------------------------------------------------------------------------------







static void InitPorts(void)

{

  P1SEL = 0;

  P1OUT = 0;                                     // switch all unused ports to output   

  P1DIR = 0xff;                                  // (Rem.: ports 3 & 5 are set in "cs8900.c")

  P2SEL = 0;

  P2OUT = 0;

  P2DIR = 0xff;

  P4OUT = 0;

  P4DIR = 0xff;

  P6SEL = 0x00;                                  // use P6.7 for the ADC module

  P6OUT = 0;

  P6DIR = 0xff;                                  // all output except P6.7

}



//------------------------------------------------------------------------------

void main(void)

{

/* 以下添加LCD与温湿度程序初始变量定义部分*/

  unsigned char num[10]={'0','1','2','3','4','5','6','7','8','9'};

  unsigned char error,check_sum;

  unsigned char HUMI,TEMP;



  uchar FirstLine[]="temp:  10.0  'C";

  uchar TwoLine[]=  "humi:  10.0 %RH";



/* 结束添加LCD与温湿度程序初始变量定义部分*/



  InitOsc();

  InitPorts();





/* 以下添加LCD与温湿度程序初始化部分*/

  HUMI=0x01;

  TEMP=0x02;

  WDTCTL=WDTPW+WDTHOLD;

  LcdReset();

  Delay_ms(1);

  DispStr(0,0,FirstLine);

  DispStr(0,1,TwoLine);

  Delay_ms(1);

  sht10_con_rst();

/* 结束添加LCD与温湿度程序初始化部分*/



  while (1)                                      // repeat forever

  {



/* 以下添加LCD与温湿度程序无限循环(主体)部分*/

    error=0;

    error+=get((unsigned char*)&humi_val.i,&check_sum,HUMI);//湿度测量

    Delay_ms(5000);

    error+=get((unsigned char*)&temp_val.i,&check_sum,TEMP);//温度测量

    if(error!=0)    sht10_con_rst();                            //如果发生错误,系统复位

    else   

    { 

      humi_val.f=(float)humi_val.i;             //转换为浮点数

      temp_val.f=(float)temp_val.i;             //转换为浮点数

      calculate_sht10(&humi_val.f,&temp_val.f); //修正相对湿度及温度

     //calculate_sht10(&temp_val.f,&humi_val.f);

      temp_val.i=(int)temp_val.f*10;

      humi_val.i=(int)humi_val.f*10;

      Disp1Char(7,0,num[temp_val.i/100]);

    //  Delay_ms(100);

      Disp1Char(7,1,num[humi_val.i/100]);

     // Delay_ms(100);

      Disp1Char(8,0,num[(temp_val.i%100)/10]);

      Delay_ms(5); 

      Disp1Char(8,1,num[(humi_val.i%100)/10]);

      Delay_ms(5);

      Disp1Char(10,0,num[temp_val.i%10]); 

     // Delay_ms(100);

      Disp1Char(10,1,num[humi_val.i%10]);

    }

    Delay_ms(5000);

    //Delay_ms(60000);

   //等待足够长的时间,等待下一次采集

  /* 结束LCD与温湿度程序无限循环(主体)部分*/

   }

}



/*

void main( void )

{

   // uchar i;

   int i,x,y;

   // WDTCTL = WDTPW + WDTHOLD;               //关闭看门口

    InitOsc();

    P2DIR |= 0xFF; 

    LcdReset();

//    DispStr(0,0,s1);

//    DispStr(0,1,s2);

    DispStr(0,0,FirstLine);

    DispStr(0,1,TwoLine);



    while(1){

    ;



    x=200;

    for (i=-5;i<120;i++){

      x--;

      if(i<0)  {Disp1Char(6,0,PosNeg[1]);y=-i;}

      else {Disp1Char(6,0,PosNeg[0]);y=i;}

      Disp1Char(7,0,num[y/100]);

      Disp1Char(7,1,num[y/100]);

      Disp1Char(8,0,num[y%100/10]);

      Disp1Char(8,1,num[y%100/10]);

      Disp1Char(10,0,num[y%10]);

      Disp1Char(10,1,num[y%10]);

      for (y=1;y<100;y++)delay();

    }

    delay();

    }

}*/

    

    

  /*  LocateXY(0,9);                      //确定写入字符的显示位置

    LcdWriteCommand(0x07, 1);           //整体显示左移

    for(i = 12; i> 0; i--) 

    {

        LcdWriteData(0x20);

        //延时250ms

        IFG1 &= ~WDTIFG;

        while(!(IFG1 & WDTIFG));//等待清除标志

        IFG1 &= ~WDTIFG; 

    }

    while(1)

    {

       

      

      

      LcdWriteCommand(0x05, 1);           //整体显示右移

        

        for(i = 24; i> 0; i--)  

        {

            LcdWriteData(0x20);

            //延时250ms

            IFG1 &= ~WDTIFG;

            while(!(IFG1 & WDTIFG));//等待清除中断标志

            IFG1 &= ~WDTIFG; 

        }

    

        

        

        

        

        LcdWriteCommand(0x07, 1);           //整体显示左移

        

        for(i = 24; i> 0; i--)

        {

            LcdWriteData(0x20);

            //延时250ms

            IFG1 &= ~WDTIFG;

            while(!(IFG1 & WDTIFG));

            IFG1 &= ~WDTIFG; 

        }

    }    

}

*/

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2008-10-16 16:59:14 | 显示全部楼层
SHT10裸片那么娇贵的东西。胶水使用不慎滴到感应区,烘干时间过长等等都会让它挂了。

楼主应该多试几片。

出0入0汤圆

 楼主| 发表于 2008-10-16 16:25:46 | 显示全部楼层
大侠们帮我看下嘛  怎么收不到应答信号!

出0入0汤圆

发表于 2012-8-15 21:09:54 | 显示全部楼层
我用 STM32 也是收不到应答信号
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-25 20:12

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表