baudrate==4800
//FOSC==11.5920MHz;
#include
#include
#define uchar unsigned char
#define uint unsigned int
//*****************************************************************************
*************************************************************
adr
command
000
00011;
temperature
//
// 温 度 测 量 指 令
#define measure_TEMP 0x03
meterage
#define measure_HUMI 0x05
#define status_reg_r
0x07
#define status_reg_w 0x06
#define Reset 0x1e
//*****************************************************************************
****************************************************************************
//湿度测量指令
000
//读寄存器状态指令 000
//写寄存器状态指令 000
//软启动指令
000
00101;
00111
00110
11110;
humidity meterage
//状态寄存器;"0"是默认值
//加热;状态寄存器第三位为"1"时开加热,为"0"时关加热
//状态寄存器第 1 位为"1"时,=8 位相对湿度,12 位温度分辨率。状
#define HotUp
0x04;
#define BitWide 0x01;
态寄存器第 1 位为"0"时,12 位相对湿度,14 位温度分辨率
#define OTP
//*****************************************************************************
************************************************************************
//不从 OTP 重下载
0X02;
bit NOACK = 0;
bit ACK = 1;
sbit sda = P1^0;
sbit scl = P1^1;
enum {TEMP,HUMI};
uchar temp_value[2];
uchar humi_value[2];
uchar check_number;
uchar a1;
//uchar error1;
unsigned char *t_v = temp_value;
unsigned char *h_m = humi_value;
float RH1, TEMPE;
float *t = &TEMPE;
float *h = &RH1;
//*****************************************************************************
/********************void start ()*********************/
/********************起动"sht711"时序***************************/
//sda-----______----
//scl ___---___---___
void start_sht11(void)
{
sda = 1;
scl = 0;
_nop_();_nop_();_nop_();_nop_();
scl = 1;
_nop_();_nop_();_nop_();_nop_();
sda = 0;
_nop_();_nop_();_nop_();_nop_();
scl = 0;
_nop_();_nop_();_nop_();_nop_();
scl = 1;
_nop_();_nop_();_nop_();_nop_();
sda = 1;
_nop_();_nop_();_nop_();_nop_();
scl = 0;
_nop_();_nop_();
}
//******************************************************
//通讯中断后,用以下时序重新连接
//sda---------------------------------------____--
//
//scl__--__--__--__--__--__--__--__--__--__--__--__
void connection_sht11(void)
{
7
1
8
9
2
3
4
5
6
uchar i;
sda =1;
_nop_();_nop_();
scl = 0;
_nop_();_nop_();
for(i=0;i<9;i++)
{
scl = 1;
_nop_();_nop_();_nop_();_nop_();
scl = 0;
_nop_();_nop_();_nop_();_nop_();
}
start_sht11();
}
//****************************************************************
uchar sensor_write(uchar value)
{
uchar i;
uchar error = 0;
for(i=0x80;i>0;i/=2)
{
if(i&value)
sda = 1;
else
sda = 0;
_nop_();_nop_();
scl = 1;
_nop_();_nop_();_nop_();_nop_();
scl = 0;
_nop_();_nop_();_nop_();_nop_();
}
_nop_();_nop_();_nop_();_nop_();
sda = 1;
_nop_();_nop_(); _nop_();_nop_();
scl = 1;
_nop_();_nop_();_nop_();_nop_();
error = sda;
scl = 0;
_nop_();_nop_();
return(error);
//if sda == '0 ',sht11 ack
}
//*******************************************************************
uchar sensor_read(bit ack)
{
uchar i , value = 0 ;
sda = 1;
for(i=0x80;i>0;i/=2)
{
scl = 1;
_nop_();_nop_();_nop_();_nop_();
if(sda)
value = value | i;
scl = 0;
_nop_();_nop_();_nop_();_nop_();
sda = 1;
_nop_();_nop_();_nop_();_nop_();
}
sda = !ack;
scl = 1;
_nop_();_nop_();_nop_();_nop_();
scl = 0;
_nop_();_nop_();_nop_();_nop_();
sda = 1;
_nop_();_nop_();_nop_();_nop_();
return value;
}
//**********************************************************************
/*uchar soft_reset(void)
{
uchar error = 0;
connection_sht11();
error+= sensor_write(Reset);
return(error);
}
//***********************************************************************/
uchar read_status_reg()//uchar *P_v,uchar *P_c)
{
uchar j,l, error = 0;
start_sht11();
error = sensor_write(status_reg_r);
j = sensor_read(ACK);
l = sensor_read(NOACK);
return(error);
}
//*************************************************************************
uchar write_status_reg(uchar s )//*P_values)
{
uchar error = 0;
start_sht11();
error += sensor_write(status_reg_w);
error += sensor_write(s);//*P_values);
return(error);
}
//*************************************************************************
uchar sensor_measure(uchar mode,uchar *value,uchar *check)
{
uchar k,k1, error = 0;
uint i;
start_sht11();
switch(mode)
{
case TEMP: error += sensor_write(measure_TEMP);break;
case HUMI: error += sensor_write(measure_HUMI);break;
default
: break;
}
for(i=0;i<65535;i++)
if(sda == 0) break;
if(sda)
error = error+1;
k = sensor_read(ACK);
k1 = sensor_read(ACK);
*check = sensor_read(NOACK);
*value=k;
*(value+1)=k1;
return(error);
}
//*****************************************************************************
*******
/*void init_uart(void)
{
TH1=0xfd;
TL1=0xfd;
TMOD=0x25;
PCON=0x80;
SCON=0xf0;
ES=1;
EA=1;
TR1=1;
}
*/
//*****************************************************************************
*******
void ssio(void) interrupt 4 using 3
{
ES = 0;
REN = 0;
RI = 0;
a1 = SBUF;
REN=1;
ES = 1;
}
//*****************************************************************************
********
/*
//RH = c1+c2*SORH+c3*SORH*SORH
void RH(void)
{
//float RH;
const float C1 = 4;
const float C2 = 0.0408;
const float C3 = -0.0000028;
int SORH ;
SORH =(int)
*h_m;
SORH = SORH << 8;
SORH = SORH | ((int)*(h_m+1));
RH1 = C2 * SORH + C3 * SORH * SORH - C1;
//return(RH);
}
//*****************************************************************************
******
//T = d1+d2*SOT
void TEM(void)
{
int K;
const float d1 = 40;
const float d2 = 0.01;
//int TP3 ;
K =(int)*t_v;
K<<=8;
K = K | ((int)*(t_v+1));
TEMPE = d2 * K - d1;
//return(T2);
}
*/
//*****************************************************************************
********
void main(void)
{
//float tm,hi;
uchar error1;
char x, s=0;
//char *P;
TH1=0xf3;
TL1=0XF3;
TMOD=0x20;
PCON=0x00;
SCON=0x50;
ES=1;
EA=1;
{
write_status_reg(0x00);}
TR1=1;
if(read_status_reg()!=0x00)
while(1)
{ // connection_sht11();
switch(a1)
{
case 1 :
{
EA=0;
error1 = sensor_measure(TEMP,temp_value,&check_number);
x = *t_v;
SBUF = x;
while(TI==0);
TI = 0;
//1 byte
x = *(t_v+1);
SBUF = x;
while(TI==0);
TI = 0;
a1 = 0;
break;
}
EA=0;
error1 = sensor_measure(HUMI,humi_value,&check_number);
x = *h_m;
SBUF = x;
while(TI==0);
TI = 0;
//1 byte
x = *(h_m+1);
SBUF = x;
while(TI==0);
TI = 0;
a1 = 0;
break;
}
case 2 :
{
default:
break;
}
EA=1;
}
}