打印
[I2C]

寻求温度传感器HTU21的程序设计

[复制链接]
774|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
a1435930003|  楼主 | 2016-8-6 17:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是我写的程序,请问哪里写的不对,希望各位能帮我指正一下,谢谢,我做好了好久都没连通
以下是程序:

#define ACK 0
#define NACK 1
#define HTU21_ADDRESS_Write 0x80
#define HTU21_ADDRESS_Read 0x81
#define Hold_Master_Temperature_Measurement 0xE3
#define Hold_Master_Humidity_Measurement 0xE5
#define No_Hold_Master_Temperature_Measurement 0xF3
#define No_Hold_Master_Humidity_Measurement 0xF5
#define W_User_Register 0xE6
#define R_User_Register 0xE7
#define Soft_Reset 0xFE


sbit SCL_T=P3^2;
sbit SDA_T=P3^3;

void delayus(uint us)
{
        while(us--);
}

void Init_IIC(void)        //free HTU21
{
        SDA_T=1;
        SCL_T=1;
}

void T_Start_IIC(void)
{                       
        SDA_T=1;
        SCL_T=1;
        delayms(1);
        SDA_T=0;
        delayus(10);
        SCL_T=0;
       
}

void T_Stop_IIC(void)
{
        SCL_T=0;
        SDA_T=0;
        delayms(1);
        SCL_T=1;
        delayus(10);
        SDA_T=1;
}

uchar Write_IIC_Byte(char Byte)
{
        uchar i;
        SCL_T=0;           //Initization SCL=0, Wait Send Data to HTU21
        for(i=0;i<8;i++)   //Start Send Data to HTU21 From MCU  
        {
                if(Byte & 0x80)
                {
                        SDA_T=1;    //MCU Write 1 to HTU21
                }
                else
                {
                        SDA_T=0;    //MCU Write 0 to HTU21
                }
                delayus(1);   //Wait SDA=1 or SDA=0 stable;
                SCL_T=1;      //MCU Send
                delayus(5);   //Wait HTU21 Receive
                SCL_T=0;      //Initization SCL=0, Wait Send Next bit to HTU21
                delayus(1);   //Wait SCL=0 stable;
                Byte<<=1;     
        }               //Send Finished
        delayus(1); // Wait Send Finished
        SDA_T=0;    // HTU21 Feedback Response
        delayus(1);
        SCL_T=1;                // Receive Response
        delayus(5); // Wait Receive
        if(SDA_T==1)
        {
                SCL=0;
                delayus(5);
                return NACK;    //invalidity
        }
        else
        {
                SCL=0;
                delayus(5);
                return ACK;     //validity
        }

}

uchar Read_IIC_Byte(char ACK_NACK)
{

        uchar i,RD_Byte=0;
        SCL_T=0;
        delayus(5);
        SDA_T=1;
        delayus(5);
        for(i=0;i<8;i++)
        {
                SCL_T= 1;
                delayus(2);
                RD_Byte<<= 1;
                if(SDA_T==1)
                {
                        RD_Byte|=0x01;
                }
                else
                {
                        RD_Byte&=0xfe;
                }
                delayus(1);
                SCL_T = 0;
                delayus(5);
        }
        SDA_T=ACK_NACK;
        delayus(3);
        SCL_T=1;
        delayus(5);
        SCL_T=0;
        return RD_Byte;
       
}

void HTU21_Reset()
{
        Init_IIC();
        T_Start_IIC();
        Write_IIC_Byte(HTU21_ADDRESS_Write);     //HTU21_ADDRESS_Write是0x80
        Write_IIC_Byte(Soft_Reset);                       //Soft_Reset 是 0xFE
        T_Stop_IIC();
}

float HTU21_Start(char what)
{
        float temp=0,Humidity=0,temperature=0;
        uchar MSB=0,LSB=0;
       
        T_Start_IIC();
       
        if(Write_IIC_Byte(HTU21_ADDRESS_Write&0xfe)==ACK)
        {
                if(Write_IIC_Byte(what)==ACK)
                {
                        do
                        {
                                T_Start_IIC();
                                delayms(15);
                        }while(Write_IIC_Byte(HTU21_ADDRESS_Write|0x01)==NACK);
                       
                        MSB =  Read_IIC_Byte(ACK);
                        LSB =  Read_IIC_Byte(ACK);
                        Read_IIC_Byte(NACK);              //Checksum + NACK
                        T_Stop_IIC();
                       
                        LSB &= 0xfc;
//                        temp=HumidityH*256+HumidityL;
                        temp=(MSB<<8)|LSB;

                        if(what==((char)No_Hold_Master_Humidity_Measurement))
                        {
                                /*
                                        equation:RH%=-6+125*SRH/2^RH_Resolution
                                */
                                Humidity=temp*125/65535-6;
                                return Humidity;
                        }
                        else
                        {
                                /*
                                        equation:¡æ=-46.85+175.12*ST/2^T_Resolution
                                */
                                temperature=temp*175.12/65535-46.85;
                                return temperature;
                        }
                }
        }

        return 0;

}

相关帖子

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

本版积分规则

2

主题

2

帖子

0

粉丝