STM32cubemx配置硬件IIC如圖所示
?SHT40的驅(qū)動(dòng)代碼
#include "SHT40.h"
#include "i2c.h"
#include <rtthread.h>
#include <rtdbg.h>
#define SHT40_ADDR_WRITE 0x44<<1 //10001000
#define SHT40_ADDR_READ (0x44<<1)+1 //10001011
#define CRC8_POLYNOMIAL 0x31
uint8_t SHT40_CheckCrc8(uint8_t* const message, uint8_t initial_value)
{
uint8_t remainder; //余數(shù)
uint8_t i = 0, j = 0; //循環(huán)變量
/* 初始化 */
remainder = initial_value;
for(j = 0; j < 2;j++)
{
remainder ^= message[j];
/* 從最高位開(kāi)始依次計(jì)算 */
for (i = 0; i < 8; i++)
{
if (remainder & 0x80)
{
remainder = (remainder << 1)^CRC8_POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}
}
/* 返回計(jì)算的CRC碼 */
return remainder;
}
/**
* @brief 向SHT40發(fā)送一條指令(16bit)
* @param cmd —— SHT40指令(在etCommands中枚舉定義)
* @retval 成功返回HAL_OK
*/
static uint8_t SHT40_Send_Cmd(SHT40Command cmd)
{
uint8_t cmd_buffer = 0;
cmd_buffer = cmd ;
return HAL_I2C_Master_Transmit(&hi2c1, SHT40_ADDR_WRITE, (uint8_t*)&cmd_buffer, 1, 0x00FF);
}
/**
* @brief 復(fù)位SHT40
* @param none
* @retval none
*/
void SHT40_reset(void)
{
SHT40_Send_Cmd(SHT4X_CMD_SOFT_RESET);
// HAL_Delay(20);
rt_thread_mdelay(20);
}
/**
* @brief 初始化SHT40
* @param none
* @retval 成功返回HAL_OK
* @note 周期測(cè)量模式
*/
uint8_t SHT40_Init(void)
{
return SHT40_Send_Cmd(SHT4X_CMD_MEASURE_HPM);
}
/**
* @brief 從SHT40讀取一次數(shù)據(jù)
* @param dat —— 存儲(chǔ)讀取數(shù)據(jù)的地址(6個(gè)字節(jié)數(shù)組)
* @retval 成功 —— 返回HAL_OK
*/
uint8_t SHT40_Read_Dat(uint8_t* dat)
{
SHT40_Send_Cmd(SHT4X_CMD_MEASURE_HPM);
rt_thread_mdelay(10);
return HAL_I2C_Master_Receive(&hi2c1, SHT40_ADDR_READ, dat, 6, 0xFFFF);
}
/**
* @brief 將SHT40接收的6個(gè)字節(jié)數(shù)據(jù)進(jìn)行CRC校驗(yàn),并轉(zhuǎn)換為溫度值和濕度值
* @param dat —— 存儲(chǔ)接收數(shù)據(jù)的地址(6個(gè)字節(jié)數(shù)組)
* @retval 校驗(yàn)成功 —— 返回0
* 校驗(yàn)失敗 —— 返回1,并設(shè)置溫度值和濕度值為0
*/
uint8_t SHT40_Dat_To_Float(uint8_t* const dat, float* temperature, float* humidity)
{
uint16_t recv_temperature = 0;
uint16_t recv_humidity = 0;
/* 校驗(yàn)溫度數(shù)據(jù)和濕度數(shù)據(jù)是否接收正確 */
if(SHT40_CheckCrc8(dat, 0xFF) != dat[2] || SHT40_CheckCrc8(&dat[3], 0xFF) != dat[5])
return 1;
/* 轉(zhuǎn)換溫度數(shù)據(jù) */
recv_temperature = ((uint16_t)dat[0]<<8)|dat[1];
*temperature = -45 + 175*((float)recv_temperature/65535);
/* 轉(zhuǎn)換濕度數(shù)據(jù) */
recv_humidity = ((uint16_t)dat[3]<<8)|dat[4];
*humidity = -6 + 125 * ((float)recv_humidity / 65535);
if(*humidity>=100) //根據(jù)數(shù)據(jù)手冊(cè)編寫
*humidity=100;
else if(*humidity<=0)
*humidity=0;
return 0;
}
#ifndef __SHT40_H__
#define __SHT40_H__
#include "main.h"
// Sensor Commands
typedef enum {
SHT4X_CMD_MEASURE_HPM = 0xFD, //measure T & RH with high precision (high repeatability)
SHT4X_CMD_MEASURE_MPM = 0xF6, //measure T & RH with medium precision (medium repeatability)
SHT4X_CMD_MEASURE_LPM = 0xE0, //measure T & RH with lowest precision (low repeatability)
SHT4X_CMD_READ_SERIAL = 0x89, //read serial number
SHT4X_CMD_SOFT_RESET = 0x94, //soft reset
SHT4X_CMD_200W_1S = 0x39, //activate heater with 200mW for 1s, including a high precision measurement just before deactivation
SHT4X_CMD_200W_01S = 0x32, //activate heater with 200mW for 0.1s including a high precision measurement just before deactivation
SHT4X_CMD_110W_1S = 0x2F, //activate heater with 110mW for 1s including a high precision measurement just before deactivation
SHT4X_CMD_110W_01S = 0x24, //activate heater with 110mW for 0.1s including a high precision measurement just before deactivation
SHT4X_CMD_20W_1S = 0x1E, //activate heater with 20mW for 1s including a high precision measurement just before deactivation
SHT4X_CMD_20W_01S = 0x15, //activate heater with 20mW for 0.1s including a high precision measurement just before deactivation
}SHT40Command;
uint8_t SHT40_CheckCrc8(uint8_t* const message, uint8_t initial_value);
uint8_t SHT40_Init(void);
void SHT40_reset(void);
uint8_t SHT40_Read_Dat(uint8_t* dat);
uint8_t SHT40_Dat_To_Float(uint8_t* const dat, float* temperature, float* humidity);
#endif
注意:在讀sht40的寄存器之后一定要加10幾個(gè)ms的延時(shí)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-656120.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-656120.html
到了這里,關(guān)于STM32Cubemxhal庫(kù)硬件IIC驅(qū)動(dòng)SHT40溫濕度傳感器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!