📄 sht.c
字号:
/****************************************************************
*文件名:sht.c
*描述:提供数字式温湿度传感器的低层驱动程序
*环境:KEIL C51
*版本:v1.0
*作者:wentsing
*日期:2006/03/07
******************************************************************/
#include "include.h"
#define STH_C 1
#define CRC8 1 //需要CRC8计算
/****************************************************************
*函数性质:SHT专用
*入口:要写入的SHT的数据unsigned char value
*出口:无
*功能:writes a byte on the Sensibus and checks the acknowledge
*调用方式:uchar ShtWriteByte(unsigned char value)
*****************************************************************/
uchar ShtWriteByte(unsigned char value)
{
unsigned char i,error = 0;
for (i = 0x80; i > 0; i >>= 1) //shift bit for masking
{
if (i & value)
DATA = 1; //masking value with i , write to SENSI-BUS
else DATA = 0;
SCK = 1; //clk for SENSI-BUS
NOP();NOP();NOP(); //pulswith approx. 5 us
SCK = 0;
}
DATA = 1; //release DATA-line
SCK = 1; //clk #9 for ack
error = DATA; //check ack (DATA will be pulled down by SHT11)
SCK = 0;
return error; //error=1 in case of no acknowledge
}
/****************************************************************
*函数性质:SHT专用
*入口:要响应SHT的数据unsigned char ack
*出口:返回读取的值
*功能:reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
*调用方式:uchar ShtReadByte(unsigned char ack
*****************************************************************/
uchar ShtReadByte(unsigned char ack)
{
unsigned char i,val = 0;
DATA = 1; //release DATA-line
for ( i = 0x80; i > 0; i >>= 1) //shift bit for masking
{
SCK = 1; //clk for SENSI-BUS
if (DATA)
val | = i; //read bit
SCK = 0;
}
DATA =! ack; //in case of "ack==1" pull down DATA-Line
SCK = 1; //clk #9 for ack
NOP();NOP();NOP(); //pulswith approx. 5 us
SCK = 0;
DATA = 1; //release DATA-line
return val;
}
/****************************************************************
*函数性质:SHT专用
*入口:无
*出口:无
*功能:generates a transmission start for SHT
*调用方式:void ShtTransStart(void)
*****************************************************************/
void ShtTransStart(void)
//---------------------------------------------------------------
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
//---------------------------------------------------------------
{
DATA = 1; SCK = 0; //Initial state
NOP();
SCK = 1;
NOP();
DATA = 0;
NOP();
SCK = 0;
NOP();NOP();NOP();
SCK = 1;
NOP();
DATA = 1;
NOP();
SCK = 0;
}
/****************************************************************
*函数性质:专用
*入口:无
*出口:无
*功能:communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
*调用方式:ShtConnectRst(void)
*****************************************************************/
void ShtConnectRst(void)
//---------------------------------------------------------------
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
//---------------------------------------------------------------
{
unsigned char i;
DATA = 1; SCK = 0; //Initial state
for(i = 0; i < 9; i++) //9 SCK cycles
{
SCK=1;
SCK=0;
}
ShtTransStart(); //transmission start
}
/****************************************************************
*函数性质:公共
*入口:无
*出口:返回成功或者失败
*功能:resets the sensor by a softreset
*调用方式:uchar ShtSoftReset(void)
*****************************************************************/
uchar ShtSoftReset(void)
{
unsigned char error = 0;
ShtConnectRst(); //reset communication
error += ShtWriteByte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
/****************************************************************
*函数性质:公共
*入口:读入数据缓冲 unsigned char *p_value
*出口:返回成功或者失败
*功能:reads the status register with checksum (8-bit)
*调用方式:uchar ShtReadStatusReg(unsigned char *p_value)
*****************************************************************/
uchar ShtReadStatusReg(unsigned char *p_value)
{
unsigned char error = 0,crc;
ShtTransStart(); //transmission start
error = ShtWriteByte(STATUS_REG_R); //send command to sensor
*p_value = ShtReadByte(1); //read status register (8-bit)
crc = ShtReadByte(0); //read checksum (8-bit)
if(crc != CalCRC8(p_value, 1))
error++;
return error; //error=1 in case of no response form the sensor or crc check error
}
/****************************************************************
*函数性质:公共
*入口:要写入的状态寄存器数据 unsigned char p_value
*出口:返回成功或者失败
*功能:writes the status register with checksum (8-bit)
*调用方式:uchar ShtWriteStatusReg(unsigned char p_value)
*****************************************************************/
uchar ShtWriteStatusReg(unsigned char p_value)
{
unsigned char error = 0;
ShtTransStart(); //transmission start
error += ShtWriteByte(STATUS_REG_W); //send command to sensor
error += ShtWriteByte(p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
/****************************************************************
*函数性质:公共
*入口:返回值数据缓冲 unsigned char *p_value 测量类型unsigned char mode
*出口:返回成功或者失败
*功能:makes a measurement (humidity/temperature) with checksum
*调用方式:unsigned char ShtMeasure(unsigned char *p_value, unsigned char mode)
*****************************************************************/
unsigned char ShtMeasure(unsigned char *p_value, unsigned char mode)
{
unsigned char error = 0,crc;
unsigned int i;
ShtTransStart(); //transmission start
switch(mode)
{ //send command to sensor
case TEMP :
error += ShtWriteByte(MEASURE_TEMP);
break;
case HUMI :
error += ShtWriteByte(MEASURE_HUMI);
break;
default :
break;
}
for (i = 0; i < 65535; i++)
if(DATA == 0)
break; //wait until sensor has finished the measurement
if(DATA)
error ++; // or timeout (~2 sec.) is reached
*p_value++ = ShtReadByte(1); //read the first byte (MSB)
*p_value = ShtReadByte(1); //read the second byte (LSB)
crc = ShtReadByte(0); //read checksum
if(crc != CalCRC8(p_value, 2))
error++;
return error;
}
/****************************************************************
*函数性质:公共
*入口:返回值湿度缓冲float *p_humidity ,返回值温度缓冲float *p_temperature
*出口:无
*功能:// calculates temperature [癈] and humidity [%RH]
*调用方式:void calc_sth11(float *p_humidity ,float *p_temperature)
*****************************************************************/
void calc_sth11(float *p_humidity ,float *p_temperature)
{
const float C1 = -4.0; // for 12 Bit
const float C2 =+ 0.0405; // for 12 Bit
const float C3 =- 0.0000028; // for 12 Bit
const float T1 =+ 0.01; // for 14 Bit @ 5V
const float T2 =+ 0.00008; // for 14 Bit @ 5V
float rh = *p_humidity; // rh: Humidity [Ticks] 12 Bit
float t = *p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [癈]
t_C = t*0.01 - 40; //calc. temperature from ticks to [癈]
rh_lin = C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true = (t_C - 25)*(T1 + T2*rh) + rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)
rh_true = 100; //cut if the value is outside of
if(rh_true<0.1)
rh_true = 0.1; //the physical possible range
*p_temperature=t_C; //return temperature [癈]
*p_humidity=rh_true; //return humidity[%RH]
}
/****************************************************************
*函数性质:公共
*入口:humidity [%RH], temperature [癈]
*出口:露点值
*功能:// calculates dew point
*调用方式:float calc_dewpoint(float h,float t)
*****************************************************************/
#if DEWPOINT == true
float calc_dewpoint(float h,float t)
{
float logEx,dew_point;
logEx = 0.66077 + 7.5*t/(237.3 + t) + (log10(h) - 2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -