水质监测系统

#include "REG52.h"
#include "oled.h"
#include "Delay.h"
#include "OneWire.h"
#include "DS18B20.h"
#include "Key.h"
#include "Timer0.h"
#include "Buzzer.h"
#include "PCF8591.h"
#include "math.h"
#include "wifi.h"
#include "stdio.h"
#include "string.h"


typedef unsigned char uchar;
typedef unsigned char uint;
unsigned char KeyNum,Mode=0,yuzhiSetSelect,yuzhiSetFlashFlag;
char yuzhi[]={44,-10,13,2,89};//TH,TL,PHH.PHL,TDSH

uchar buf[]={'A','+',2,3,4,5,0,5,3,6,3, 4, 5, '+',5, 6, '-',1, 0, 1, 4,    0, 0,    9, 9};
/*            0,  1, 2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16,17,18, 19,20,   21,22,   23,24
              Í·  T          PH      TDS       yuzhi[0] yuzhi[1]  yuzhi[2] yuzhi[3] yuzhi[4]
*/


uint TDS=0;
float cv;    //compensationVolatge£¬Î¶Ȳ¹³¥ÏµÊý
float PH=0.0,T=0.0;
double ADC_Voltage=0.0;

	
/*PH¶ÁÈ¡ºÍÏÔʾº¯Êý*/
void PH_Value_Conversion(){	
  uchar i=2;     
	EA=0; 
	//pcf8591ÿ¶ÁÒ»´ÎÆô¶¯Ò»´Îת»»£¬×ª»»µÄ½á¹ûÔÚÏ´βÅÄܶÁ³ö£¬ËùÒÔÁ¬ÐÁ½´Îø¶ÁÈ¡Ò»¸öͨµÀµÄÊý¾Ý
	while(i--){  
			ISendByte(PCF8591,0x40);  //»ñÈ¡PCF8591µÄת»»µçѹֵ
			ADC_Voltage=IRcvByte(PCF8591)*5.0/255;  //ת»»Îªµçѹֵ
  }
  EA=1;  
	
	PH=-5.7541*ADC_Voltage+16.654;   //¹«Ê½×ª»»
	if(PH>=14.00){PH=14.00;}
			
	buf[6]=(int)(PH*100)/1000+'0';
  buf[7]=(int)(PH*100)%1000/100+'0';	
	buf[8]=(int)(PH*100)%100/10+'0';
	buf[9]=(int)(PH*100)%10+'0';
	
	/*ÏÔʾ PH Ũ¶ÈÖµ*/
	OLED_ShowChar(24,2,buf[6],16);
	OLED_ShowChar(33,2,buf[7],16);
	OLED_ShowChar(42,2,'.',16);
	OLED_ShowChar(51,2,buf[8],16);
	OLED_ShowChar(60,2,buf[9],16);
}


/*TDS¶ÁÈ¡ºÍÏÔʾº¯Êýº¯Êý*/
void TDS_Value_Conversion(){
	uchar i=2;
	EA=0;
  while(i--){
		ISendByte(PCF8591,0x41);  //»ñÈ¡PCF8591µÄת»»µçѹֵ
		ADC_Voltage=IRcvByte(PCF8591)*5.0/255;  //ת»»Îªµçѹֵ
	}
	EA=1;
		
	cv=ADC_Voltage/(1.0+0.02*(T-25.0));  //¹«Ê½×ª»»		
	TDS=(int)(13342*cv*cv*cv-25586*cv*cv+85739*cv)/200;  //¹«Ê½×ª»»		
	 
	buf[10]=(int)(TDS)/100+'0';
	buf[11]=(int)(TDS)%100/10+'0';
	buf[12]=(int)(TDS)%10+'0';	
	
	OLED_ShowNum(32,6,TDS,3,16);  //ÏÔʾTDSÖµ
	
}


/*ζȶÁÈ¡ºÍÏÔʾº¯Êý*/
void T_Conversion(){
	EA=0;
	DS18B20_ConvertT();	
	T=DS18B20_ReadT();	//´«¸ÐÆ÷¶ÁÈ¡µ±Ç°Î¶È
	EA=1;
		
	if(T<0) buf[1]='-';
	else buf[1]='+';
	buf[2]=(int)(T*100)/1000+'0';  //ζÈÕûÊý²¿·Ö
	buf[3]=(int)(T*100)%1000/100+'0';	
	buf[4]=(int)(T*100)%100/10+'0'; //ζÈСÊý²¿·Ö
	buf[5]=(int)(T*100)%10+'0';
				
	//ÏÔʾζÈT
	OLED_ShowChar(16,4,buf[1],16);
	OLED_ShowChar(24,4,buf[2],16);
	OLED_ShowChar(32,4,buf[3],16);	
	OLED_ShowChar(40,4,'.',16);	
	OLED_ShowChar(48,4,buf[4],16);	
	OLED_ShowChar(56,4,buf[5],16);			
}


//ÏÔʾ¸÷ãÐÖµ
void yuzhi_Show(){
	if(yuzhi[0]<0){buf[13]='-';}
	else {buf[13]='+';}
	if(yuzhi[1]<0) {buf[16]='-';}
	else{buf[16]='+';}

	OLED_ShowChar(94,4,buf[13],12);
  OLED_ShowChar(94,5,buf[16],12);
	OLED_ShowNum(102,4,abs(yuzhi[0]),2,12);
	OLED_ShowNum(102,5,abs(yuzhi[1]),2,12);
	OLED_ShowNum(72,2,yuzhi[3],2,12);
	OLED_ShowNum(96,2,yuzhi[2],2,12);
	OLED_ShowNum(96,7,yuzhi[4]*10,3,12);
}


//°´¼üµ÷½ÚãÐÖµ¹¦ÄÜ
void yuzhi_Set(void){
	OLED_ShowNum(120,3,yuzhiSetSelect,1,12); //ÏÔʾãÐֵѡÔñºÅ
	if(KeyNum==2){         //°´¼ü2°´ÏÂ
		yuzhiSetSelect++;    //ÉèÖÃÑ¡Ôñλ¼Ó1
		OLED_ShowNum(120,2,yuzhiSetSelect,1,12);
		yuzhiSetSelect%=5;    //Ô½½çÇåÁã
	}
	if(KeyNum==3){  //ãÐÖµ¼Ó
		yuzhi[yuzhiSetSelect]++;
		if(yuzhi[0]>=99){yuzhi[0]=99;}
		if(yuzhi[1]>yuzhi[0]){yuzhi[1]--;}
		if(yuzhi[2]>=14){yuzhi[2]=14;}
		if(yuzhi[3]>yuzhi[2]){yuzhi[3]--;}
		if(yuzhi[4]>=99){yuzhi[4]=99;}
	}
	if(KeyNum==4){  //ãÐÖµ¼õ
		yuzhi[yuzhiSetSelect]--;
		if(yuzhi[0]<yuzhi[1]){yuzhi[0]++;}
		if(yuzhi[1]<=-55){yuzhi[1]=-55;}
		if(yuzhi[2]<yuzhi[3]){yuzhi[2]++;}
		if(yuzhi[3]<=0){yuzhi[3]=0;}
		if(yuzhi[4]<=0){yuzhi[4]=0;}
	}
  	
	//ãÐÖµ¸üÐÂÏÔʾ(ʵÏÖÐÞ¸ÄʱÉÁ˸¹¦ÄÜ)
	if(yuzhiSetSelect==0&&yuzhiSetFlashFlag==1){OLED_ShowString(102,4, "  ",12);}
	if(yuzhiSetSelect==1&&yuzhiSetFlashFlag==1){OLED_ShowString(102,5, "  ",12);}
  if(yuzhiSetSelect==3&&yuzhiSetFlashFlag==1){OLED_ShowString(72,2, "  ",12);}
	if(yuzhiSetSelect==2&&yuzhiSetFlashFlag==1){OLED_ShowString(96,2, "  ",12);}
	if(yuzhiSetSelect==4&&yuzhiSetFlashFlag==1){OLED_ShowString(96,7, "   ",12);}
	else{yuzhi_Show();}
}


//½«Ã¿¸öãÐÖµ²ðµ½Ò»¸öÊý×éÖбãÓÚ´«ËÍÊý¾Ý
void chai(){
	if(yuzhi[0]>=0) buf[13]=(int)'+';  //TH
	else buf[13]=(int)'-';
	buf[14]=(int)abs(yuzhi[0])/10+'0';
	buf[15]=(int)abs(yuzhi[0])%10+'0';
	
	if(yuzhi[1]>=0) buf[16]=(int)'+';  //TL
	else buf[16]=(int)'-';
	buf[17]=(int)abs(yuzhi[1])/10+'0';
	buf[18]=(int)abs(yuzhi[1])%10+'0';
	
	buf[19]=(int)yuzhi[2]/10+'0';  //PHH
	buf[20]=(int)yuzhi[2]%10+'0'; 
	
	buf[21]=(int)yuzhi[3]/10+'0';   //PHL
	buf[22]=(int)yuzhi[3]%10+'0';
	
	buf[23]=(int)yuzhi[4]/10+'0';  //TDSH
	buf[24]=(int)yuzhi[4]%10+'0';
}


//ãÐÖµÔ½½çÅжÏ
void ifover(){
	if(T>yuzhi[0]||T<yuzhi[1]||PH>yuzhi[2]||PH<yuzhi[3]||TDS>(yuzhi[4]*10)){
		OLED_ShowCHinese(110,0,9);//±¨¾¯Í¼±ê
		Buzzer_Time(100);   //·äÃùÆ÷±¨¾¯
	}
}


//Ö÷º¯Êý
int main(void){		  
	 buf[0]='A';
	 OLED_Init();		//OLED³õʼ»¯	
	 OLED_Clear();
	 
   //ϵͳ»ù±¾½çÃæ
	 OLED_ShowCHinese(0,0,0);//Ë®
	 OLED_ShowCHinese(18,0,1);//ÖÊ
	 OLED_ShowCHinese(36,0,2);//¼à
	 OLED_ShowCHinese(54,0,3);//²â
	 OLED_ShowCHinese(72,0,4);//ϵ
	 OLED_ShowCHinese(90,0,5);//ͳ
	 OLED_ShowCHinese(68,4,8);//¡ãC
	 OLED_ShowString(0,4,"T:",16);//ζÈT£º
	 OLED_ShowString(0,2,"PH:",16);//PH£º
	 OLED_ShowString(0,6,"TDS:",16);//TDS£º 
	 OLED_ShowChar(88,2,'-',12);
	 OLED_ShowChar(88,7,'<',12);
	 OLED_ShowString(59,7,"PPM",12);
	 chai();  
	 yuzhi_Show();   //ÏÔʾãÐÖµ
	 Timer0_Init();  //¶¨Ê±Æ÷³õʼ»¯
	 SysInit();  //ϵͳ³õʼ»¯£¬°üÀ¨´®¿Ú³õʼ»¯ÒÔ¼°WiFi³õʼ»¯
 
  while(1) 
	{		
		KeyNum=Key();//¶ÁÈ¡°´¼ü¼üÂë
		
		T_Conversion();  //»ñȡζÈ
		PH_Value_Conversion();  //»ñÈ¡PH
		TDS_Value_Conversion();  //»ñÈ¡TDS
		
		if(KeyNum==1){  //µ±°´¼ü1°´ÏÂ
			if(Mode==0){Mode=1;yuzhiSetSelect=0;}//°´¼üµ÷½Ú¹¦ÄÜÇл»
			else if(Mode==1){Mode=0;}
			OLED_ShowNum(120,2,Mode,1,12);  //0£ºÏÔʾģʽ£¬1£ºãÐÖµÐÞ¸Äģʽ
		 }
			
		switch(Mode){//¸ù¾Ý²»Í¬µÄģʽ¹¦ÄÜÖ´Ðв»Í¬µÄº¯Êý
			case 0:yuzhi_Show();break;
			case 1:yuzhi_Set();break;
		}
		 		
	  ifover();//ÅжÏÊÇ·ñ³¬³öãÐÖµ
			
	  //·¢ËÍÊý¾Ýµ½´®¿Ú		
		sendString("AT+CIPSEND=0,25\r\n");
		Delay(100);
		sendString(buf);  //·¢ËÍTCPÊý¾Ý°ü
		Delay(100);
		sendString("\r\n");
		
		}  //while
}  //main



//¶¨Ê±Æ÷ÖжϺ¯Êý
void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TL0 = 0x18;		//ÉèÖö¨Ê±³õÖµ
	TH0 = 0xFC;		//ÉèÖö¨Ê±³õÖµ
	T0Count++;
	if(T0Count>=20)
	{
		T0Count=0;
		yuzhiSetFlashFlag=!yuzhiSetFlashFlag;  //ÉÁ˸±ê־λȡ·´
	 	Key_Loop();	 //ÿ20msµ÷ÓÃÒ»´Î°´¼üÇý¶¯º¯Êý
	}
}



你可能感兴趣的:(嵌入式,c++,开发语言,51单片机,物联网)