//=============================================================================
// S E N S I R I O N
//=============================================================================
// Project : SHT3x Sample Code (V1.1)
// File : sht3x.c (V1.1)
// Brief : Sensor Layer: Implementation of functions for sensor access.
//=============================================================================
//-- Includes -----------------------------------------------------------------
#include <stdio.h>
#include "sht3x.h"
//-- Defines ------------------------------------------------------------------
// Generator polynomial for CRC
#define POLYNOMIAL 0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001
//=============================================================================
// IO-Pins /* -- adapt the defines for your uC -- */
//-----------------------------------------------------------------------------
#ifndef I2C_ONLY
// Reset on port B, bit 12
#define RESET_LOW() (GPIOB->BSRR = 0x10000000) // set Reset to low
#define RESET_HIGH() (GPIOB->BSRR = 0x00001000) // set Reset to high
// Alert on port B, bit 10
#define ALERT_READ (GPIOB->IDR & 0x0400) // read Alert
#else
// Reset
#define RESET_LOW() ; // set Reset to low
#define RESET_HIGH() ; // set Reset to high
// Alert
#define ALERT_READ (0x00) // read Alert
#endif
//=============================================================================
//-- Global variables ---------------------------------------------------------
static uint8 _i2cAddress; // I2C Address
//-- Static function prototypes -----------------------------------------------
static etError SHT3X_WriteAlertLimitData(float humidity, float temperature);
static etError SHT3X_ReadAlertLimitData(float* humidity, float* temperature);
static etError SHT3X_StartWriteAccess(void);
static etError SHT3X_StartReadAccess(void);
static void SHT3X_StopAccess(void);
static etError SHT3X_WriteCommand(etCommands command);
static etError SHT3X_Read2BytesAndCrc(uint16* data, etI2cAck finaleAckNack,
uint8 timeout);
static etError SHT3X_Write2BytesAndCrc(uint16 data);
static uint8 SHT3X_CalcCrc(uint8 data[], uint8 nbrOfBytes);
static etError SHT3X_CheckCrc(uint8 data[], uint8 nbrOfBytes, uint8 checksum);
static float SHT3X_CalcTemperature(uint16 rawValue);
static float SHT3X_CalcHumidity(uint16 rawValue);
static uint16 SHT3X_CalcRawTemperature(float temperature);
static uint16 SHT3X_CalcRawHumidity(float humidity);
//-----------------------------------------------------------------------------
etError SHT3X_Init(uint8 i2cAddress) /* -- adapt the init for your uC -- */
{
etError error = NO_ERROR; // error code
#if 0 //原版
// init I/O-pins 初始化MCU控制复位Reset、警报Alert引脚功能
;// Alert引脚
;// Reset引脚
RESET_LOW();
I2c_Init(); //Init I2C
SHT3X_SetI2cAdr(i2cAddress);
//Release reset
RESET_HIGH();
return error;
#else
uint32 serialNumber;// serial number
//Init I2C
I2c_Init();
SHT3X_SetI2cAdr(i2cAddress);
//Reset
SHT3X_SoftReset();
// wait 50ms after power on
DelayMicroSeconds(50000);
error = SHT3x_ReadSerialNumber(&serialNumber);
/*
(void)serialNumber;//消除编译器警告
*/
//printf("SHT3X Serial Number = %lu\r\n", serialNumber);
return error;
#endif
}
//-----------------------------------------------------------------------------
void SHT3X_SetI2cAdr(uint8 i2cAddress)
{
_i2cAddress = i2cAddress;
}
//-----------------------------------------------------------------------------
etError SHT3x_ReadSerialNumber(uint32* serialNumber)
{
etError error; // error code
uint16 serialNumWords[2];
error = SHT3X_StartWriteAccess();
// write "read serial number" command
error |= SHT3X_WriteCommand(CMD_READ_SERIALNBR);
// if no error, start read access
if(error == NO_ERROR) error = SHT3X_StartReadAccess();
// if no error, read first serial number word
if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&serialNumWords[0], ACK, 100);
// if no error, read second serial number word
if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&serialNumWords[1], NACK, 0);
SHT3X_StopAccess();
// if no error, calc serial number as 32-bit integer
if(error == NO_ERROR)
{
*serialNumber = ((uint32)serialNumWords[0] << 16) | (uint32)serialNumWords[1];
}
return error;
}
//-----------------------------------------------------------------------------
etError SHT3X_ReadStatus(uint16* status)
{
etError error; // error code
error = SHT3X_StartWriteAccess();
// if no error, write "read status" command
if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_READ_STATUS);
// if no error, start read access
if(error == NO_ERROR) error = SHT3X_StartReadAccess();
// if no error, read status
if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(status, NACK, 0);
SHT3X_StopAccess();
return error;
}
//-----------------------------------------------------------------------------
etError SHT3X_ClearAllAlertFlags(void)
{
etError error; // error code
error = SHT3X_StartWriteAccess();
// if no error, write clear status register command
if(error == NO_ERROR) error = SHT3X_WriteCommand(CMD_CLEAR_STATUS);
SHT3X_StopAccess();
return error;
}
//-----------------------------------------------------------------------------
etError SHT3X_GetTempAndHumi(float* temperature, float* humidity,
etRepeatability repeatability, etMode mode,
uint8 timeout)
{
etError error;
switch(mode)
{
case MODE_CLKSTRETCH: // get temperature with clock stretching mode
error = SHT3X_GetTempAndHumiClkStretch(temperature, humidity,
repeatability, timeout);
break;
case MODE_POLLING: // get temperature with polling mode
error = SHT3X_GetTempAndHumiPolling(temperature, humidity,
repeatability, timeout);
break;
default:
error = PARM_ERROR;
break;
}
return error;
}
//-----------------------------------------------------------------------------
etError SHT3X_GetTempAndHumiClkStretch(float* temperature, float* humidity,
etRepeatability repeatability,
uint8 timeout)
{
etError error; // error code
uint16 rawValueTemp; // temperature raw value from sensor
uint16 rawValueHumi; // humidity raw value from sensor
error = SHT3X_StartWriteAccess();
// if no error ...
if(error == NO_ERROR)
{
// start measurement in clock stretching mode
// use depending on the required repeatability, the corresponding command
switch(repeatability)
{
case REPEATAB_LOW:
error = SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_L);
break;
case REPEATAB_MEDIUM:
error = SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_M);
break;
case REPEATAB_HIGH:
error = SHT3X_WriteCommand(CMD_MEAS_CLOCKSTR_H);
break;
default:
error = PARM_ERROR;
break;
}
}
// if no error, start read access
if(error == NO_ERROR) error = SHT3X_StartReadAccess();
// if no error, read temperature raw values
if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&rawValueTemp, ACK, timeout);
// if no error, read humidity raw values
if(error == NO_ERROR) error = SHT3X_Read2BytesAndCrc(&rawValueHumi, NACK, 0);
SHT3X
- 1
- 2
前往页