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

📄 timer.c

📁 This Program Cotains Eeprom Read-write functions, 4 Bit LCD interface routines, Keyboard Interface w
💻 C
字号:
#include<reg52.h>
#include<ee.h>
#include<eprom.h>
#include<lcdfuncs.h>

sbit relay1=P3^1;
sbit relay2=P3^5;
sbit relay3=P3^6;
//sbit relay4=P3^7;
sbit i1=P1^5;
sbit i2=P1^6;
sbit i3=P1^4;

char char_sel=1,screen_no,ccc1,ccc2,ccc3;

unsigned int on_time_rl1,on_time_rl2,on_time_rl3;
unsigned int off_time_rl1,off_time_rl2,off_time_rl3;
unsigned int t_i1_on=0,t_i2_on=0,t_i3_on=0;
unsigned int t_i1_off=0,t_i2_off=0,t_i3_off=0;
char on_time1_rl1,on_time2_rl1,on_time3_rl1;
char on_time1_rl2,on_time2_rl2,on_time3_rl2;
char on_time1_rl3,on_time2_rl3,on_time3_rl3;
char off_time1_rl1,off_time2_rl1,off_time3_rl1;
char off_time1_rl2,off_time2_rl2,off_time3_rl2;
char off_time1_rl3,off_time2_rl3,off_time3_rl3;
bit in_1_on_flag=0,in_2_on_flag=0,in_3_on_flag=0;
bit in_1_off_flag=0,in_2_off_flag=0,in_3_off_flag=0;

int k;
unsigned int temp_time=0;

char *x;
key_pressed();

void delay(unsigned int del)
{
	unsigned long int j;
	for(j=0;j<del*2;j++);
}
display_count (unsigned char c3,unsigned char c2,unsigned char c1)  
{
write_LCD_byte(LCD_CONTROL,LINE_2 + 8);
	write_LCD_byte(LCD_DATA,c3 + 0x30);
write_LCD_byte(LCD_CONTROL,LINE_2 + 9);
	write_LCD_byte(LCD_DATA,'.');
write_LCD_byte(LCD_CONTROL,LINE_2 + 10);
	write_LCD_byte(LCD_DATA,c2 + 0x30);
write_LCD_byte(LCD_CONTROL,LINE_2 + 11);
	write_LCD_byte(LCD_DATA,c1 + 0x30);
write_LCD_byte(LCD_CONTROL,LINE_2 + 13);
	write_LCD_byte(LCD_DATA,'S');
write_LCD_byte(LCD_CONTROL,LINE_2 + 14);
	write_LCD_byte(LCD_DATA,'e');
write_LCD_byte(LCD_CONTROL,LINE_2 + 15);
	write_LCD_byte(LCD_DATA,'c');
	switch(char_sel)
	{
		case 1:		write_LCD_byte(LCD_CONTROL,0x0E);
					write_LCD_byte(LCD_CONTROL,LINE_2 + 11);
					break;
		case 2:		write_LCD_byte(LCD_CONTROL,0x0E);
					write_LCD_byte(LCD_CONTROL,LINE_2 + 10);
					break;
		case 3:		write_LCD_byte(LCD_CONTROL,0x0E);
					write_LCD_byte(LCD_CONTROL,LINE_2 + 8);
					break;
		default:	write_LCD_byte(LCD_CONTROL,0x0C);
					break;
	}					   
}

main()
{
	delay(100);
//	P0=0xff;
	P2=0XFF;
	P1=0xff;
	P3=0xff;
	SP=0x7f;
	TL1=0x2f;
	TH1=0xf8;
	TMOD=(TMOD & 0x0f) | 0x10;
	ET1=1;
	IT1=1;
	EA=1;
	WP=0;
	sda=1;
	scl=1;
	delay(100);
	init_2_line_LCD();
	delay(10);
	ee_byte_from_ee(0x0b,&on_time1_rl1);delay(10);
	ee_byte_from_ee(0x1b,&on_time2_rl1);delay(10);
	ee_byte_from_ee(0x2b,&on_time3_rl1);delay(10);
	ee_byte_from_ee(0x4b,&on_time1_rl2);delay(10);
	ee_byte_from_ee(0x5b,&on_time2_rl2);delay(10);
	ee_byte_from_ee(0x6b,&on_time3_rl2);delay(10);
	ee_byte_from_ee(0x7b,&on_time1_rl3);delay(10);
	ee_byte_from_ee(0x8b,&on_time2_rl3);delay(10);
	ee_byte_from_ee(0x9b,&on_time3_rl3);delay(10);
	ee_byte_from_ee(0xab,&off_time1_rl1);delay(10);
	ee_byte_from_ee(0xbb,&off_time2_rl1);delay(10);
	ee_byte_from_ee(0xcb,&off_time3_rl1);delay(10);
	ee_byte_from_ee(0xdb,&off_time1_rl2);delay(10);
	ee_byte_from_ee(0xeb,&off_time2_rl2);delay(10);
	ee_byte_from_ee(0x43,&off_time3_rl2);delay(10);
	ee_byte_from_ee(0x53,&off_time1_rl3);delay(10);
	ee_byte_from_ee(0x63,&off_time2_rl3);delay(10);
	ee_byte_from_ee(0x73,&off_time3_rl3);delay(10);
	on_time_rl1=on_time1_rl1+(10*on_time2_rl1)+(100*on_time3_rl1);
	on_time_rl2=on_time1_rl2+(10*on_time2_rl2)+(100*on_time3_rl2);
	on_time_rl3=on_time1_rl3+(10*on_time2_rl3)+(100*on_time3_rl3);
	off_time_rl1=off_time1_rl1+(10*off_time2_rl1)+(100*off_time3_rl1);
	off_time_rl1=off_time1_rl2+(10*off_time2_rl2)+(100*off_time3_rl2);
	off_time_rl1=off_time1_rl3+(10*off_time2_rl3)+(100*off_time3_rl3);
	delay(10);
	screen_no=0;
	clear_lcd();
	write_LCD_line(LINE_1+2,"WELCOME TO");
	write_LCD_line(LINE_2+4,"ALLIED ENGG.");
		delay(150);
	TR1=1;
	
	while(1)
	{
	 if(i1==0 & relay1==1 & relay2==1 & relay3==1)	in_1_on_flag=1;	else	in_1_on_flag=0;
	 if(i2==0 & relay1==1 & relay2==1 & relay3==1)	in_2_on_flag=1;	else	in_2_on_flag=0;
	 if(i3==0 & relay1==1 & relay2==1 & relay3==1)	in_3_on_flag=1;	else	in_3_on_flag=0;
	 if(i1==1)
	 {
	 	in_1_off_flag=1;
		in_1_on_flag=0;
		t_i1_on=0;
	 }	else{	in_1_off_flag=0;	t_i1_off=0;	}
	 if(i2==1)
	 {
	 	in_2_off_flag=1;
		in_2_on_flag=0;
		t_i2_on=0;
	 }	else{	in_2_off_flag=0;	t_i2_off=0;}
	 if(i3==1)	
	 {
	 	in_3_off_flag=1;
		in_3_on_flag=0;
		t_i3_on=0;
	 }	else{	in_3_off_flag=0;	t_i3_off=0;}

		if(t_i1_on>=on_time_rl1)
		{
			relay1=0;
			in_1_on_flag=0;
			t_i1_on=0;
			in_1_off_flag=0;
			t_i1_off=0;
		}
		if(t_i2_on>=on_time_rl2)
		{
			relay2=0;
			in_2_on_flag=0;
			t_i2_on=0;
			in_2_off_flag=0;
			t_i2_off=0;
		}
		if(t_i3_on>=on_time_rl3)
		{
			relay3=0;
			in_3_on_flag=0;
			t_i3_on=0;
			in_3_off_flag=0;
			t_i3_off=0;
		}
		if(t_i1_off>=off_time_rl1 & i1==1)
		{
			relay1=1;
			in_1_off_flag=0;
			t_i1_off=0;
		}
		if(t_i2_off>=off_time_rl2 & i2==1)
		{
			relay2=1;
			in_2_off_flag=0;
			t_i2_off=0;
		}
		if(t_i3_off>=off_time_rl3 & i3==1)
		{
			relay3=1;
			in_3_off_flag=0;
			t_i3_off=0;
		}
		
		if(P2!=0xff)
			key_pressed();
	}
}	// End of Main routine.
key_pressed()
{
delay(1500);
switch(P2)
		{			
			case 0x7f :			//	SET key
						clear_lcd();
						if(screen_no==0)	
						{
							screen_no=1;
							write_LCD_line(LINE_1,"I/P 1 ON TIME:");
							char_sel=1;
							ccc1=on_time1_rl1;
							ccc2=on_time2_rl1;
							ccc3=on_time3_rl1;
						}
						else if(screen_no==1)
						{	
							on_time1_rl1=ccc1;
							on_time2_rl1=ccc2;
							on_time3_rl1=ccc3;
							on_time_rl1=on_time1_rl1+(10*on_time2_rl1)+(100*on_time3_rl1);
							ee_byte_to_ee(0x0b,on_time1_rl1);delay(10);
							ee_byte_to_ee(0x1b,on_time2_rl1);delay(10);
							ee_byte_to_ee(0x2b,on_time3_rl1);delay(10);
							screen_no=2;
							write_LCD_line(LINE_1,"I/P 2 ON TIME:");
							char_sel=1;
							ccc1=on_time1_rl2;
							ccc2=on_time2_rl2;
							ccc3=on_time3_rl2;
						}
						else if(screen_no==2)	
						{	
							on_time1_rl2=ccc1;
							on_time2_rl2=ccc2;
							on_time3_rl2=ccc3;
							on_time_rl2=on_time1_rl2+(10*on_time2_rl2)+(100*on_time3_rl2);
							ee_byte_to_ee(0x4b,on_time1_rl2);		   delay(10);
							ee_byte_to_ee(0x5b,on_time2_rl2);	delay(10);
							ee_byte_to_ee(0x6b,on_time3_rl2);delay(10);
							screen_no=3;
							write_LCD_line(LINE_1,"I/P 3 ON TIME:");
							char_sel=1;
							ccc1=on_time1_rl3;
							ccc2=on_time2_rl3;
							ccc3=on_time3_rl3;
						}
						else if(screen_no==3)	
						{	
							on_time1_rl3=ccc1;
							on_time2_rl3=ccc2;
							on_time3_rl3=ccc3;
							on_time_rl3=on_time1_rl3+(10*on_time2_rl3)+(100*on_time3_rl3);
							ee_byte_to_ee(0x7b,on_time1_rl3);	delay(10);
							ee_byte_to_ee(0x8b,on_time2_rl3);	delay(10);
							ee_byte_to_ee(0x9b,on_time3_rl3);	delay(10);
							screen_no=4;
							write_LCD_line(LINE_1,"I/P 1 OFF TIME:");
							char_sel=1;
							ccc1=off_time1_rl1;
							ccc2=off_time2_rl1;
							ccc3=off_time3_rl1;
						}
						else if(screen_no==4)	
						{	
							off_time1_rl1=ccc1;
							off_time2_rl1=ccc2;
							off_time3_rl1=ccc3;
							off_time_rl1=off_time1_rl1+(10*off_time2_rl1)+(100*off_time3_rl1);
							ee_byte_to_ee(0xab,off_time1_rl1);	delay(10);
							ee_byte_to_ee(0xbb,off_time2_rl1);	delay(10);
							ee_byte_to_ee(0xcb,off_time3_rl1);	delay(10);
							screen_no=5;
							write_LCD_line(LINE_1,"I/P 2 OFF TIME:");
							char_sel=1;
							ccc1=off_time1_rl2;
							ccc2=off_time2_rl2;
							ccc3=off_time3_rl2;
							
						}
						else if(screen_no==5)	
						{	
							off_time1_rl2=ccc1;
							off_time2_rl2=ccc2;
							off_time3_rl2=ccc3;
							off_time_rl2=off_time1_rl2+(10*off_time2_rl2)+(100*off_time3_rl2);
							ee_byte_to_ee(0xdb,off_time1_rl2);	delay(10);
							ee_byte_to_ee(0xeb,off_time2_rl2);	delay(10);
							ee_byte_to_ee(0x43,off_time3_rl2);	delay(10);
							screen_no=6;
							write_LCD_line(LINE_1,"I/P 3 OFF TIME:");
							char_sel=1;
							ccc1=off_time1_rl3;
							ccc2=off_time2_rl3;
							ccc3=off_time3_rl3;
							
						}
						else if(screen_no==6)	
						{	
							off_time1_rl3=ccc1;
							off_time2_rl3=ccc2;
							off_time3_rl3=ccc3;
							off_time_rl3=off_time1_rl3+(10*off_time2_rl3)+(100*off_time3_rl3);
							ee_byte_to_ee(0x53,off_time1_rl3);	delay(10);
							ee_byte_to_ee(0x63,off_time2_rl3);	delay(10);
							ee_byte_to_ee(0x73,off_time3_rl3);delay(10);
							write_LCD_byte(LCD_CONTROL,0x0C);	
							screen_no=0;
							char_sel=0;
							write_LCD_line(LINE_1+2,"WELCOME TO");
							write_LCD_line(LINE_2+4,"ALLIED ENGG.");
						}
						if(screen_no!=0)
						{
							display_count (ccc3,ccc2,ccc1);
						}
						break;
			case 0xbf :			//	SHIFT key
						
						if(screen_no!=0)
						{
							switch(char_sel)
							{
								case 1:	char_sel=2;	break;
								case 2:	char_sel=3;	break;
								case 3:	char_sel=1;	break;	
								default:char_sel=1; break;
							}
						display_count (ccc3,ccc2,ccc1);	
						}
						break;
			case 0xdf :			//	INC key
						if(screen_no!=0)
						{
							if(char_sel==3)
							{
								ccc3++;
								if(ccc3==10)
									ccc3=0;
	    					}
							if(char_sel==2)
							{
								ccc2++;
								if(ccc2==10)
									ccc2=0;
	    					}
					   		if(char_sel==1)
							{
								ccc1++;
								if(ccc1==10)
									ccc1=0;
								if(ccc3==0 & ccc2==0 & ccc1==0)
									ccc1=1;
							}
						display_count (ccc3,ccc2,ccc1);
						}
						break;
			case 0xef :			//	RESET key
						
					 	if(screen_no==1)	//	Enter on time
						{	
							ee_byte_from_ee(0x0b,&on_time1_rl1);delay(10);
							ee_byte_from_ee(0x1b,&on_time2_rl1);delay(10);
							ee_byte_from_ee(0x2b,&on_time3_rl1);delay(10);
							ccc1=on_time1_rl1;
							ccc2=on_time2_rl1;
							ccc3=on_time3_rl1;
						}
						else if(screen_no==2)	//	Enter Relay Off Time
						{	
							ee_byte_from_ee(0x4b,&on_time1_rl2);delay(10);
							ee_byte_from_ee(0x5b,&on_time2_rl2);delay(10);
							ee_byte_from_ee(0x6b,&on_time3_rl2);delay(10);
							ccc1=on_time1_rl2;
							ccc2=on_time2_rl2;
							ccc3=on_time3_rl2;
						}
						else if(screen_no==3)	//	Enter no. of channels
						{	
							ee_byte_from_ee(0x7b,&on_time1_rl3);delay(10);
							ee_byte_from_ee(0x8b,&on_time2_rl3);delay(10);
							ee_byte_from_ee(0x9b,&on_time3_rl3);delay(10);
							ccc1=on_time1_rl3;
							ccc2=on_time2_rl3;
							ccc3=on_time3_rl3;							
						}
						else if(screen_no==4)	//	Enter no. of channels
						{	
							ee_byte_from_ee(0xab,&off_time1_rl1);delay(10);
							ee_byte_from_ee(0xbb,&off_time2_rl1);delay(10);
							ee_byte_from_ee(0xcb,&off_time3_rl1);delay(10);
							ccc1=off_time1_rl1;
							ccc2=off_time2_rl1;
							ccc3=off_time3_rl1;
						}
						else if(screen_no==5)	//	Enter no. of channels
						{	
							ee_byte_from_ee(0xdb,&off_time1_rl2);delay(10);
							ee_byte_from_ee(0xeb,&off_time2_rl2);delay(10);
							ee_byte_from_ee(0x43,&off_time3_rl2);delay(10);
							ccc1=off_time1_rl2;
							ccc2=off_time2_rl2;
							ccc3=off_time3_rl2;
						}
						else if(screen_no==6)	//	Enter no. of channels
						{	
							ee_byte_from_ee(0x53,&off_time1_rl3);delay(10);
							ee_byte_from_ee(0x63,&off_time2_rl3);delay(10);
							ee_byte_from_ee(0x73,&off_time3_rl3);delay(10);
							ccc1=off_time1_rl1;
							ccc2=off_time2_rl1;
							ccc3=off_time3_rl1;
						}
						if(screen_no!=0)
						display_count (ccc3,ccc2,ccc1);
						break;
			default :   break;
		}
}
init_timer1_isr() interrupt 3
{
	TL1=0x2f;
	TH1=0xf8;
	temp_time++;
	if(temp_time>=5)
	{	
		if(in_1_on_flag==1 & relay1==1)t_i1_on++;
		if(in_1_off_flag==1)t_i1_off++;
		if(in_2_on_flag==1 & relay2==1)t_i2_on++;
		if(in_2_off_flag==1)t_i2_off++;
		if(in_3_on_flag==1 & relay3==1)t_i3_on++;
		if(in_3_off_flag==1)t_i3_off++;
		temp_time=0;
	}
	TF1=0;
}

⌨️ 快捷键说明

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