|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
基于51单片机的农田自动灌溉系统的设计SHT10传感器的proteus仿真原理图如下:7 g' E F: L; N5 k2 y* h
5 o$ N% w) }3 Y }) \: y) c' z W
# q: y6 N+ E4 e2 h单片机源程序如下:
. l# a, v7 a% S2 N/ T% o# ]. q- #include"SHT10.h"
- #define uchar unsigned char
- /*--------------------------------------
- ;启动传输函数
- ;-------------------------------------*/
- void s_transstart(void)
- {
- DATA=1; SCK=0; //Initial state
- _nop_();
- SCK=1;
- _nop_();
- DATA=0;
- _nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();
- DATA=1;
- _nop_();
- SCK=0;
- }
- /*--------------------------------------
- ;连接复位函数
- ;-------------------------------------*/
- void s_connectionreset(void)
- {
- unsigned char i;
- DATA=1; SCK=0;
- for(i=0;i<9;i++)
- {
- SCK=1;
- SCK=0;
- }
- s_transstart();
- }
- /*--------------------------------------
- ;SHT10写函数
- ;-------------------------------------*/
- char s_write_byte(unsigned char value)
- {
- unsigned char i,error=0;
- for (i=0x80;i>0;i/=2) //shift bit for masking
- {
- if (i & value) DATA=1; //masking value with i , write to SENSI-BU
- 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 SHT11),DATA在第9个上升沿将被SHT10自动下拉为低电
- _nop_();_nop_();_nop_();
- SCK=0;
- DATA=1; //release DATA-line
- return error; //error=1 in case of no acknowledge //返回:0成功,1失败
- }
- /*--------------------------------------
- ;SHT10读函数
- ;-------------------------------------*/
- char s_read_byte(unsigned char ack)
- {
- unsigned char i,val=0;
- DATA=1;
- for (i=0x80;i>0;i/=2)
- { SCK=1;
- if (DATA) val=(val | i);
- _nop_();_nop_();_nop_();
- SCK=0;
- }
- if(ack==1)DATA=0;
- else DATA=1; //如果是校验(ack==0),读取完后结束通讯
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();_nop_();_nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- DATA=1;
- return val;
- }
- /*--------------------------------------
- ;测量温湿度函数
- ;-------------------------------------*/
- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
- {
- unsigned error=0;
- unsigned int i;
- s_transstart();
- switch(mode)
- {
- 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;
- if(DATA) error+=1;
- *(p_value) =s_read_byte(ACK);
- *(p_value+1)=s_read_byte(ACK);
- *p_checksum =s_read_byte(noACK);
- return error;
- }
- /*--------------------------------------
- ;温湿度补偿函数
- ;-------------------------------------*/
- void calc_SHT10(float *p_humidity ,float *p_temperature)
- {
- const float C1=-4.0;
- const float C2=+0.0405;
- const float C3=-0.0000028;
- const float T1=+0.01;
- const float T2=+0.00008;
- float rh=*p_humidity;
- float t=*p_temperature;
- float rh_lin;
- float rh_true;
- float t_C;
- t_C=t*0.01 - 40;
- rh_lin=C3*rh*rh + C2*rh + C1;
- rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;
- if(rh_true>100)rh_true=100;
- if(rh_true<0.1)rh_true=0.1;
- *p_temperature=t_C;
- *p_humidity=rh_true+C1;
- }
- //读状态寄存器
- char s_read_statusreg(unsigned char*p_value, unsigned char *p_checksum)
- //----------------------------------------------------------------------------------
- // reads the status register with checksum(8-bit)
- {
- unsigned char error=0;
- s_transstart(); //transmission start
- error=s_write_byte(STATUS_REG_R);//send command to sensor
- *p_value=s_read_byte(ACK); //read status register (8-bit)
- *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
- return error; //error=1 in case of no response form the sensor
- }
- //写状态寄存器
- char s_write_statusreg(unsigned char*p_value)
- // writes the status register with checksum(8-bit)
- {
- unsigned char error=0;
- s_transstart(); //transmission start
- error+=s_write_byte(STATUS_REG_W);//sendcommand 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
- }
- ……………………5 R- m, O: N# c
$ g& x: K$ g3 v# I* U) @& w |
|