標(biāo)題:
SHT11.h下載 溫濕度傳感器SHT11的驅(qū)動程序,提供了外界調(diào)用接口函數(shù)
[打印本頁]
作者:
51hei學(xué)習(xí)技術(shù)中
時間:
2016-10-11 19:29
標(biāo)題:
SHT11.h下載 溫濕度傳感器SHT11的驅(qū)動程序,提供了外界調(diào)用接口函數(shù)
0.png
(58.59 KB, 下載次數(shù): 79)
下載附件
2016-10-11 19:28 上傳
所有文件下載:
溫濕度傳感器SHT11的驅(qū)動程序.rar
(44.81 KB, 下載次數(shù): 50)
2016-10-11 19:29 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
下面是部分程序的預(yù)覽:
/*******************************************************************
*【文 件 名】:SHT11.h *
*【建立日期】:7月5日 *
*【修改日期】:7月5日 *
*【文件作用】:溫濕度傳感器SHT11的驅(qū)動程序,提供了外界調(diào)用接口函數(shù) *
*【說 明】: *
*------------------------------------------------------------------*
*【作 者】:郭鑫(ben) *
*【版 權(quán)】:國家創(chuàng)新性實驗項目,編號GCS07001 *
*******************************************************************/
#ifndef _SHT11_08_07_5_
#define _SHT11 _08_07_5_
/***************************頭文件部分*****************************/
#include <reg52.h>
#define ACK 1
#define noACK 0
#define measure_temp 0x03 //測量溫度命令
#define measure_humi 0x05 //測量濕度命令
#define RESET 0x1e //軟啟動
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//-------------------------管腳定義--------------------------------
sbit DATA=P1^0;
sbit SCK=P1^1;
//------------------------數(shù)據(jù)結(jié)構(gòu)體定義---------------------------
typedef union //保存所測得的溫度&濕度值
{ uint i;
float f;
} value;
typedef struct __WENSHIDU__
{
uchar gewei;
uchar shiwei;
uchar DateString1[5];
uchar DateString2[6];
}WENSHIDU;
/***************************函數(shù)聲明*******************************/
char write_byte(unsigned char value); // write a byte and checks the ack signal
char read_byte(unsigned char ack); //read a byte and checks the ack signal
void transstart(void);
void connectionreset(void);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
void calc_sth11(float *p_humidity ,float *p_temperature);
void DateToStr(WENSHIDU *Time,float datax,float datax1);
void call_sht11(void);
void sht11_window(void); //溫濕度界面顯示
//--------- write a byte and checks the ack signal-----------------
char write_byte(uchar value)
{
uchar i,error=0;
for (i=0x80;i>0;i/=2) /*連續(xù)向右移動8位*/
{
if (i & value) DATA=1; /*把相應(yīng)的位送數(shù)據(jù)線*/
else DATA=0;
SCK=1; /*時序脈沖,應(yīng)嚴(yán)格按著此標(biāo)準(zhǔn)*/
_nop_();
_nop_();
_nop_(); /*大約0.5us*/
SCK=0;
}
DATA=1; /*釋放數(shù)據(jù)線*/
SCK=1; /*第9位作為響應(yīng)位*/
error=DATA; /*檢測響應(yīng)情況,如果有響應(yīng)數(shù)據(jù)線就會被SHT10拉低*/
SCK=0;
return error; /*返回1表示沒響應(yīng)*/
}
//--------- read a byte and checks the ack signal-----------------
char read_byte(uchar ack)
{
uchar i,val=0;
DATA=1; /*釋放數(shù)據(jù)線*/
for (i=0x80;i>0;i/=2) /*連續(xù)向右移動8位*/
{
SCK=1; /*clk for SENSI-BUS*/
if (DATA) val=(val | i); /*read bit */
SCK=0;
}
DATA=!ack; /*當(dāng)ack=1時拉低數(shù)據(jù)線*/
SCK=1; /*clk #9 for ack*/
_nop_();_nop_();_nop_(); /*pulswith approx. 5 us */
SCK=0;
DATA=1; /*釋放數(shù)據(jù)線*/
return val;
}
/*******************************************************************
*【函 數(shù) 名】:transstart *
*【修改日期】:2008年7月5日 *
*【函數(shù)作用】:傳輸控制,發(fā)出傳輸開始命令 *
*------------------------------------------------------------------*
*【備 注】: *
*******************************************************************/
void transstart(void)
{
DATA=1;
SCK=0; //初始狀態(tài)
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
/*******************************************************************
*【函 數(shù) 名】:connectionreset *
*【修改日期】:2008年7月5日 *
*【函數(shù)作用】:復(fù)位:當(dāng)DATA線處于高低平時,觸發(fā)SCK9次以上(含9次) *
* 此后應(yīng)接發(fā)一個"傳輸開始"命令 *
*------------------------------------------------------------------*
*【備 注】: *
*******************************************************************/
void connectionreset(void)
{
unsigned char i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SCK=1;
SCK=0;
}
transstart(); //transmission start
}
//-------makes a measurement (humidity/temperature) with checksum-----
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
uint i;
transstart(); //transmission start
switch(mode){ //send command to sensor*/
case 0 : error+=write_byte(measure_temp); break;
case 1 : error+=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) =read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=read_byte(ACK); //read the second byte (LSB)
*p_checksum =read_byte(noACK); //read checksum
return error;
}
//---------------溫濕度值標(biāo)度變換及溫度補(bǔ)償-------
void calc_sth11(float *p_humidity ,float *p_temperature)
{
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 [癈]
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]
}
/*******************************************************************
*【函 數(shù) 名】:DateToStr *
*【修改日期】:2008年7月5日 *
*【函數(shù)作用】:數(shù)值轉(zhuǎn)換,供顯示使用 *
*------------------------------------------------------------------*
*【備 注】: *
*******************************************************************/
void DateToStr(WENSHIDU *Time,float datax,float datax1)
{
uint i;
i=(int)datax;
Time->shiwei=i/10;
Time->gewei =i%10;
Time->DateString1[0] = Time->shiwei + '0';
Time->DateString1[1] = Time->gewei + '0';
i=(int)((datax-i)*10);
Time->DateString1[2] ='.';
Time->DateString1[3] = i + '0';
Time->DateString1[4] = '\0';
i=(int)datax1;
Time->shiwei=i/10;
Time->gewei =i%10;
Time->DateString2[0] = Time->shiwei + '0';
Time->DateString2[1] = Time->gewei + '0';
i=(int)((datax1-i)*10);
Time->DateString2[2] ='.';
Time->DateString2[3] = i + '0';
Time->DateString2[4] = '%';
Time->DateString2[5] = '\0';
}
/*******************************************************************
*【函 數(shù) 名】:call_sht11 *
*【修改日期】:2008年7月5日 *
*【函數(shù)作用】:sht11驅(qū)動調(diào)用,調(diào)用即可完成一次測量 *
*------------------------------------------------------------------*
*【備 注】: *
*******************************************************************/
void call_sht11(void)
{
WENSHIDU S;
value humi_val,temp_val;
unsigned char error,checksum;
unsigned int i;
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,1); /*measure humidity*/
error+=s_measure((unsigned char*) &temp_val.i,&checksum,0); /*measure temperature*/
if(error!=0)
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*/
DateToStr(&S,temp_val.f,humi_val.f);
lcd_setposition(2,5);
lcd_str_w(S.DateString1);
lcd_setposition(3,5);
lcd_str_w(S.DateString2);
}
//----------wait approx. 0.8s to avoid heating up SHT10------------------------------ */
for (i=0;i<10000;i++); //(be sure that the compiler doesn't eliminate this line!)
//----------------------------------------------------------------------------------- */
}
//--------------------溫濕度界面顯示------------------------
void sht11_window(void)
{
lcd_cmd_w(0x01);
lcd_setposition(1,1);
lcd_str_w("溫濕測量系統(tǒng)");
lcd_setposition(2,0);
lcd_str_w("當(dāng)前溫度:");
lcd_setposition(3,0);
lcd_str_w("當(dāng)前濕度:");
}
#endif
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1