该传感器低功耗,可以用在很多实际场合,只是价格上贵了一点。
温湿度传感器的SHT10代码如下:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~功能说明:SHT10与上位机(串口调试助手)的温湿度显示微处理器:STC89C52编译环境:Keil uVision V4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;*************************************************************/#include <reg52.h> //Microcontroller specific library, e.g. port definitions#include <intrins.h> //Keil library (is used for _nop()_ operation)#include <math.h> //Keil library#include <stdio.h> //Keil library/*************定义接口******************P2.6------SCK (SHT10)P2.7------DATA (SHT10)*****************************************/sbit SCK = P2^6; //定义通讯时钟端口sbit DATA = P2^7; //定义通讯数据端口typedef union{ unsigned int i; //定义了两个共用体float f;} value;enum {TEMP,HUMI}; //枚举测量温度或湿度#define noACK 0 //用于判断是否结束通讯#define ACK 1 //结束数据传输//adr command r/w#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/****************定义函数****************/void s_transstart(void); //启动传输函数void s_connectionreset(void); //连接复位函数char s_write_byte(unsigned char value);//SHT10写函数char s_read_byte(unsigned char ack); //SHT10读函数char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿度函数void calc_sht10(float *p_humidity ,float *p_temperature);//温湿度补偿float calc_dewpoint(float h,float t); //计算露点//char s_softreset(void);//char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum);//char s_write_statusreg(unsigned char *p_value);/*--------------------------------------;模块名称:s_write_byte();;功 能:SHT10写函数;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/char s_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) //shift bit for masking{ if (i & value) DATA=1; //masking value with i , write to SENSI-BUSelse DATA=0;SCK=1; //clk for SENSI-BUS_nop_();_nop_();_nop_(); //pulswith approx. 5 usSCK=0;}DATA=1; //release DATA-lineSCK=1; //clk #9 for ackerror=DATA; //check ack (DATA will be pulled down by SHT11)SCK=0;return error; //error=1 in case of no acknowledge}/*--------------------------------------;模块名称:s_read_byte();;功 能:SHT10读函数;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/char s_read_byte(unsigned char ack)// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"{unsigned char i,val=0;DATA=1; //release DATA-linefor (i=0x80;i>0;i/=2) //shift bit for masking{ SCK=1; //clk for SENSI-BUSif (DATA) val=(val | i); //read bitSCK=0;}DATA=!ack; //in case of "ack==1" pull down DATA-LineSCK=1; //clk #9 for ack_nop_();_nop_();_nop_(); //pulswith approx. 5 usSCK=0;DATA=1; //release DATA-linereturn val;}/*--------------------------------------;模块名称:s_transstart();;功 能:启动传输函数;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/void s_transstart(void)// generates a transmission start// _____ ________// DATA: |_______|// ___ ___// SCK : ___| |___| |______{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;}/*--------------------------------------;模块名称:s_connectionreset();;功 能:连接复位函数;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/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 statefor(i=0;i<9;i++) //9 SCK cycles{ SCK=1;SCK=0;}s_transstart(); //transmission start}/*//----------------------------------------------------------------------------------char s_softreset(void)//----------------------------------------------------------------------------------// resets the sensor by a softreset{unsigned char error=0;s_connectionreset(); //reset communicationerror+=s_write_byte(RESET); //send RESET-command to sensorreturn error; //error=1 in case of no response form the sensor}//----------------------------------------------------------------------------------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 starterror=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 starterror+=s_write_byte(STATUS_REG_W);//send command to sensorerror+=s_write_byte(*p_value); //send value of status registerreturn error; //error>=1 in case of no response form the sensor}*//*--------------------------------------;模块名称:s_measure();;功 能:测量温湿度函数;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/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;s_transstart(); //transmission startswitch(mode){ //send command to sensorcase 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 measurementif(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 checksumreturn error;}/*--------------------------------------;模块名称:init_uart();;功 能:串口初始化;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/void init_uart()//9600 bps @ 11.059 MHz{SCON = 0x52;TMOD = 0x20;TCON = 0x69;TH1 = 0xfd;}/*--------------------------------------;模块名称:calc_sht10();;功 能:温湿度补偿函数;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/void calc_sth11(float *p_humidity ,float *p_temperature)// calculates temperature [℃] and humidity [%RH]// input : humi [Ticks] (12 bit)// temp [Ticks] (14 bit)// output: humi [%RH]// temp [℃]{ const float C1=-4.0; // for 12 Bitconst float C2=+0.0405; // for 12 Bitconst float C3=-0.0000028; // for 12 Bitconst float T1=+0.01; // for 14 Bit @ 5Vconst float T2=+0.00008; // for 14 Bit @ 5Vfloat rh=*p_humidity; // rh: Humidity [Ticks] 12 Bitfloat t=*p_temperature; // t: Temperature [Ticks] 14 Bitfloat rh_lin; // rh_lin: Humidity linearfloat rh_true; // rh_true: Temperature compensated humidityfloat 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 ofif(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]}/*--------------------------------------;模块名称:calc_dewpoint();;功 能:露点,空气湿度达到饱和时的温度;占用资源:--;参数说明:--;创建日期:2012.04.10;版 本:FV1.0(函数版本Function Version);修改日期:--;修改说明:--;-------------------------------------*/float calc_dewpoint(float h,float t)// calculates dew point// input: humidity [%RH], temperature [℃]// output: dew point [℃]{ float logEx,dew_point;logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);return dew_point;}/*****************主函数*********************///----------------------------------------------------------------------------------void main()//----------------------------------------------------------------------------------// sample program that shows how to use SHT11 functions// 1. connection reset// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)// 3. calculate humidity [%RH] and temperature [℃]// 4. calculate dew point [℃]// 5. print temperature, humidity, dew point{ value humi_val,temp_val;float dew_point;unsigned char error,checksum;unsigned int i;init_uart(); //串口初始化s_connectionreset();while(1){ error=0;error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidityerror+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperatureif(error!=0) s_connectionreset(); //in case of an error: connection resetelse{ humi_val.f=(float)humi_val.i; //converts integer to floattemp_val.f=(float)temp_val.i; //converts integer to floatcalc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperaturedew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew pointprintf("Temp:%5.1f℃ Humi:%5.1f%% Dew point:%5.1f℃n",temp_val.f,humi_val.f,dew_point);}//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!)//-----------------------------------------------------------------------------------}}复制代码