• 方案介紹
  • 附件下載
  • 相關推薦
申請入駐 產(chǎn)業(yè)圖譜

基于51單片機的漏水檢測【溫濕度,浸水,上位機,LCD1602】(仿真)

01/27 08:43
1127
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論

有需要資料的可了解一下.docx

共1個文件

主要內(nèi)容:基于檔案館滲漏檢測需要,要求設計一套檔案庫房漏水檢測報警系統(tǒng)

1、包括水浸探測器,漏水信號采集模塊,聲光報警器,聲光報警控制模塊,漏水報警控制器,通訊模塊及控制主機。

2、實現(xiàn)智能化,自動化,集成化的檔案庫房漏水檢測報警系統(tǒng)。選擇合適的控制單元,實現(xiàn)檢測原理設計,繪制電路圖,實現(xiàn)軟件硬件結合。

3、測量溫濕度、漏水狀況,過限報警,并上傳到上位機。

4、上位機用labview。

#include <SHT11.h>
#include"intrins.h"
unsigned char sht_temp;			// 保存溫度
unsigned char sht_humi;			// 保存濕度

typedef union              		//定義共用同類型
{
	unsigned int i;
	float f;
}value;

char ShtWriteByte(unsigned char value)
{
	unsigned char i,error=0;
	for(i=128;i>0;i>>=1)  // 高位為1,循環(huán)右移
	{
		if (i&value)
			Data_P=1;       	// 和要發(fā)送的數(shù)相與,結果為發(fā)送的位
		else
			Data_P=0;
		Sck_P=1;
		_nop_();						// 延時3us
		_nop_();
		_nop_();
		Sck_P=0;
	}
	Data_P=1;    					// 釋放數(shù)據(jù)線
	Sck_P=1;
	error=Data_P;  				// 檢查應答信號,確認通訊正常
	_nop_();
	_nop_();
	_nop_();
	Sck_P=0;
	Data_P=1;
	return error; 				// error=1 通訊錯誤
}

char ShtReadByte(unsigned char ack)
{
	unsigned char i,val=0;
	Data_P=1; 						// 釋放數(shù)據(jù)線
	for(i=0x80;i>0;i>>=1)	// 高位為1,循環(huán)右移
	{
		Sck_P=1;
		if(Data_P)
			val=(val|i);    	// 讀一位數(shù)據(jù)線的值
		Sck_P=0;
	}
	Data_P=!ack;    			// 如果是校驗,讀取完后結束通訊
	Sck_P=1;
	_nop_();							// 延時3us
	_nop_();
	_nop_();
	Sck_P=0;
	_nop_();
	_nop_();
	_nop_();
	Data_P=1; 						// 釋放數(shù)據(jù)線
	return val;
}


void ShtTransStart(void)
{
	Data_P=1;
	Sck_P=0;
	_nop_();
	Sck_P=1;
	_nop_();
	Data_P=0;
	_nop_();
	Sck_P=0;
	_nop_();
	_nop_();
	_nop_();
	Sck_P=1;
	_nop_();
	Data_P=1;
	_nop_();
	Sck_P=0;
}

void ShtConnectReset(void)
{
	unsigned char i;
	Data_P=1; 		   		//準備
	Sck_P=0;
	for(i=0;i<9;i++)  	//DATA保持高,SCK時鐘觸發(fā)9次,發(fā)送啟動傳輸,通迅即復位
	{
		Sck_P=1;
		Sck_P=0;
	}
	ShtTransStart();   	//啟動傳輸
}

char ShtMeasure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
	unsigned error=0;
	unsigned int i;
	ShtTransStart();  		// 啟動傳輸
	switch(mode)       		// 選擇發(fā)送命令
	{
		case 1 :   					// 測量溫度
			error+=ShtWriteByte(0x03);
			break;
		case 2 :   					// 測量濕度
			error+=ShtWriteByte(0x05);
			break;
		default:
			break;
	}
	for(i=0;i<65535;i++)
		if(Data_P==0)
			break;  					// 等待測量結束
		if(Data_P)
			error+=1;   			// 如果長時間數(shù)據(jù)線沒有拉低,說明測量錯誤
	*(p_value) =ShtReadByte(1);  		// 讀第一個字節(jié),高字節(jié) (MSB)
	*(p_value+1)=ShtReadByte(1); 		// 讀第二個字節(jié),低字節(jié) (LSB)
	*p_checksum =ShtReadByte(0);  	// read CRC校驗碼
	return error;  									// error=1 通訊錯誤
}

void CalcSHT11(float *p_humidity ,float *p_temperature)
{
	const float C1=-4.0;	 			// 12位濕度精度 修正公式
	const float C2=+0.0405;			// 12位濕度精度 修正公式
	const float C3=-0.0000028;	// 12位濕度精度 修正公式
	const float T1=+0.01;	 			// 14位溫度精度 5V條件 修正公式
	const float T2=+0.00008;	 	// 14位溫度精度 5V條件 修正公式
	float rh=*p_humidity;	 			// rh: 12位 濕度
	float t=*p_temperature;			// t:  14位 溫度
	float rh_lin;								// rh_lin: 濕度 linear值
	float rh_true;							// rh_true: 濕度 ture值
	float t_C;	 								// 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;		//相對濕度對于溫度依賴性補償
	*p_temperature=t_C;	 				//返回溫度結果
	*p_humidity=rh_true;	 			//返回濕度結果
}

unsigned char TempCorrect(int temp)
{
	if(temp<0)	temp=0;
	if(temp>970)  temp=970;
	if(temp>235)  temp=temp+10;
	if(temp>555)  temp=temp+10;
	if(temp>875)  temp=temp+10;
	temp=(temp%1000)/10;
	return temp;
}

unsigned char HumiCorrect(unsigned int humi)
{
	if(humi>999)  humi=999;
	if((humi>490)&&(humi<951))  humi=humi-10;
	humi=(humi%1000)/10;
	return humi+4;
}

void ReadShtData()
{
	value humi_val,temp_val;  	// 定義兩個共同體,一個用于濕度,一個用于溫度
	unsigned char error;  							// 用于檢驗是否出現(xiàn)錯誤
	unsigned char checksum;  						// CRC
	unsigned int temp1,humi1;						// 臨時讀取到的溫濕度數(shù)據(jù)

	error=0; 										//初始化error=0,即沒有錯誤
	error+=ShtMeasure((unsigned char*)&temp_val.i,&checksum,1); 	//溫度測量
	error+=ShtMeasure((unsigned char*)&humi_val.i,&checksum,2); 	//濕度測量

	if(error!=0) 		  					//如果發(fā)生錯誤,系統(tǒng)復位
		ShtConnectReset();
	else
	{
		humi_val.f=(float)humi_val.i; 				//轉(zhuǎn)換為浮點數(shù)
		temp_val.f=(float)temp_val.i;  				//轉(zhuǎn)換為浮點數(shù)
		CalcSHT11(&humi_val.f,&temp_val.f);  	//修正相對濕度及溫度
		temp1=temp_val.f*10;
		sht_temp=TempCorrect(temp1);
		humi1=humi_val.f*10-50;
		sht_humi=HumiCorrect(humi1);
		humi1=humi1-1;
	}

}

資料借鑒于此紛傳

  • 有需要資料的可了解一下.docx
    下載

相關推薦