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

📄 my_clock.c

📁 LPC2104 LCD 16x2 显示驱动 字符显示功能 带PROTEUS 仿真功能
💻 C
📖 第 1 页 / 共 2 页
字号:

}

//======================设置日期

setdate()
{   
	uchar cru=0,max=1,xp=2,key;   //定义无符号字符类型
    uchar year,month,day;        
	buf[0]=date.year/10;   //设置年高位  
	buf[1]=date.year%10;   //设置年底位
	buf[2]=date.month/10;  //设置月高位
	buf[3]=date.month%10;  //设置月底位
	buf[4]=date.day/10;    //设置日高位
	buf[5]=date.day%10;    //设置日高位
	buf[6]='s'-48;         //设置"确定"位
	ClrLcd();
	
	WriteString("Set Date",3,0);  //各位显示位置
	LcdPos(0,1);
	LcdWd('2');
	LcdWd('0');
	LcdWd(buf[0]+48);
	LcdWd(buf[1]+48);
	LcdPos(4,1);
	LcdWd('-');
	LcdWd(buf[2]+48);
	LcdWd(buf[3]+48);
	LcdPos(7,1);
	LcdWd('-');
	LcdWd(buf[4]+48);
	LcdWd(buf[5]+48);
	SetCur(CurNoFlash);
	LcdPos(xp,1);
	while(key=getkey())
	{	
		if(key==1)
		{ 
			if(cru==6) 
			{
				year=buf[0]*10+buf[1];
				month=buf[2]*10+buf[3];
				day=buf[4]*10+buf[5];
				key=day;
				if(0<month && month<13 && day!=0)  //写入日期
				{   
					key=monthday[month];
					if((year%4)==0 && month==2)  key++;
					if(day<=key)
					{
					date.year=year;
					date.month=month;
					date.day=day;
					save();
					ClrLcd();
					SetCur(NoCur);
					calweek();
					return;
					}
					else 
					{
					WriteString("Date Wrong!    ",0,0);	  //写入错误
					cru=4;
					max=2;
					xp=8;
					}
				 }
				else 
				{
				WriteString("Date Wrong!    ",0,0);     //写入错误
				cru=2;
				max=1;
				xp=5;
				}

			}


			if((++buf[cru])>max) 
			{
				buf[cru]=0;
			}
			LcdPos(xp,1);   //清屏幕,待显示
			LcdWd(buf[cru]+48);
			

		}
		else if(key==2)
		{
			if(++cru>6) cru=0;
				switch (cru)
				{
				case 0:{max=9;xp=2;} break;
				case 1:{max=9;xp=3;} break;
				case 2:{max=1;xp=5;} break;
				case 3:{max=9;xp=6;} break;
				case 4:{max=3;xp=8;} break;
				case 5:{xp=9;max=9;} break;
				case 6:{xp=10;} break;

				}
		}
		else if(key==3)
		{
			if(cru==0) cru=6; else cru--;
			switch (cru)
			{
				case 0:{max=9;xp=2;} break;
				case 1:{max=9;xp=3;} break;
				case 2:{max=1;xp=5;} break;
				case 3:{max=9;xp=6;} break;
				case 4:{max=3;xp=8;} break;
				case 5:{xp=9;max=9;} break;
				case 6:{xp=10;} break;

			}
		}

	  LcdPos(xp,1);
	} 
	SetCur(NoCur);
}

///============================
menumode()   //菜单修改
{   	uchar num=0,key;
		menudis(num);   																						
		while(key=getkey())
		{
			
			if(key==1)
			{
				switch(num)
				{
					case 0: {settime();ClrLcd(); return;}
							break; 
					case 1: {setdate();ClrLcd();return;}
							break; 
					case 2: {setbell();
								ClrLcd();return;}
							break;
					case 3: {cancel();ClrLcd();return;}
							break;
				}
			}
			else if(key==2)
			{
			if(++num>3) num=0;
			
			}
			else if(key==3)
			{
			if(num) num--;
			else num=3;
			}
			else
			{
			}
			menudis(num);
		}
		ClrLcd();

}

calweek()   //星期对应日期子程序
{
	uchar year;
	year=date.year/4;
	week=(weekmon[date.month]+date.day+6+year+date.year)%7;
}

weekdis()  //写星期
{
WriteString(&weekday[week][0],11,0);
}

dayinc()   //日期约束子程序
{
	uchar i,year;
	i=monthday[date.month];
	year=date.year & 0x03;
	if(date.month==2)
	{
	if( year==0 && date.year )  i++;
	}
	date.day++;
	if(date.day>i)
	{ 
		date.day=1;
		if(++date.month>12) 
		{
		date.month=1;
		date.year++;
		}
	}
	calweek();


}
///秒加一程序   
secinc()
{
	if(++time.sec>=60)
		{
			time.sec=0;  //秒回零
			bellfla=1;
			if(++time.min>=60)
			{
				time.min=0;  //分回零
				if(++time.hour>=24)
			 	{
				time.hour=0;
				dayinc();
				}
			}
		}
}  

///时间显示程序   
timedis()
{  
	LcdPos(3,1);
	LcdWd(time.hour/10+48);
	LcdWd(time.hour%10+48);
	LcdPos(5,1);
	LcdWd(':');
	LcdWd(time.min/10+48);
	LcdWd(time.min%10+48);
	LcdPos(8,1);
	LcdWd(':');
	LcdWd(time.sec/10+48);
	LcdWd(time.sec%10+48);
}
datedis()  //日期显示程序  
{ 	LcdPos(0,0);
	LcdWd('2');
	LcdWd('0');
	LcdWd(date.year/10+48);
	LcdWd(date.year%10+48);
	LcdPos(4,0);
	LcdWd('-');
	LcdWd(date.month/10+48);
	LcdWd(date.month%10+48);
	LcdPos(7,0);
	LcdWd('-');
	LcdWd(date.day/10+48);
	LcdWd(date.day%10+48);
}

strbell()   //响铃子程序
{
	reflash=0;
	ClrLcd();
	WriteString("Timer Over",0,0);
	while(1)
	{
		if(reflash<5)
		BELLING=0;
		else if(reflash<7)
		BELLING=1;         //响铃
		else if(reflash<12)
		{
			BELLING=0 ;
			if((P1&0x0f)!=0x0f)
			{
			BELLING=1;     //响铃
			break;
			}
		}
		else
		{
			BELLING=1;    //响铃
			break;
		}

	timedis();   //返回
	//datedis();		  
	weekdis();
	}
	ClrLcd();

}

bell()
{   uchar i,hour;
	for(i=0;i<40;i++)
	{
	
		if(belltime[i].hour>50)
		{
	 	hour=belltime[i].hour & 0x7f;
	 	if(hour==time.hour & belltime[i].min==time.min)  strbell();
		}
	}
}

timer2() interrupt 5   //中断溢出
{
	static uint ovtime;
	if(++ovtime>=20)
	{
		ovtime=0;
		secinc();
		reflash++;
		
	}
	TF2=0;

}

uchar getkey()    //声明getkey函数
{
	uchar key=0;
	uint i;
	reflash=0;
	while(reflash<10)
	{
		if((P1&0x0f)!=0x0f)
		{
			mDelay(100);
			if((P1&0x0f)!=0x0f)
			{
				if(!ESC)	{key=0;break;}
				else if(!EN) {key=1;break;}
				else if(!UP) {key=2;break;}
				else if(!DOWN) {key=3;break;}
				else {key=0;break;}
						  

			}  			
		}
		
	}
	return key;
}


initial()   //学号各位数字对应码
{ 	if(
	ID[0]==51      //"3"
	&&ID[1]==49    //"1"
	&&ID[2]==48    //"0"
	&&ID[3]==53    //"5"
	&&ID[4]==48    //"0"
	&&ID[5]==48    //"0"
	&&ID[6]==57    //"9"
	&&ID[7]==55    //"7"
	&&ID[8]==50    //"2"
	&&ID[9]==48    //"0"
    )
	{
	
	TL2=(0xffff-50000)%256;      //装初值底位
	TH2=(0xffff-50000)/256;      //装初值高位
	RCAP2L=(0xffff-50000)%256;   //重装初值底位
	RCAP2H=(0xffff-50000)/256;   //重装初值高位
	T2CON=0x04;  //开中断
	IE=0xa0;
	WriteString(&ID[0],3,2);   //写ID
	}
	else
	while(1);


}

cancel()    //清除函数
{
uchar i;
for(i=0;i<34;i++)
belltime[i].hour&=0x7f;
ClrLcd();
WriteString("Cancel alarm....",0,0);  //清除报警时间
WriteString("done",4,1);
getkey();

}

void main()   //主函数
{	//uchar xPos,yPos,i;
	
	RstLcd();
	ClrLcd(); 	
	SetCur(NoCur);  
	initial();
	getkey();
	ClrLcd();
	//====================                  //设置初始时间
	time.hour=14;
	time.min=28;
	time.sec=00;
	date.year=8;
	date.month=5;
	date.day=12;
	calweek();

	belltime[1].hour=0x01;     //闹钟1
	belltime[1].min=1;
	//belltime[1].hour|=0x80;

	
	belltime[2].hour=0x02;    //闹钟2
	belltime[2].min=2;
	//belltime[2].hour|=0x80;

	belltime[3].hour=0x03;    //闹钟3
	belltime[3].min=3;
//	belltime[3].hour|=0x80;
	//===================
//	xPos=3;
//	yPos=0;
	
	            	//开光标显示、闪烁    
	while(1)
	{


		if(reflash) 
		{
			timedis();
			datedis();
			weekdis();
			reflash=0; 
			if(bellfla) 
			{
			bell();					    
			bellfla=0;
			}
			
		}
		if(EN==0)  
			{
			 mDelay(100);
			 if(EN==0) menumode();
			}

		
  	}
}


⌨️ 快捷键说明

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