编辑: QQ215851406 | 2019-07-12 |
h' #include 'stdio.h' Typedef union //定义一 公用体 { Unsigned int i;
Float f;
} value;
Enum{temp,humi}定义一枚举变量 #define data p1.1 //虚拟p1.1为数据线 #define sck p1.0 //虚拟p1.0为时钟线 #define reset p0.0 #define erase p0.1 #define stop p0.2 #define fwd p0.3 #define bwd p0.4 #define noack o #define ack
1 #define STATUS_REG_W 0x06 //000
0011 0 #define STATUS_REG_R 0x07 //000
0011 1 #define MEASURE_TEMP 0x03 //000
0001 1 #define MEASURE_HUMI 0x05 //000
0010 1 #define RESET 0x1e //000
1111 0 s_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;
} char s_write_byte(unsigned char value)//输入命令 { Unsigned char i,erro=0;
For (i=0x80;
i>0;
i\=2) {if (i&value) data=1;
//按位与传输数据 Else data=0;
Sck=1;
_nop_();
_nop_();
_nop_();
Sck=0;
} _nop_();
Sck=1;
Erro=data;
//sht11使data脚为低电平表示收到命令 Sck=0;
_nop_();
Data=1;
//释放data脚为高电平 Return erro;
} Char s_read byte(unsigned char ack)//读测量数据 { Unsigned char,value=0;
Data=1;
For(i=0x80;
i>0,i\=2)//十六位数据 {sck=1;
If(data) value=(value|i);
//利用按位或来获取数据 Sck=0;
} Data=!ack;
//通过低电平表示应答位 Sck=1;
//下一个时钟脉冲 _nop_();
_nop_();
_nop_();
Sck=0;
data=1;
//释放数据线 return value;
} void s_connectionreset(void)//通信复位 {unsigned char i;
Data=1,sck=0;
For(i=0;
ib) {for(i=0;
i0) j+=1;
if(fwd>0&&bwd1) reset=1;
} void calc_sth11(float *p_humidity ,float *p_temperature)//计算温湿度值 { const float C1=-4.0;
//
12 Bit精度 const float C2= 0.0405;
//
12 Bit精度 const float C3=-0.0000028;
//
12 Bit精度 const float T1=0.01;
//
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 [_C] t_C=t*0.01 C 40;
//calc. Temperature from ticks to [_C] 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