⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 water.c

📁 详细介绍了怎么利用单片机实现自身温度控制的程序!通过了实验验证!效果不错!
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "water.h"

uchar xdata lcdPosX, lcdPosY;						//X,Y坐标
uchar xdata halfScr, halfLineCnt, basicBlock;		//半屏,半屏行数,N*8块

uchar xdata Option = 0;	//液晶屏选项
uchar xdata Page = 0;	//液晶屏页码

uchar  ErrorNum = 0;         //0-无错误
uchar xdata Errorflag[9];

/**********************************delay**************************************************/
void delay(uint i)
{
	while (i--) ;		
}
void Ldelay()
{
	int i;
	for(i=0;i<10;i++)
	{
		delay(50000);
		delay(50000);
		delay(50000);
		delay(50000);
		delay(50000);
	}
}
/********************************** HD7279A *********************************************/
/* Long delay for 7279 read or write */
void long_delay(void)
{
	uchar  i;
	for( i = 0; i < 0xff; i++)
	{
		 nop();
		 nop();
	}
}

/* Short delay for 7279 read or write */
void short_delay(void)
{
	uchar i;
	for( i = 0; i <0x60; i++);
}

/*Write 7279 function */
void send_byte( uchar out_byte)
{
	 uchar i;
	 CS = 0;
	 long_delay();
	 for(i = 0; i < 8; i++)
	 {	
	 	if(out_byte & 0x80)
		{
			DAT = 1;
		}
		else
		{
			DAT = 0;
		}
		CLK = 1;
		short_delay();
		CLK = 0;
		short_delay();
		out_byte = out_byte*2;
	}
	DAT = 0; 
}//end of send_byte function

uchar receive_byte(void)
{
	uchar i, in_byte;
	DAT = 1;
	long_delay();
	for( i = 0; i < 8; i++)
	{
		CLK = 1;
		short_delay();
		in_byte = in_byte*2;
		if(DAT)
		{
			in_byte  = in_byte | 0x01;
		}
		CLK  = 0;
	    short_delay();
	}
	DAT = 0;
	return (in_byte);
}

/* interface of write 7279 function*/
void write7279(uchar cmd, uchar dta)
{
	send_byte(cmd);    //first send command byte
	send_byte(dta);    //second send user data
}

/* interface of read 7279 function*/
uchar read7279(uchar cmd)
{
	send_byte(cmd);   //first send command to read data
	return (receive_byte());     //read data
}


/********************************************* 液晶 ********************************************/

//ms延时  
void lcd_DelayMs (uint ms)			
{
	uchar i;
	while (ms)
	{
		for (i = 0; i < 254; i++);
		ms--;
	}
}



//串行数据输入一字节
void LCD_SendByte(uchar s_data)
{
	uchar i;
	SID=0;
	SCLK=0;
	for(i=0;i<8;i++)
	{
		SID=(bit)(s_data & 0x80);
		delay(1);                            
		SCLK=1;
		delay(1);                            
		s_data<<=1;
		delay(1);                            
		SCLK=0;
		delay(1);                            
	}
}
//命令输入
void LCD_SendComm(uchar comm_data)
{
	delay(1);                            
	LCD_SendByte(0xF8 & 0xFF);                //send 1 1 1 1 1 RW=0 RS=0 0
	delay(1);                            
	LCD_SendByte(comm_data & 0xF0);           //send D7 D6 D5 D4 0 0 0 0
	delay(1);                            
	LCD_SendByte((comm_data & 0x0F)<<4);      //send D3 D2 D1 D0 0 0 0 0
	delay(150);
}


//数据输入
void LCD_SendData(uchar send_data)
{
	LCD_SendByte(0xFA & 0xFF);                // 1 1 1 1 1 RW=0 RS=1 0
	delay(1);                            
	LCD_SendByte(send_data & 0xF0);           //send D7 D6 D5 D4 0 0 0 0
	delay(1);                            
	LCD_SendByte((send_data & 0x0F)<<4);      //send D3 D2 D1 D0 0 0 0 0
	delay(150);
}


//设定光标位置
void Set_Cursor(uchar X, Y)			
{
	if (X == 0)
	{
		X = 0x80;
	}
	else if (X == 1)
	{
		X = 0x90;
	}
	else if (X == 2)
	{
		X = 0x88;
	}
	else if (X == 3)
	{
		X = 0x98;
	}
	Y = X + Y;
	LCD_SendComm(Y);

}


//清除屏幕
void lcd_Clear(void)						
{
	
	LCD_SendComm(SClear);
	lcd_DelayMs(30);
	LCD_SendComm(SOrigin);
	delay(150);
}

//显示字符串
void lcd_DispString(uchar X, Y, uchar *msg )
{

	Set_Cursor(X, Y);
	while (*msg != '\0')
	{
		LCD_SendData(*msg++);
	}
}

//坐标写入
void wrPosition(void)
{
	LCD_SendComm(SFunSetE);
	LCD_SendComm(lcdPosY);
	LCD_SendComm(lcdPosX);
	LCD_SendComm(SFunSetB);	
}	

//函数功能:全屏显示图形
//bmpCls=清除图形屏幕选项(OFF为不清屏,即显示图形,ON为清屏)
void lcd_DispPhoto (uchar *bmp, uchar bmpCls)
{
	lcdPosX = 0x80;
	halfScr = 2;
	
	for (;halfScr != 0; halfScr--)
	{
		lcdPosY = 0x80;
		halfLineCnt = 32;
		
		for (;halfLineCnt != 0; halfLineCnt--)
		{
			basicBlock = 16;
			wrPosition ();
			
			for (; basicBlock != 0; basicBlock--)
			{
				if (bmpCls == OFF)
				{
					LCD_SendData (*bmp++);
				}
				else if (bmpCls == ON)
				{
					LCD_SendData (0x00);
				}
			}
			lcdPosY++;
		}
		lcdPosX = 0x88;
	}
	LCD_SendComm(SFunDrawOn);
	LCD_SendComm(SFunSetB);
}

//---------------------------------------------------------
//函数原形:void lcd_ConvertChar (uchar CX, CY, width)
//函数功能:字符反白显示
//输入参数:
//返回值  :
//---------------------------------------------------------
	
void lcd_ConvertChar (uchar CX, CY, width) 	
{
	lcd_DispPhoto(OFF,ON);
	lcdPosY = 0x80;

	if (CX == 0)
	{
		CX = 0x80;
		halfLineCnt = 16;
	}
	else if (CX == 1)
	{
		CX = 0x80;
		halfLineCnt = 32;
	}
	else if (CX == 2)
	{
		CX = 0x88;
		halfLineCnt = 16;
	}
	else if (CX == 3)
	{
		CX = 0x88;
		halfLineCnt = 32;
	}
	lcdPosX = CX + CY;
	
    for (; halfLineCnt != 0; halfLineCnt--)
	{
		basicBlock = width;
		wrPosition();
	
		for (;basicBlock != 0; basicBlock--)
		{
			if (halfLineCnt > 16)
			{
				LCD_SendData(0x00);
			}
			else
			{
				LCD_SendData (0xff);
			}
		}
		lcdPosY++;
	}

	LCD_SendComm(SFunDrawOn);
	LCD_SendComm(SFunSetB);

}

//---------------------------------------------------------
//函数原形:void lcd_LineChoose (uchar line)
//函数功能:选中某行反白显示
//输入参数:
//返回值  :
//---------------------------------------------------------

void lcd_LineChoose (uchar line)
{
	if (line<4)
	{
		lcd_ConvertChar(line,0,16);
		Set_Cursor(line,7);	
	}

}





//初始化
void LCD_init(void)
{
	SCS=0;
	lcd_DelayMs(30);
	SCS=1;
	SCS=1;
	SCLK=0;
	lcd_DelayMs(40);
	LCD_SendComm(SFunSetE);            //功能设置,一次送8位数据,基本指令集
	delay(300);
	LCD_SendComm(SFunSetB);            //功能设置,一次送8位数据,基本指令集
	delay(300);
	LCD_SendComm(SDispOn);            //0000,1100  整体显示,游标off,游标位置off
	delay(300);
	LCD_SendComm(SClear);            //0000,0001 清DDRAM
	delay(300);
	lcd_DelayMs(1);
	LCD_SendComm(SEntryMode);            //0000,0010 DDRAM地址归位
	lcd_DelayMs(1);
	LCD_SendComm(SOrigin);            //1000,0000 设定DDRAM 7位地址000,0000到地址计数器AC
	lcd_DelayMs(1);
	//LCD_SendComm(0x04);            //点设定,显示字符/光标从左到右移位,DDRAM地址加 一
	lcd_DelayMs(1);
	// LCD_SendComm(0x0f);            //显示设定,开显示,显示光标,当前显示位反白闪动
	lcd_DelayMs(5);	
}


/*****************************************按键和显示****************************************/
void DispPage(uchar page)
{
	char xdata buffer[20];
	lcd_Clear();
	switch (page)
	{
	case 0:
	      {
	       sprintf(buffer,"Tout: %5.2f C",Tout/100.0);
	       lcd_DispString(0,0,buffer);

            sprintf(buffer,"FlowRate: %3.1f L" ,Rate/100.0);
            lcd_DispString(1,0,buffer);

            sprintf(buffer,"Tset: %5.2f C" ,Tset/100.0);
	        lcd_DispString(2,0,buffer);

			lcd_DispString(3,0,"Status:");
			FaultDisp();
              }
         break;
  /*       case 1:
               {    	   
            sprintf(buffer,"Tset: %.2f C" ,Tset/100.0);
	        lcd_DispString(0,0,buffer);
            sprintf(buffer,"Pout: %.2f" ,Pout/100.0);
	        lcd_DispString(1,0,buffer);

            sprintf(buffer,"FlowLim: %.1fL/M" ,Lim/100.0);
	        lcd_DispString(2,0,buffer);

			lcd_DispString(3,0,"Fault:");
			FaultDisp();

                } break;*/
				default: break;
		}
}

void ScreenFresh()
{
	char xdata buffer[20];
	switch (Page)
	{
			case 0:
	      {
	       sprintf(buffer,"Tout: %5.2f C",Tout/100.0);
	       lcd_DispString(0,0,buffer);

            sprintf(buffer,"FlowRate: %3.1f L" ,Rate/100.0);
            lcd_DispString(1,0,buffer);

            sprintf(buffer,"Tset: %5.2f C" ,Tset/100.0);
	        lcd_DispString(2,0,buffer);

			lcd_DispString(3,0,"Status:");
			FaultDisp();
              }
         break;
    /*     case 1:
               {    	    
            sprintf(buffer,"Tset: %.2f C" ,Tset/100.0);
	        lcd_DispString(0,0,buffer);

            sprintf(buffer,"Pout: %.2f" ,Pout/100.0);
	        lcd_DispString(1,0,buffer);

            sprintf(buffer,"FlowLim: %.1fL/M" ,Lim/100.0);
	        lcd_DispString(2,0,buffer);

			lcd_DispString(3,0,"Fault:");
			FaultDisp();

                } break;*/
				default: break;
		}

}

void SubProcess(void)
{
	xdata char buffer[10];
	if(Option == 0)                                  //ISet
	{
		if(Tset > TsetL)
		{
			Tset-=10;
			if(Tset < TsetL)
			{
				Tset = TsetL;
			}
		}
		sprintf(buffer,"%.2f C" ,Tset/100.0);
		lcd_DispString(2,3,buffer);					
	}
/*	else if(Option == 1)                              //Tset
	{
		if(Lim > LimL)
		{
			Lim = Lim-10;
			if (Lim < LimL)
			{
				Lim = LimL;
			}
		 }
            sprintf(buffer,"  %.1f L/M" ,Lim/100.0);
		lcd_DispString(2,3,buffer);
	}
*/	
}

void AddProcess(void)
{
	xdata char buffer[10];
	if(Option == 0)                           //Iset
	{
		if(Tset < TsetH)
		{
			Tset+=10;
			if(Tset > TsetH)
			{
				Tset = TsetH;
			}
		}
		sprintf(buffer,"%.2f C" ,Tset/100.0);
		lcd_DispString(2,3,buffer);					
	}
/*	else if(Option == 1)
	{
		if(Lim < LimH)
		{
			Lim += 10;			
			if (Lim > LimH)
			{
				Lim = LimH;
			}
		}
            sprintf(buffer,"  %.1f L/M" ,Lim/100.0);
		lcd_DispString(2,3,buffer);
	}
	*/
}

void key_process(uchar key_number)
{
	switch(key_number)
	{
		case KEY_STANDBY:
 	    {
			if(Moder==0)
			{
				nop();
				if(WATER==1)
				{
					WATER=0;
					Ldelay();
					Fault();
				}
			}
			if(Moder==1)
			{
				begin=!begin;
				if(begin==0)
				{
					write7279(SEGOFF,  LED_Standby);				
				}			
				if(begin==1)
				{
					if(WATER==1)
					{
						WATER=0;
						Ldelay();
						Fault();
					}
					write7279(SEGON,  LED_Standby);
				}
			}
		}
		case KEY_SUB:
		{    
			if(DataChflag)
			{
				SubProcess();
			}
		}break;	
		case KEY_ADD:
		{   
			if(DataChflag)
			{
				AddProcess();
			}
		}break; 
/*		case KEY_DISPLAY:
		{  
 	    	DataChflag=0;
			write7279(SEGOFF,  LED_Enter);
			Option=2;
           if(Page >=1)
		   {
              Page = 0;
		   }
           else
		   {
	          Page++; 
		   }
		   DispPage(Page);

		}break;*/
		case KEY_ENTER:
		{
			if(Moder==1)
			{
				if(DataChflag>=1)
				{
					DataChflag=0;
				}
				else
				{
					DataChflag++;
				}
				if(DataChflag==0)   
				{
			//		DataChflag = 0;//退出更改数据模式
					StoreData2();
					write7279(SEGOFF,  LED_Enter);
					//刷新页面
					DispPage(Page);	
				}
				if(DataChflag==1)
				{
					Option=0;
					Page=0;
					write7279(SEGON,  LED_Enter);
					DispPage(Page);	
					lcd_LineChoose(2);
				}
	/*
				if(DataChflag==2)
				{
				//	DataChflag = 1;   //进入更改数据模式
					Option=1;
					Page=1;
					write7279(SEGON,  LED_Enter);
					DispPage(Page);	
					lcd_LineChoose(2);
				}*/
			}
			else
			{
				nop();
			}
		}break;
		case KEY_MODE:
		{
			Moder++;
			if(Moder >= 2)
			{
				Moder = 0;
			}
		    DispPage(Page);	
			if(Moder==0)
			{
				DataChflag=0;
				begin=0;
				write7279(SEGOFF,  LED_Standby);
				write7279(SEGOFF,  LED_Enter);
				write7279(SEGOFF,  LED_Internal);
				write7279(SEGON,  LED_External);
				ExTempSet();
				delay(1000);
			}
			if(Moder==1)
			{
				write7279(SEGON,  LED_Internal);
				write7279(SEGOFF,  LED_External);
				StoreData2();
			}
		}
		default : break;
	}
}

void key_detect()                     
{
	int key_number;
	uint i = 0;
	longflag = 0;
	KEY = 1;
	if(!KEY)
	{
		delay(6000);
		key_number = read7279(CMD_READ);
		key_process(key_number);
	    delay(65000);
		delay(64000);
	    delay(63000);
		delay(62000);
	    delay(61000);
	    delay(60000);
	    KEY = 1;
		while(!KEY)						//加减键长按连续调整
		{
			i++;
			delay(200);
			if(i >= 1000)
			{
				i = 0;
				key_number = read7279(CMD_READ);
				if(key_number == KEY_SUB || key_number == KEY_ADD)
				{
					longflag = 1;
					key_process(key_number);
				}
			}	
		}	
	}
}
void Lcd_Start()
{
	uint  i=0;
	LCD_init();	
	lcd_Clear();

	lcd_DispString(0, 0, "    E0-WC150    ");   
	lcd_DispString(1, 0, "  Water Chiller ");  
	lcd_DispString(2, 0, "    NO.:  01    ");
    lcd_DispString(3, 0, "  Warming Up... ");
 

	lcd_DelayMs(6510);
	lcd_DelayMs(6430);
	lcd_DelayMs(6550);
	Page=0;
	
	send_byte(CMD_RESET);         //7279 reset
	delay(50000);
	send_byte(CMD_RESET);  
	write7279(SEGON,  LED_Power);
	write7279(SEGON,  LED_External);
	EA = 1;	 
	lcd_DelayMs(500);
	delay(1);	
}

/*******************************************测温测流量***********************************/
uint GetADCData(uchar channel)
{	
	uchar xdata dth,dtl;
	uint xdata sump = 0;
	uint xdata i;	
	for(i=0; i<20; i++)
	{
		ADCCON2 = channel;   //select channel to convert
		SCONV = 1;
		while(SCONV);
		dth = ADCDATAH;
		dtl = ADCDATAL;
		dth = dth&0x0f;
		sump += dtl+dth*256;
		delay(50);
	}
	return sump / 20;
}

uint filter(uchar channel)       //递推平均滤波法
{ 
    char  count;
    uint  sum = 0;
	if(channel == 0)
	{
		filter0_buf[array0++] = GetADCData(0);
		if (array0 == N0)  array0 = 0;	
		for (count=0;count<N0;count++)
		{
		   sum += filter0_buf[count]/10.0;
		}
		return (uint)(sum/N0*10.0);
	}
	else if(channel == 2)
	{
		filter2_buf[array2++] = GetADCData(2);
		if (array2 == N2)  array2 = 0;	
		for (count=0;count<N2;count++)
		{
		   sum += filter2_buf[count];
		}
		return (uint)(sum/N2);
	}
	else if(channel == 3)
	{
		filter3_buf[array3++] = GetADCData(3);
		if (array3 == N3)  array3 = 0;	
		for (count=0;count<N3;count++)
		{
		   sum += filter3_buf[count];
		}
		return (uint)(sum/N3);
	}
	else if(channel == 4)
	{
		filter4_buf[array4++] = GetADCData(4);
		if (array4 == N4)  array4 = 0;	
		for (count=0;count<N4;count++)
		{
		   sum += filter4_buf[count];
		}
		return (uint)(sum/N4);
	}
	else if(channel == 5)
	{
		filter5_buf[array5++] = GetADCData(5);
		if (array5 == N5)  array5 = 0;	
		for (count=0;count<N5;count++)
		{
		   sum += filter5_buf[count];
		}
		return (uint)(sum/N5);
	}
	else if (channel == 6)
	{
		filter6_buf[array6++] = GetADCData(6);
		if (array6 == N6)  array6 = 0;	
		for (count=0;count<N6;count++)
		{
		   sum += filter6_buf[count];
		}
		return (uint)(sum/N6);
	}
	else if (channel == 7)
	{
		filter7_buf[array7++] = GetADCData(7);
		if (array7 == N7)  array7 = 0;	
		for (count=0;count<N7;count++)
		{
		   sum += filter7_buf[count];
		}
		return (uint)(sum/N7);
	}		
}


void GetTemTout()                              //测出水温度
{
	uint  adresult;
	float  v,tem,logval;
	float  m,n,z;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -