温湿度传感器SHT10驱动.docx

上传人:b****3 文档编号:4655439 上传时间:2022-12-07 格式:DOCX 页数:8 大小:17.37KB
下载 相关 举报
温湿度传感器SHT10驱动.docx_第1页
第1页 / 共8页
温湿度传感器SHT10驱动.docx_第2页
第2页 / 共8页
温湿度传感器SHT10驱动.docx_第3页
第3页 / 共8页
温湿度传感器SHT10驱动.docx_第4页
第4页 / 共8页
温湿度传感器SHT10驱动.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

温湿度传感器SHT10驱动.docx

《温湿度传感器SHT10驱动.docx》由会员分享,可在线阅读,更多相关《温湿度传感器SHT10驱动.docx(8页珍藏版)》请在冰豆网上搜索。

温湿度传感器SHT10驱动.docx

温湿度传感器SHT10驱动

/****************************************Copyright(c)**************************************************

******************************************LiPeng********************************************************

**--------------FileInfo-------------------------------------------------------------------------------

**FileName:

      Sht10_Driver.c

**Createdby:

     LiPeng

**Createddate:

  2008-09-15

**Version:

         1.0

**Descriptions:

  Theoriginalversion

**

**------------------------------------------------------------------------------------------------------

**Modifiedby:

**Modifieddate:

**Version:

**Descriptions:

**

**------------------------------------------------------------------------------------------------------

**SystemFunction:

  Sht10Driver------温湿度传感器SHT10驱动

**使用MSP430-F413连接方式:

**                             VCC:

P6.3

**                             SCK:

P6.4

**                             SDA:

P6.5

**

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

#include

/*宏定义,延时函数,参数为1时相应延时分别为1us和1ms*/

#defineCPU_F              (double)1000000

#definedelay_us(x)        __delay_cycles((long)(CPU_F*(double)x/1000000.0))

#definedelay_ms(x)        __delay_cycles((long)(CPU_F*(double)x/1000.0))

/*常量定义*/

#defineuintunsignedint

#defineucharunsignedchar

#defineulongunsignedlong

                             //adrcommandr/w

#defineSTATUS_REG_W0x06   //000  0011   0

#defineSTATUS_REG_R0x07   //000  0011   1

#defineMEASURE_TEMP0x03   //000  0001   1

#defineMEASURE_HUMI0x05   //000  0010   1

#defineRESET        0x1e   //000  1111   0

#definebitselect    0x01   //选择温度与湿度的低位读

#definenoACK        0

#defineACK          1

#defineHUMIDITY     2

#defineTEMPERATURE  1

#defineSCK          BIT4

#defineSDA          BIT5

#defineSVCC         BIT3

#defineSCK_H        P6OUT|=SCK

#defineSCK_L        P6OUT&=~SCK

#defineSDA_H        P6OUT|=SDA

#defineSDA_L        P6OUT&=~SDA

#defineSVCC_H       P6OUT|=SVCC

#defineSVCC_L       P6OUT&=~SVCC

typedefunion

{

unsignedinti;

floatf;

}value;

uinttable_temp[3];

uinttable_humi[3];

uinttemten;

uinthumi_true;

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

**FunctionName:

     S_Init

**Description:

       初始化

**InputParameters:

  无

**OutputParameters:

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

voidS_Init()

{

P6SEL&=~(SCK+SDA+SVCC);                  //选择P6.3P6.4为IO端口,输出P6.5输入

P6DIR|=(SCK+SVCC);

P6DIR&=~SDA;

BCSCTL1=(XT2OFF+RSEL2);            //关闭XT2,1MHzDOC     

DCOCTL=DCO2;                             //设定DCO频率为1MHz

}

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

**FunctionName:

     S_Transstart

**Description:

       发送开始时序

**                   

**                   generatesatransmissionstart

**                         _____        ________

**                   DATA:

     |_______|

**                             ___    ___

**                   SCK:

___|  |___|  |______

**InputParameters:

  无

**OutputParameters:

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

voidS_Transstart()

{

P6DIR|=SDA;

SDA_H;SCK_L;

_NOP();

SCK_H;

_NOP();

SDA_L;

_NOP();

SCK_L;

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

SCK_H;

_NOP();

SDA_H;

_NOP();

SCK_L;

P6DIR&=~SDA;

}

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

**FunctionName:

     S_WriteByte

**Description:

       写时序

**InputParameters:

  无

**OutputParameters:

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

charS_WriteByte(unsignedcharvalue)

{

unsignedchari,error=0;

P6DIR|=SDA;

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

{

   if(i&value)

     SDA_H;                       //maskingvaluewithi,writetoSENSI-BUS

   else

     SDA_L;                       

   SCK_H;                         //clkforSENSI-BUS

   _NOP();_NOP();_NOP();          //pulswithapprox.5us

   SCK_L;

}

SDA_H;                           //releaseDATA-line

P6DIR&=~SDA;                     //ChangeSDAtobeinput

SCK_H;                           //clk#9forack

error=P6IN;                      //checkack(DATAwillbepulleddownbySHT11)

error&=SDA;

P6DIR|=SDA;

SCK_L;

if(error)

   return1;                      //error=1incaseofnoacknowledge

return0;

}

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

**FunctionName:

     S_ReadByte

**Description:

       读时序

**InputParameters:

  ack--->readsabyteformtheSensibusandgivesanacknowledgeincaseof"ack=1"

**OutputParameters:

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

charS_ReadByte(unsignedcharack)

{

unsignedchari,val=0;

P6DIR|=SDA;

SDA_H;                           //releaseDATA-line

P6DIR&=~SDA;

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

{

   SCK_H;                         //clkforSENSI-BUS

   if(P6IN&SDA)

     val=(val|i);                 //readbit

   SCK_L;     

}

P6DIR|=SDA;

if(ack)                          //incaseof"ack==1"pulldownDATA-Line

   SDA_L;

else

   SDA_H;                        

SCK_H;                           //clk#9forack

_NOP();_NOP();_NOP();            //pulswithapprox.5us

SCK_L;         

SDA_H;                           //releaseDATA-line

P6DIR&=~SDA;

returnval;

}

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

**FunctionName:

     S_Connectionreset

**Description:

       通讯复位时序

**                   communicationreset:

DATA-line=1andatleast9SCKcyclesfollowedbytransstart

**                         _____________________________________________________        ________

**                   DATA:

                                                     |_______|

**                            _   _   _   _   _   _   _   _   _       ___    ___

**                   SCK:

__||__||__||__||__||__||__||__||__||______|  |___|  |______

**InputParameters:

  无

**OutputParameters:

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

voidS_Connectionreset()

{

unsignedcharClkCnt;

P6DIR|=SDA;

SDA_H;SCK_L;                             //Initialstate

for(ClkCnt=0;ClkCnt<9;ClkCnt++)          //9SCKcycles

{

   SCK_H;

   SCK_L;

}                               

S_Transstart();                          //transmissionstart

}

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

**FunctionName:

     S_Softreset

**Description:

       软件复位时序resetsthesensorbyasoftreset

**InputParameters:

  无

**OutputParameters:

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

charS_Softreset()

{

unsignedcharerror=0;

S_Connectionreset();             //resetcommunication

error+=S_WriteByte(RESET);       //sendRESET-commandtosensor

returnerror;                    //error=1incaseofnoresponseformthesensor

}

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

**FunctionName:

     S_WriteStatusReg

**Description:

       写状态寄存器

**InputParameters:

  *p_value

**OutputParameters:

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

charS_WriteStatusReg(unsignedchar*p_value)

{

unsignedcharerror=0;

S_Transstart();                          //transmissionstart

error+=S_WriteByte(STATUS_REG_W);        //sendcommandtosensor

error+=S_WriteByte(*p_value);            //sendvalueofstatusregister

returnerror;                            //error>=1incaseofnoresponseformthesensor

}

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

**FunctionName:

     S_Mearsure

**Description:

       读时序     makesameasurement(humidity/temperature)withchecksum

**InputParameters:

  *p_value      ,*p_checknum      ,mode                  

**OutputParameters:

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

unsignedcharS_Measure(unsignedchar*p_value,unsignedchar*p_checksum,unsignedcharmode)

{

unsignederror=0;

unsignedinti;

S_Transstart();                  //transmissionstart

switch(mode)

{                                //sendcommandtosensor

   caseTEMPERATURE:

error+=S_WriteByte(MEASURE_TEMP);break;

   caseHUMIDITY:

   error+=S_WriteByte(MEASURE_HUMI);break;

}

P6DIR&=~SDA;

for(i=0;i<65535;i++)             //waituntilsensorhasfinishedthemeasurement

   if((P6IN&SDA)==0)

     break;

if(P6IN&SDA)

   error+=1;                      //ortimeout(~2sec.)isreached

*(p_value)=S_ReadByte(ACK);      //readthefirstbyte(MSB)

*(p_value+1)=S_ReadByte(ACK);    //readthesecondbyte(LSB)

*p_checksum=S_ReadByte(noACK);   //readchecksum

return(error);

}

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

**FunctionName:

     S_Calculate

**Description:

       计算

**InputParameters:

  humi[Ticks](12bit)

**                   temp[Ticks](14bit)                          

**OutputParameters:

humi[%R

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1