为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

sht11 传感器程序——打印

2018-01-12 19页 doc 44KB 15阅读

用户头像

is_594905

暂无简介

举报
sht11 传感器程序——打印sht11 传感器程序——打印 温湿度传感器SHT11说明 温湿度传感器SHT11程序 1、 单独测温度的程序, #include #include #include #define uchar unsigned char #define uint unsigned int sbit led1=P1^7; sbit led2=P1^6; sbit DATA=P1^1; sbit SCK=P1^0; #define noACK 0 #define ACK 1 unsigned char code t...
sht11 传感器程序——打印
sht11 传感器程序——打印 温湿度传感器SHT11说明 温湿度传感器SHT11程序 1、 单独测温度的程序, #include #include #include #define uchar unsigned char #define uint unsigned int sbit led1=P1^7; sbit led2=P1^6; sbit DATA=P1^1; sbit SCK=P1^0; #define noACK 0 #define ACK 1 unsigned char code tab[]={0x0c0,0x0f9,0x0a4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90,0xc6}; //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 sbit dula=P2^6; sbit wela=P2^7; uchar error=0; void delayms(uint z) { uint i,j; for(i=z;i>0;i--) for(j=110;j>0;j--); } /**************************74LS164的引脚定义********************/ sbit sdata=P0^0; sbit clk =P0^1; /**************************数码管的位码**************************/ sbit com1=P0^2; sbit com2=P0^3; sbit com3=P0^4; sbit com4=P0^5; sbit com5=P0^6; sbit com6=P0^7; /************************发送数据sendbyte************************/ void send(unsigned char number) { unsigned char num,i; num = tab[number]; clk=0; for(i=0;i<8;i++) { if(_crol_(num,i)&0x80) sdata=1; else sdata=0; clk=1; _nop_(); clk=0; } 1 温湿度传感器SHT11说明 } /************************显示数据************************/ void display1(unsigned char time1)//显示第一位 { send(time1); com1=0; delayms(2); com1=1; } void display2(unsigned char time2)//显示第二位 { send(time2); com2=0; delayms(2); com2=1; } void display3(unsigned char time2)//显示第三位 { send(time2); com3=0; delayms(2); com3=1; } void display4(unsigned char time1)//显示第四位 { send(time1); com4=0; delayms(2); com4=1; } //启动时序 void transstart() { SCK=1;_nop_(); DATA=0;_nop_(); SCK=0;_nop_();_nop_();_nop_(); SCK=1;_nop_(); DATA=1;_nop_(); SCK=0; } //写入命令子函数 uchar write_byte(uchar value) { uchar i; for(i=0x80;i>0;i=i/2) { if(i&value) DATA=1; else DATA=0; SCK=1; _nop_();_nop_();_nop_(); SCK=0; } DATA=1; SCK=1; error=DATA; SCK=0; return error; 2 温湿度传感器SHT11说明 } //从传感器读数据 uchar read_byte(bit ack) { uchar i,val=0; DATA=1; for(i=0x80;i>0;i=i/2) { SCK=1; if(DATA) val=val|i; SCK=0; } DATA=!ack; SCK=1; _nop_();_nop_();_nop_(); SCK=0; DATA=1; return val; } void connectionreset() { uchar i; DATA=1; SCK=0; for(i=0;i<9;i++) { SCK=1; SCK=0; } transstart(); } //测量函数,温度和湿度 uint measure() { uchar value_1,value_2; uint tempvalue; transstart(); error+=write_byte(MEASURE_HUMI); if(error!=0) connectionreset(); else { while(DATA==1) _nop_(); if(DATA==0) { value_1=read_byte(ACK); value_2=read_byte(noACK); tempvalue=value_2+value_1*256; //转换成16位的int型 } else error=1; } return tempvalue; } void display(float dis_value) { float value; 3 温湿度传感器SHT11说明 int qian,bai,shi,ge; value=dis_value*100; qian=(int)value/1000; bai=(int)value%1000/100; shi=(int)value%100/10; ge=(int)value%10; display4(ge); display3(shi); display2(bai); display1(qian); } //计算湿度值子函数 float calc_humi(float humivalue) { const float c1=-4.0,c2=0.0405,c3=-0.0000028,t1=0.01,t2=0.00008; float rh=humivalue; float rhlin; float rhtrue; uchar i; rhlin=c1+c2*rh+c3*rh*rh; rhtrue=(27.87-25)*(t1+t2*rh)+rhlin; if(rhtrue>100) rhtrue=100; if(rhtrue<0.1) rhtrue=0.1; humivalue=rhtrue; for(i=100;i>0;i--) display(humivalue); return humivalue; } void main() {// uchar i; float dis_humivalue; uint humivalue; while(1) {connectionreset(); error=0; humivalue=measure(); if(error==0) { led1=0; delayms(200); led1=1; delayms(200); dis_humivalue=calc_humi(humivalue); } else { continue; } } } 2、参考程序 #include //Microcontroller specific library, e.g. port definitions #include //Keil library (is used for _nop()_ operation) #include //Keil library 4 温湿度传感器SHT11说明 #include //Keil library typedef union { unsigned int i; float f; } value; enum {TEMP,HUMI}; #define DATA P1_1 #define SCK P1_0 #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 //---------------------------------------------------------------------------------- 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-BUS else DATA=0; SCK=1; //clk for SENSI-BUS _nop_();_nop_();_nop_(); //pulswith approx. 5 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) SCK=0; return error; //error=1 in case of no acknowledge } //---------------------------------------------------------------------------------- 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-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 SCK=0; } DATA=!ack; //in case of "ack==1" pull down DATA-Line SCK=1; //clk #9 for ack _nop_();_nop_();_nop_(); //pulswith approx. 5 us 5 温湿度传感器SHT11说明 SCK=0; DATA=1; //release DATA-line return val; } //---------------------------------------------------------------------------------- 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; } 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_softreset(void) // resets the sensor by a softreset { unsigned char error=0; s_connectionreset(); //reset communication error+=s_write_byte(RESET); //send RESET-command to sensor return 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 start 6 温湿度传感器SHT11说明 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);//send command 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 } 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 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 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 init_uart() //9600 bps @ 11.059 MHz {SCON = 0x52; TMOD = 0x20; TCON = 0x69; TH1 = 0xfd; } 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 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 [癈] t_C=t*0.01 - 40; //calc. temperature from ticks to [癈] 7 温湿度传感器SHT11说明 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<0.1)rh_true=0.1; //the physical possible range *p_temperature=t_C; //return temperature [癈] *p_humidity=rh_true; //return humidity[%RH] } 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 humidity error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature if(error!=0) s_connectionreset(); //in case of an error: connection reset else { humi_val.f=(float)humi_val.i; //converts integer to float temp_val.f=(float)temp_val.i; //converts integer to float calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\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!) //----------------------------------------------------------------------------------- } } 8
/
本文档为【sht11 传感器程序——打印】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索