📄 sht.c
字号:
//***********************************************************************
//FILE: sht.c
//功能: 温湿渡传感器SHT11的全部操作
//***********************************************************************
//***********************************************************************
#include "main.h"
#include "intrins.h"
#include "tools.h"
#include "sht.h"
#include "stdio.h"
#include "math.h"
//***********************************************************************
#define _Nop() _nop_(),_nop_(),_nop_() //定义空指令
/*************************************
名称: s_write_byte
功能: 写入一个字节函数
*************************************/
char s_write_byte(uchar tvalue)
// writes a byte on the Sensibus and checks the acknowledge
{
uchar i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & tvalue)
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;
_Nop();
}
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
}
/*************************************
名称: s_read_byte
功能: 读出一个字节函数
*************************************/
char s_read_byte(uchar ack)
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
uchar i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK=1; //clk for SENSI-BUS
_Nop();
if (DATA) val=(val | i); //read bit
SCK=0;
_Nop();
}
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
_Nop(); //pulswith approx. 5 us
SCK=0;
_Nop();
DATA=1; //release DATA-line
return val;
}
/*************************************
名称: s_transstart
功能: 传输开始
*************************************/
void s_transstart(void)
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1; SCK=0; //Initial state
_Nop();
SCK=1;
_Nop();
DATA=0;
_Nop();
SCK=0;
_Nop();
SCK=1;
_Nop();
DATA=1;
_Nop();
SCK=0;
}
/*************************************
名称: s_connectionreset
功能: 重新连接
*************************************/
void s_connectionreset(void)
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
uchar i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SCK=1;
_Nop();
SCK=0;
_Nop();
}
s_transstart(); //transmission start
}
/*************************************
名称: s_read_statusreg
功能: 读状态寄存器
*************************************/
/*
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
// reads the status register with checksum (8-bit)
{
uchar error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
Prints("status=");
Puthexbyte(*p_value);
Prints("\r\n");
return error; //error=1 in case of no response form the sensor
}
*/
/*************************************
名称: s_write_statusreg
功能: 写状态寄存器
*************************************/
/*
char s_write_statusreg(unsigned char *p_value)
// writes the status register with checksum (8-bit)
{
uchar error=0;
//s_transstart(); //transmission start
s_connectionreset(); //后加
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
*/
/*************************************
名称: s_softreset
功能: 重置寄存器
*************************************/
/*
char s_softreset(void)
// resets the sensor by a softreset
{
uchar error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
*/
/*************************************
名称: s_humi
功能: 测量湿度
*************************************/
char s_humi(uchar *p_value, uchar *p_checksum)
// measure the huminity
{
uchar error=0;
uint i;
s_transstart(); //transmission start
error=s_write_byte(MEASURE_HUMI); //send messure huminity command to sensor
for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
Prints("\r\n");
Prints("humi=");
Puthexbyte(*p_value);
Puthexbyte(*(p_value+1));
Prints("\r\n");
return error; //error=1 in case of no response form the sensor
}
/*************************************
名称: s_temp
功能: 测量温度
*************************************/
char s_temp(uchar *p_value, uchar *p_checksum)
// measure the temprature
{
uchar error=0;
uint i;
s_transstart(); //transmission start
error=s_write_byte(MEASURE_TEMP); //send messure tempture command to sensor
for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
Prints("temp=");
Puthexbyte(*p_value);
Puthexbyte(*(p_value+1));
Prints("\r\n");
return error; //error=1 in case of no response form the sensor
}
/*************************************
名称: calc_sth11
功能: 计算温湿渡的值
*************************************/
void calc_sth11(float *p_humidity ,float *p_temperature)
// calculates temperature and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp
{
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 T11=+0.01; // for 14 Bit @ 5V
const float T22=+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)*(T11+T22*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
Humi=rh_true*10;
Temp=t_C*10;
}
/*************************************
名称: calc_sth11
功能: 计算露点值
*************************************/
/*
float calc_dewpoint(float h,float t)
// calculates dew point
// input: humidity [%RH], temperature
// output: dew point
{ 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;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -