📄 sht11.h
字号:
#include "math.h" /*Keil library*/
#include "stdio.h" /*Keil library*/
#include "absacc.h"
#define ACK 1
#define noACK 0
#define measure_temp 0x03 //测量温度命令
#define measure_humi 0x05 //测量湿度命令
#define RESET 0x1e //软启动
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit DATA = P1^0;
sbit SCK = P1^1;
uint sht11[2];
typedef union /*保存所测得的温度&湿度值*/
{
uint i;
float f;
}value;
typedef struct __SHT11__
{
unsigned char gewei;
unsigned char shiwei;
unsigned char DateString1[5];
unsigned char DateString2[5];
}SHT11;
char write_byte(unsigned char value)
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) /*连续向右移动8位*/
{
if (i & value) DATA=1; /*把相应的位送数据线*/
else DATA=0;
SCK=1; /*时序脉冲,应严格按着此标准*/
_nop_();
_nop_();
_nop_(); /*大约0.5us*/
SCK=0;
}
DATA=1; /*释放数据线*/
SCK=1; /*第9位作为响应位*/
error=DATA; /*检测响应情况,如果有响应数据线就会被SHT10拉低*/
SCK=0;
return error; /*返回1表示没响应*/
}
char read_byte(unsigned char ack)
{
unsigned char i,val=0;
DATA=1; /*释放数据线*/
for (i=0x80;i>0;i/=2) /*连续向右移动8位*/
{ SCK=1; /*clk for SENSI-BUS*/
if (DATA) val=(val | i); /*read bit */
SCK=0;
}
DATA=!ack; /*当ack=1时拉低数据线*/
SCK=1; /*clk #9 for ack*/
_nop_();_nop_();_nop_(); /*pulswith approx. 5 us */
SCK=0;
DATA=1; /*释放数据线*/
return val;
}
void transstart(void)
{
DATA=1;
SCK=0; //初始状态
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
void connectionreset(void)//复位:当DATA线处于高低平时,触发SCK9次以上(含9次),此后应接发一个"传输开始"命令
{
unsigned char i;
DATA=1; SCK=0; /*Initial state*/
for(i=0;i<9;i++) /*9 SCK cycles*/
{
SCK=1;
SCK=0;
}
transstart(); /*transmission start*/
}
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;
transstart(); /*transmission start*/
switch(mode){ /*send command to sensor*/
case 0 : error+=write_byte(measure_temp); break;
case 1 : error+=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) =read_byte(ACK); /*read the first byte (MSB)*/
*(p_value+1)=read_byte(ACK); /*read the second byte (LSB)*/
*p_checksum =read_byte(noACK); /*read checksum*/
return error;
}
//温湿度值标度变换及温度补偿
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]*/
}
/*************************************************************************************/
void DateToString(SHT11 *SHT,float datax,float datax1)
{
uint i;
i=(int)datax;
SHT->shiwei=i/10;
SHT->gewei =i%10;
SHT->DateString1[0] = SHT->shiwei + '0';
SHT->DateString1[1] = SHT->gewei + '0';
i=(int)((datax-i)*10);
SHT->DateString1[2] ='.';
SHT->DateString1[3] = i + '0';
SHT->DateString1[4] = '\0';
if(0<=i<=4)
sht11[0] = (int)datax;
else
sht11[0] = (int)(datax+1);
i=(int)datax1;
SHT->shiwei=i/10;
SHT->gewei =i%10;
SHT->DateString2[0] = SHT->shiwei + '0';
SHT->DateString2[1] = SHT->gewei + '0';
i=(int)((datax1-i)*10);
SHT->DateString2[2] ='.';
SHT->DateString2[3] = i + '0';
SHT->DateString2[4] = '\0';
if(0<=i<=4)
sht11[1] = (int)datax1;
else
sht11[1] = (int)(datax1+1);
}
/*************************************************************************************/
void TEM_HUM_DISP(void)
{
SHT11 S1;
value humi_val,temp_val;
unsigned char error,checksum;
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,1);
error+=s_measure((unsigned char*) &temp_val.i,&checksum,0);
if(error!=0)
connectionreset();
else
{
humi_val.f=(float)humi_val.i;
temp_val.f=(float)temp_val.i;
calc_sth11(&humi_val.f,&temp_val.f);
DateToString(&S1,temp_val.f,humi_val.f);
setPosition(2,2);
writeString(S1.DateString1);
setPosition(2,6);
writeString(S1.DateString2);
}
}
/*************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -