📄 sht7i.c
字号:
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
unsigned char 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
}
//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets 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; //error=1 in case of no response form the sensor
}
*/
/*
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
s_transstart(); //transmission start
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; //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
Prints("MSB=");
Puthexbyte(*p_value);
Prints("\r\n");
Prints("LSB=");
Puthexbyte(*(p_value+1));
Prints("\r\n");
return error;
}
*/
//----------------------------------------------------------------------------------
char s_humi(uchar *p_value, uchar *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
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
}
//----------------------------------------------------------------------------------
char s_temp(uchar *p_value, uchar *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned int tt;
unsigned char t;
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");
// tt=*((unsigned int *)p_value);
// tt=(tt/100-40);
// t=(tt%100);
// Puthexbyte(tt);
// Putchar('.');
// Puthexbyte(t);
// Prints("\r\n");
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------------
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
printf("temp:%f\n",t_C);
Prints("\r\n");
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
printf("%humi:%f\n",rh_true);
Prints("\r\n");
*p_temperature=t_C; //return temperature
*p_humidity=rh_true; //return humidity[%RH]
}
/*
//--------------------------------------------------------------------
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;
}
*/
void main()
{
value humi_val,temp_val;
// float dew_point;
// char temp,humi;
uchar error,checksum;
// uchar statusreg=0x01;
// uchar status=0;
float f =0.02;
unsigned int i;
for(i=0;i<1000;i++);
InitialIO();
InitialUart();
//Prints("Initial is over\r\n");
//s_connectionreset();
while(1) //调试用程序,试验证明可对寄存器的读取操作;
{ //证明硬件电路没有问题;
// error=0;
// f=f*0.01 - 40;
// Putchar('a');
//f=f*0.01*f - 40*f*f; //calc. temperature from ticks to
//rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
//printf("temp:%f\n",f);
// error+=s_softreset();
// Prints("error=");
// Puthexbyte(error);
// Prints("\r\n");
// error+= s_write_statusreg( &statusreg);
// Prints("error=");
// Puthexbyte(error);
// Prints("\r\n");
// error+= s_read_statusreg( &status,&checksum);
// Prints("error=");
// Puthexbyte(error);
// Prints("\r\n");
error+=s_humi((unsigned char*) &humi_val,&checksum); //measure humidity
error+=s_temp((unsigned char*) &temp_val,&checksum); //measure temperature
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{
humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
}
}
}
/* s_connectionreset();
while(1)
{
error=0;
// error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
// error+=s_softreset();
// Prints("error=");
// Puthexbyte(error);
// Prints("\r\n");
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{
humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
// dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
// printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
// for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
// }
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -