当前位置:首页 > 毕业设计(论文)基于at89c51单片机温湿度显示报警系统设计
附录
}
void s_connectionreset(void) //连接复位函数
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart // _____________________________________________________ ________
// DATA: |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ {
unsigned char i;
DATA=1; SCK=0; //Initial state for(i=0;i<9;i++) //9 SCK cycles { SCK=1; SCK=0; }
s_transstart(); //transmission start }
char s_write_byte(unsigned char value) // SHT10写字节函数 //---------------------------------------------------------------------------------- // writes a byte on the Sensibus and checks the acknowledge {
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking {
第37页(共43页)
基于单片机温湿度显示报警系统设计
if (i & value) DATA=1; //masking value with i , write to SENSI-BUS else DATA=0;
SCK=1; //clk for SENSI-BUS _nop_();_nop_();_nop_(); //pulswith approx. 3 us SCK=0; }
DATA=1; //release DATA-line SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by DHT90),DATA在第9个上升沿将被DHT90自动下拉为低电平。 _nop_();_nop_();_nop_(); SCK=0;
DATA=1; //release DATA-line
return error; //error=1 in case of no acknowledge //返回:0成功,1失败 }
/*****SHT10读函数 reads a byte form the Sensibus and gives an acknowledge in case of \ char s_read_byte(unsigned char ack) {
unsigned char i,val=0;
DATA=1; //release DATA-line for (i=0x80;i>0;i/=2) //shift bit for masking { SCK=1; //clk for SENSI-BUS if (DATA) val=(val | i); //read bit
_nop_();_nop_();_nop_(); //pulswith approx. 3 us
SCK=0;
第38页(共43页)
附录
}
if(ack==1)DATA=0; //in case of \ else DATA=1; //如果是校验(ack==0),读取完后结束通讯 _nop_();_nop_();_nop_(); //pulswith approx. 3 us SCK=1; //clk #9 for ack _nop_();_nop_();_nop_(); //pulswith approx. 3 us SCK=0;
_nop_();_nop_();_nop_(); //pulswith approx. 3 us DATA=1; //release DATA-line return val; }
/* 测量温湿度函数 makes a measurement (humidity/temperature) with checksum */
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) {
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
第39页(共43页)
基于单片机温湿度显示报警系统设计
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 return error; }
void calc_sht90(float *p_humidity ,float *p_temperature) //温湿度补偿函数 // calculates temperature [C] and humidity [%RH] // input : humi [Ticks] (12 bit) // temp [Ticks] (14 bit) // output: humi [%RH] // temp [C]
{ 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 [C]
t_C=t*0.01 - 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
第40页(共43页)
共分享92篇相关文档