📄 rw_sht.c
字号:
/*****************************************************************************************************
* Filename: SHT91_Sample_code
* File func: the command to write/read the sht_sensor
* File description: the file is included in "main.c" to fulfill the read_write command
* Prozessor: 80C51 Family
* Compiler: Keil Wave IDE
* Author: qcx
* Version: v1.1
*********************************************************************************************************/
//-------------------------------------------------------------------------
#include "math.h"
#include "intrins.h"
#include "sht_demo.h"
//--------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//--------------------------------------------------------------------------
//writes a byte on the Sensibu and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if(i & value) DATA=1; //masking value with i,write every bit of value
else DATA=0; //to SENSI-BUS
SCK=1; //clk for SENSI-BUS
_nop_ (); //The frequancy of the clk should be less than
_nop_ (); //4k Hz
_nop_ ();
SCK=0;
}
DATA=1;
SCK=1;
error=DATA;
SCK=0;
return error; //error=1 in case of no acknowledge
}
//-----------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//-----------------------------------------------------------------------
//func: read a byte from Sens-bus and gives an acknowledge
{
unsigned char i,val=0;
for(i=0x80;i>0;i/=2)
{
SCK=1;
if (DATA) val=(val | i);
SCK=0;
}
DATA= !ack; //in case of "ack==1" pull down DATA-Line
SCK=1;
_nop_();
_nop_(); _nop_();
DATA=1;
return val;
}
//-------------------------------------------------------------------------
void s_transstart(void)
//-------------------------------------------------------------------------
//func: generate a transmission start signal
{
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; // refer to the pdf from SENSIRION(start transmission)
}
//------------------------------------------------------------------------------
void s_connectionreset(void)
//------------------------------------------------------------------------------
//func: communication reset:DATA-line and at least 9 sck cycles followed by transstart
{
unsigned char i;
DATA=1;
SCK=0;
for(i=0;i<9;i++)
{
SCK=1;
SCK=0;
}
s_transstart();
}
//--------------------------------------------------------------------------------
char s_softreset(void)
//--------------------------------------------------------------------------------
//func: reset the sensor by a softreset
{
unsigned char error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET -command to sensor
return error; //sensor=1 in case of no response from sensor
}
//-----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//-----------------------------------------------------------------------------------
//func: read the status register with checksum (8 bit)
{
unsigned char 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)
return error; //error=1 in case of no response from sensor
}
//----------------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------------
//func: writes the status register with checksum(8 bit)
{
unsigned char error=0;
s_transstart(); //transmission start
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 from sensor
}
//-----------------------------------------------------------------------------------------
char s_measure(unsigned char *p_value,unsigned char *p_checksum,unsigned char mode)
//-----------------------------------------------------------------------------------------
//func: make a measurement(temperature or humity) with checksum
{
unsigned char error=0;
unsigned int i;
s_transstart(); //start transmission
switch(mode) //send command to sensor
{
case TEMP: error+=s_write_byte(MEASURE_TEMP);break;
case HUMI: error+=s_write_byte(MEASURE_HUMI);break;
default: break;
}
for(i=0;i<65535;i++)
if(DATA==0) break;
if(DATA==1) error+=1;
*(p_value)=s_read_byte(ACK); //read the first byte
*(p_value+1) = s_read_byte(ACK); //read the second byte
*p_checksum = s_read_byte(noACK); //read checksum
return error;
}
//-----------------------------------------------------------------------------------------------
void calc_sth(float *p_humidity,float *p_temperature)
//-----------------------------------------------------------------------------------------------
//func: calculates temperature and humidity
{
const float C1=-4.0;
const float C2=0.0405;
const float C3=-0.0000028;
const float T1=0.01;
const float T2=0.00008;
float rh=*p_humidity;
float t=*p_temperature;
float rh_lin;
float rh_ture;
float t_C;
t_C=t*0.01 - 40;
rh_lin=C3*rh*rh + C2*rh + C1;
rh_ture=(t_C-25)*(T1+T2*rh)+rh_lin;
if(rh_ture > 100) rh_ture=100;
if(rh_ture < 0.1) rh_ture=0.1;
*p_temperature=t_C;
*p_humidity=rh_ture;
}
//--------------------------------------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//----------------------------------------------------------------------------------------------------
//func: calculates the dewpoint
//input: humidity[%RH] , temperature[C]
//output: dew point
{
float k,dew_point;
k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
dew_point = 243.12 * k/(17.62-k);
return dew_point;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -