stm32cubemx利用硬件IIC来驱动sht85

目录

1.cubemx的IIC配置

 2.SHT85的代码驱动


1.cubemx的IIC配置

        本博文的代码,STM32f407ZET6的硬件IIC2的实例:

 2.SHT85的代码驱动

 

#include "SHT85.h"


#include "i2c.h"

#define SHT85_ADDR_WRITE    0x44<<1         //10001000
#define SHT85_ADDR_READ     (0x44<<1)+1     //10001011

#define SHT85_CRC8_POLYNOMIAL 0x31

uint8_t CheckCrc8(uint8_t* const message, uint8_t initial_value)
{
    uint8_t  remainder;     //余数
    uint8_t  i = 0, j = 0;  //循环变量

    /* 初始化 */
    remainder = initial_value;

    for(j = 0; j < 2;j++)
    {
        remainder ^= message[j];

        /* 从最高位开始依次计算  */
        for (i = 0; i < 8; i++)
        {
            if (remainder & 0x80)
            {
                remainder = (remainder << 1)^SHT85_CRC8_POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
    }

    /* 返回计算的CRC码 */
    return remainder;
}
/**
 * @brief   向SHT85发送一条指令(16bit)
 * @param   cmd —— SHT85指令(在etCommands中枚举定义)
 * @retval  成功返回HAL_OK
*/
static uint8_t SHT85_Send_Cmd(etCommands cmd)
{
    uint8_t cmd_buffer[2];
    cmd_buffer[0] = cmd >> 8;
    cmd_buffer[1] = cmd;
    return HAL_I2C_Master_Transmit(&hi2c2, SHT85_ADDR_WRITE, (uint8_t*) cmd_buffer, 2, 0x00FF);
}
/**
 * @brief   复位SHT85
 * @param   none
 * @retval  none
*/
void SHT85_reset(void)
{
    SHT85_Send_Cmd(CMD_SOFT_RESET);
    HAL_Delay(20);
}
/**
 * @brief   初始化SHT85
 * @param   none
 * @retval  成功返回HAL_OK
 * @note    周期测量模式
*/
uint8_t SHT85_Init(void)
{
    return SHT85_Send_Cmd(CMD_MEAS_PERI_4_M);
}
/**
 * @brief   从SHT85读取一次数据
 * @param   dat —— 存储读取数据的地址(6个字节数组)
 * @retval  成功 —— 返回HAL_OK
*/
uint8_t SHT85_Read_Dat(uint8_t* dat)
{
    SHT85_Send_Cmd(CMD_FETCH_DATA);
    return HAL_I2C_Master_Receive(&hi2c2, SHT85_ADDR_READ, dat, 6, 0xFFFF);
}
/**
 * @brief   将SHT85接收的6个字节数据进行CRC校验,并转换为温度值和湿度值
 * @param   dat  —— 存储接收数据的地址(6个字节数组)
 * @retval  校验成功  —— 返回0
 *          校验失败  —— 返回1,并设置温度值和湿度值为0
*/
uint8_t SHT85_Dat_To_Float(uint8_t* const dat, float* temperature, float* humidity)
{
    uint16_t recv_temperature = 0;
    uint16_t recv_humidity = 0;

    /* 校验温度数据和湿度数据是否接收正确 */
    if(CheckCrc8(dat, 0xFF) != dat[2] || CheckCrc8(&dat[3], 0xFF) != dat[5])
        return 1;

    /* 转换温度数据 */
    recv_temperature = ((uint16_t)dat[0]<<8)|dat[1];
    *temperature = -45 + 175*((float)recv_temperature/65535);

    /* 转换湿度数据 */
    recv_humidity = ((uint16_t)dat[3]<<8)|dat[4];
    *humidity = 100 * ((float)recv_humidity / 65535);

    return 0;
}

      

#ifndef  __SHT85_H__
#define  __SHT85_H__

#include "main.h"



// Sensor Commands
typedef enum {
  CMD_READ_SERIALNBR = 0x3780, // read serial number
  CMD_READ_STATUS    = 0xF32D, // read status register
  CMD_CLEAR_STATUS   = 0x3041, // clear status register
  CMD_HEATER_ENABLE  = 0x306D, // enabled heater
  CMD_HEATER_DISABLE = 0x3066, // disable heater
  CMD_SOFT_RESET     = 0x30A2, // soft reset
  CMD_MEAS_SINGLE_H  = 0x2400, // single meas., high repeatability
  CMD_MEAS_SINGLE_M  = 0x240B, // single meas., medium repeatability
  CMD_MEAS_SINGLE_L  = 0x2416, // single meas., low repeatability
  CMD_MEAS_PERI_05_H = 0x2032, // periodic meas. 0.5 mps, high repeatability
  CMD_MEAS_PERI_05_M = 0x2024, // periodic meas. 0.5 mps, medium repeatability
  CMD_MEAS_PERI_05_L = 0x202F, // periodic meas. 0.5 mps, low repeatability
  CMD_MEAS_PERI_1_H  = 0x2130, // periodic meas. 1 mps, high repeatability
  CMD_MEAS_PERI_1_M  = 0x2126, // periodic meas. 1 mps, medium repeatability
  CMD_MEAS_PERI_1_L  = 0x212D, // periodic meas. 1 mps, low repeatability
  CMD_MEAS_PERI_2_H  = 0x2236, // periodic meas. 2 mps, high repeatability
  CMD_MEAS_PERI_2_M  = 0x2220, // periodic meas. 2 mps, medium repeatability
  CMD_MEAS_PERI_2_L  = 0x222B, // periodic meas. 2 mps, low repeatability
  CMD_MEAS_PERI_4_H  = 0x2334, // periodic meas. 4 mps, high repeatability
  CMD_MEAS_PERI_4_M  = 0x2322, // periodic meas. 4 mps, medium repeatability
  CMD_MEAS_PERI_4_L  = 0x2329, // periodic meas. 4 mps, low repeatability
  CMD_MEAS_PERI_10_H = 0x2737, // periodic meas. 10 mps, high repeatability
  CMD_MEAS_PERI_10_M = 0x2721, // periodic meas. 10 mps, medium repeatability
  CMD_MEAS_PERI_10_L = 0x272A, // periodic meas. 10 mps, low repeatability
  CMD_FETCH_DATA     = 0xE000, // readout measurements for periodic mode
  CMD_BREAK          = 0x3093, // stop periodic measurement
} etCommands;

// Single Shot Measurement Repeatability
typedef enum {
  SINGLE_MEAS_LOW        = CMD_MEAS_SINGLE_L, // low repeatability
  SINGLE_MEAS_MEDIUM     = CMD_MEAS_SINGLE_M, // medium repeatability
  SINGLE_MEAS_HIGH       = CMD_MEAS_SINGLE_H  // high repeatability
} etSingleMeasureModes;

// Periodic Measurement Configurations
typedef enum {
  PERI_MEAS_LOW_05_HZ    = CMD_MEAS_PERI_05_L,
  PERI_MEAS_LOW_1_HZ     = CMD_MEAS_PERI_1_L,
  PERI_MEAS_LOW_2_HZ     = CMD_MEAS_PERI_2_L,
  PERI_MEAS_LOW_4_HZ     = CMD_MEAS_PERI_4_L,
  PERI_MEAS_LOW_10_HZ    = CMD_MEAS_PERI_10_L,
  PERI_MEAS_MEDIUM_05_HZ = CMD_MEAS_PERI_05_M,
  PERI_MEAS_MEDIUM_1_HZ  = CMD_MEAS_PERI_1_M,
  PERI_MEAS_MEDIUM_2_HZ  = CMD_MEAS_PERI_2_M,
  PERI_MEAS_MEDIUM_4_HZ  = CMD_MEAS_PERI_4_M,
  PERI_MEAS_MEDIUM_10_HZ = CMD_MEAS_PERI_10_M,
  PERI_MEAS_HIGH_05_HZ   = CMD_MEAS_PERI_05_H,
  PERI_MEAS_HIGH_1_HZ    = CMD_MEAS_PERI_1_H,
  PERI_MEAS_HIGH_2_HZ    = CMD_MEAS_PERI_2_H,
  PERI_MEAS_HIGH_4_HZ    = CMD_MEAS_PERI_4_H,
  PERI_MEAS_HIGH_10_HZ   = CMD_MEAS_PERI_10_H,
} etPeriodicMeasureModes;




uint8_t CheckCrc8(uint8_t* const message, uint8_t initial_value);
	

uint8_t SHT85_Init(void);
void SHT85_reset(void);
uint8_t SHT85_Read_Dat(uint8_t* dat);
uint8_t SHT85_Dat_To_Float(uint8_t* const dat, float* temperature, float* humidity);







#endif



欢迎评论,指教。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值