OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6498|回复: 5

老哥们求助,SHT20温湿度传感器读取数据出来温度是128,湿度是118%是什么原因

[复制链接]

20

主题

70

帖子

0

精华

初级会员

Rank: 2

积分
65
金钱
65
注册时间
2020-5-6
在线时间
43 小时
发表于 2020-5-14 11:16:36 | 显示全部楼层 |阅读模式
3金钱
我的代码如下,附上工程
#define SHT2x_I2C_PORT          GPIOB
#define SHT2x_SCL_PIN           GPIO_Pin_12   //GPIO模拟I2C   PB12 PB13
#define SHT2x_SDA_PIN           GPIO_Pin_13   

#define SHT2x_SCL_HIGH()        GPIO_SetBits(SHT2x_I2C_PORT, SHT2x_SCL_PIN)
#define SHT2x_SCL_LOW()         GPIO_ResetBits(SHT2x_I2C_PORT, SHT2x_SCL_PIN)

#define SHT2x_SDA_HIGH()               GPIO_SetBits(SHT2x_I2C_PORT, SHT2x_SDA_PIN)
#define SHT2x_SDA_LOW()         GPIO_ResetBits(SHT2x_I2C_PORT, SHT2x_SDA_PIN)

#define SHT2x_SCL_STATE()       GPIO_ReadInputDataBit(SHT2x_I2C_PORT, SHT2x_SCL_PIN)
#define SHT2x_SDA_STATE()       GPIO_ReadInputDataBit(SHT2x_I2C_PORT, SHT2x_SDA_PIN)


void SHT2x_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin =  SHT2x_SCL_PIN | SHT2x_SDA_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SHT2x_I2C_PORT, &GPIO_InitStructure);
      
    SHT2x_SCL_HIGH();
    SHT2x_SDA_HIGH();

    SHT2x_Delay(80);

    SHT2x_SoftReset();
}

void SHT2x_SCL_OUTPUT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;


    GPIO_InitStructure.GPIO_Pin =  SHT2x_SCL_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SHT2x_I2C_PORT, &GPIO_InitStructure);
}

void SHT2x_SCL_INPUT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;


    GPIO_InitStructure.GPIO_Pin =  SHT2x_SCL_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SHT2x_I2C_PORT, &GPIO_InitStructure);
}

void SHT2x_SDA_OUTPUT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;


    GPIO_InitStructure.GPIO_Pin =  SHT2x_SDA_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SHT2x_I2C_PORT, &GPIO_InitStructure);
}

void SHT2x_SDA_INPUT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;


    GPIO_InitStructure.GPIO_Pin =  SHT2x_SDA_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(SHT2x_I2C_PORT, &GPIO_InitStructure);
}

void SHT2x_I2cStartCondition(void)
{
    SHT2x_SCL_OUTPUT();
    SHT2x_SDA_OUTPUT();

    SHT2x_SDA_HIGH();
    SHT2x_SCL_HIGH();
    SHT2x_SDA_LOW();
    SHT2x_Delay(30);
    SHT2x_SCL_LOW();
    SHT2x_Delay(30);
}

void SHT2x_I2cStopCondition(void)
{
    SHT2x_SCL_OUTPUT();
    SHT2x_SDA_OUTPUT();

    SHT2x_SDA_LOW();
    SHT2x_SCL_LOW();
    SHT2x_SCL_HIGH();
    SHT2x_Delay(30);
    SHT2x_SDA_HIGH();
    SHT2x_Delay(30);
}

void SHT2x_I2cAcknowledge(void)
{
    SHT2x_SCL_OUTPUT();
    SHT2x_SDA_OUTPUT();

    SHT2x_SDA_LOW();

    SHT2x_SCL_HIGH();
    SHT2x_Delay(80);
    SHT2x_SCL_LOW();   
    SHT2x_Delay(80);
}

void SHT2x_I2cNoAcknowledge(void)
{
    SHT2x_SCL_OUTPUT();
    SHT2x_SDA_OUTPUT();

    SHT2x_SDA_HIGH();

    SHT2x_SCL_HIGH();
    SHT2x_Delay(80);
    SHT2x_SCL_LOW();   
    SHT2x_Delay(80);
}

u8 SHT2x_I2cReadByte(void)
{
    u8 i, val = 0;

    SHT2x_SCL_OUTPUT();
    SHT2x_SDA_OUTPUT();

    SHT2x_SDA_HIGH();

    SHT2x_SDA_INPUT();

    for(i = 0; i < 8; i++)
    {
        val <<= 1;

        SHT2x_SCL_HIGH(); //&#182;á2ù×÷£&#172;&#214;&#195;1  

        if(Bit_SET == SHT2x_SDA_STATE())
        {
            val |= 0x01;
        }

        SHT2x_SCL_LOW();   
    }

    SHT2x_SDA_OUTPUT();

    SHT2x_SDA_HIGH();

    return (val);
}

u8 SHT2x_I2cWriteByte(u8 byte)
{
    u8 i, ack;

    SHT2x_SCL_OUTPUT();
    SHT2x_SDA_OUTPUT();

    for(i = 0; i < 8; i++)
    {
        if(byte & 0x80)
        {
            SHT2x_SDA_HIGH();
        }
        else
        {
            SHT2x_SDA_LOW();
        }

        SHT2x_SCL_HIGH();
        SHT2x_Delay(80);
        SHT2x_SCL_LOW();   
        SHT2x_Delay(80);

        byte <<= 1;
    }

    SHT2x_SDA_INPUT();

    SHT2x_SCL_HIGH();

    if(Bit_SET == SHT2x_SDA_STATE())
    {
        ack = ACK_ERROR;
    }
    else
    {
        ack = ACK_OK;
    }

    SHT2x_SCL_LOW();  

    SHT2x_SDA_OUTPUT();

    SHT2x_SDA_HIGH();

    return (ack);
}

float SHT2x_MeasureTempHM(void)
{
    float TEMP;
    u8 tmp1, tmp2;
    u16 ST;

    SHT2x_SCL_OUTPUT();

    SHT2x_I2cStartCondition();                           
    SHT2x_I2cWriteByte(I2C_ADR_W);
    SHT2x_I2cWriteByte(TRIG_TEMP_MEASUREMENT_HM);

    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_R);

    SHT2x_SCL_HIGH();

    SHT2x_SCL_INPUT();

    while(Bit_RESET == SHT2x_SCL_STATE())
    {
        SHT2x_Delay(20);
    }

    tmp1 = SHT2x_I2cReadByte();
    SHT2x_I2cAcknowledge();
    tmp2 = SHT2x_I2cReadByte();
    SHT2x_I2cNoAcknowledge();
    SHT2x_I2cStopCondition();

    ST = (tmp1 << 8) | (tmp2 << 0);
    ST &= ~0x0003;
    TEMP = ((float)ST * 0.00268127) - 46.85;

    SHT2x_SCL_OUTPUT();
    return (TEMP);         
}

float SHT2x_MeasureHumiHM(void)
{
    float HUMI;
    u8 tmp1, tmp2;   
    u16 SRH;

    SHT2x_SCL_OUTPUT();  
    SHT2x_I2cStartCondition();                              
    SHT2x_I2cWriteByte(I2C_ADR_W);
    SHT2x_I2cWriteByte(TRIG_HUMI_MEASUREMENT_HM);

    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_R);

    SHT2x_SCL_HIGH();

    SHT2x_SCL_INPUT();

    while(Bit_RESET == SHT2x_SCL_STATE())
    {
        SHT2x_Delay(20);
    }

    tmp1 = SHT2x_I2cReadByte();
    SHT2x_I2cAcknowledge();
    tmp2 = SHT2x_I2cReadByte();
    SHT2x_I2cNoAcknowledge();
    SHT2x_I2cStopCondition();

    SRH = (tmp1 << 8) | (tmp2 << 0);
    SRH &= ~0x0003;
    HUMI = ((float)SRH * 0.00190735) - 6;

    SHT2x_SCL_OUTPUT();

    return (HUMI);
}

float SHT2x_MeasureTempPoll(void)
{
    float TEMP;
    u8 ack, tmp1, tmp2;
    u16 ST;

    SHT2x_I2cStartCondition();                           
    SHT2x_I2cWriteByte(I2C_ADR_W);
    SHT2x_I2cWriteByte(TRIG_TEMP_MEASUREMENT_POLL);

    do {
        SHT2x_Delay(20);               
        SHT2x_I2cStartCondition();
        ack = SHT2x_I2cWriteByte(I2C_ADR_R);
    } while(ACK_ERROR == ack);

    tmp1 = SHT2x_I2cReadByte();
    SHT2x_I2cAcknowledge();
    tmp2 = SHT2x_I2cReadByte();
    SHT2x_I2cNoAcknowledge();
    SHT2x_I2cStopCondition();

    ST = (tmp1 << 8) | (tmp2 << 0);
    ST &= ~0x0003;
    TEMP = ((float)ST * 0.00268127) - 46.85;

    return (TEMP);         
}

float SHT2x_MeasureHumiPoll(void)
{
    float HUMI;
    u8 ack, tmp1, tmp2;   
    u16 SRH;

    SHT2x_I2cStartCondition();                              
    SHT2x_I2cWriteByte(I2C_ADR_W);
    SHT2x_I2cWriteByte(TRIG_HUMI_MEASUREMENT_POLL);

    do {
        SHT2x_Delay(20);
        SHT2x_I2cStartCondition();
        ack = SHT2x_I2cWriteByte(I2C_ADR_R);
    } while(ACK_ERROR == ack);

    tmp1 = SHT2x_I2cReadByte();
    SHT2x_I2cAcknowledge();
    tmp2 = SHT2x_I2cReadByte();
    SHT2x_I2cNoAcknowledge();
    SHT2x_I2cStopCondition();

    SRH = (tmp1 << 8) | (tmp2 << 0);
    SRH &= ~0x0003;
    HUMI = ((float)SRH * 0.00190735) - 6;

    return (HUMI);
}

u8 SHT2x_ReadUserReg(void)   
{
    u8 reg;
      
    SHT2x_I2cStartCondition();                 
    SHT2x_I2cWriteByte(I2C_ADR_W);
    SHT2x_I2cWriteByte(USER_REG_R);
    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_R);
    reg = SHT2x_I2cReadByte();
    SHT2x_I2cNoAcknowledge();
    SHT2x_I2cStopCondition();

    return (reg);
}

u8 SHT2x_WriteUserReg(u8 reg)
{
    u8 ack;
      
    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_W);
    SHT2x_I2cWriteByte(USER_REG_W);
    ack = SHT2x_I2cWriteByte(reg);
    SHT2x_I2cStopCondition();  

    return (ack);
}

void SHT2x_SoftReset(void)
{
    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_W);
    SHT2x_I2cWriteByte(SOFT_RESET);
    SHT2x_I2cStopCondition();

    SHT2x_Delay(80);
}

void SHT2x_GetSerialNumber(u8 *buf)
{
    /* Read from memory location 1 */
    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_W); //I2C address
    SHT2x_I2cWriteByte(0xFA); //Command for readout on-chip memory
    SHT2x_I2cWriteByte(0x0F); //on-chip memory address
    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_R); //I2C address
    buf[5] = SHT2x_I2cReadByte(); //Read SNB_3
    SHT2x_I2cAcknowledge();
    SHT2x_I2cReadByte(); //Read CRC SNB_3 (CRC is not analyzed)
    SHT2x_I2cAcknowledge();
    buf[4] = SHT2x_I2cReadByte(); //Read SNB_2
    SHT2x_I2cAcknowledge();
    SHT2x_I2cReadByte(); //Read CRC SNB_2 (CRC is not analyzed)
    SHT2x_I2cAcknowledge();
    buf[3] = SHT2x_I2cReadByte(); //Read SNB_1
    SHT2x_I2cAcknowledge();
    SHT2x_I2cReadByte(); //Read CRC SNB_1 (CRC is not analyzed)
    SHT2x_I2cAcknowledge();
    buf[2] = SHT2x_I2cReadByte(); //Read SNB_0
    SHT2x_I2cAcknowledge();
    SHT2x_I2cReadByte(); //Read CRC SNB_0 (CRC is not analyzed)
    SHT2x_I2cNoAcknowledge();
    SHT2x_I2cStopCondition();

    /* Read from memory location 2 */
    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_W); //I2C address
    SHT2x_I2cWriteByte(0xFC); //Command for readout on-chip memory
    SHT2x_I2cWriteByte(0xC9); //on-chip memory address
    SHT2x_I2cStartCondition();
    SHT2x_I2cWriteByte(I2C_ADR_R); //I2C address
    buf[1] = SHT2x_I2cReadByte(); //Read SNC_1
    SHT2x_I2cAcknowledge();
    buf[0] = SHT2x_I2cReadByte(); //Read SNC_0
    SHT2x_I2cAcknowledge();
    SHT2x_I2cReadByte(); //Read CRC SNC0/1 (CRC is not analyzed)
    SHT2x_I2cAcknowledge();
    buf[7] = SHT2x_I2cReadByte(); //Read SNA_1
    SHT2x_I2cAcknowledge();
    buf[6] = SHT2x_I2cReadByte(); //Read SNA_0
    SHT2x_I2cAcknowledge();
    SHT2x_I2cReadByte(); //Read CRC SNA0/1 (CRC is not analyzed)
    SHT2x_I2cNoAcknowledge();
    SHT2x_I2cStopCondition();
}

STM32UCOS2.zip

18.71 MB, 下载次数: 31

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

0

主题

61

帖子

0

精华

初级会员

Rank: 2

积分
62
金钱
62
注册时间
2018-12-27
在线时间
0 小时
发表于 2020-5-14 11:16:38 | 显示全部楼层
回复

使用道具 举报

4

主题

203

帖子

0

精华

资深版主

Rank: 8Rank: 8

积分
2923
金钱
2923
注册时间
2019-8-16
在线时间
315 小时
发表于 2020-5-14 12:30:25 | 显示全部楼层
帮顶!!
回复

使用道具 举报

22

主题

2251

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
4471
金钱
4471
注册时间
2013-4-22
在线时间
335 小时
发表于 2020-5-14 13:09:04 | 显示全部楼层
这个还不好说,要么解析错误,要么芯片坏球了
回复

使用道具 举报

0

主题

1

帖子

0

精华

新手上路

积分
35
金钱
35
注册时间
2022-12-14
在线时间
10 小时
发表于 2022-12-15 15:05:40 | 显示全部楼层
三叶草 发表于 2020-5-14 13:09
这个还不好说,要么解析错误,要么芯片坏球了

用逻辑分析仪采集一下信号分析一下,应该是没有采集到数据
回复

使用道具 举报

0

主题

2

帖子

0

精华

新手入门

积分
16
金钱
16
注册时间
2023-12-27
在线时间
5 小时
发表于 2023-12-29 16:20:31 | 显示全部楼层
你解决了没有啊哥
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-23 13:14

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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