Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

51时钟含闹钟(可按键设置)

$
0
0

        51时钟(含闹钟)是我做的第一个小东西,包括焊版,包括写代码,完了以后虽然觉得没什么,但是过程还是很享受的,下一个小东西是集成12864,红外,18B20和DS12C887来做个可以显示万年历和温度并且用红外来选择功能的小东西。下面是时钟的代码:


#include<reg52.h>
#include<define.h>
/*延时函数,毫秒级*/
void delay(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}
/*闹铃响函数*/
void music() 
{
	beep=0;
	delay(50);
	beep=1;
	delay(100);
	beep=0;
	delay(100);
	beep=1;
}
/*写命令函数,1604液晶*/
void write_com(uchar com)
{
	rs=0;
	wr=0;
	P1=com;
	lcden=0;
	delay(10);
	lcden=1;
	delay(10);
	lcden=0;	
}
/*写数据函数,1604液晶*/
void write_date(uchar date)
{
	rs=1;
	wr=0;
	P1=date;
	lcden=0;
	delay(10);
	lcden=1;
	delay(10);
	lcden=0;	
}
/*初始化函数*/
void init()
{
	uchar num;
	EA=1;
	EX0=1;
	IT0=1;

	lcden=0;
	//write_ds(0x0A,0x20);//打开振荡器
	read_ds(0x0c);
	write_com(0x80);
	for(num=0;num<15;num++)
	{
		write_date(table[num]);
		delay(5);
	}
	write_com(0x80+0x40);
	for(num=0;num<12;num++)
	{
		write_date(table1[num]);
		delay(5);
	}
}
/*写时间函数*/
void write_sfm(uchar add,uchar date)
{
	uchar shi,ge;
	shi=date/10;
	ge=date%10;
	write_com(0x80+0x40+add);
	write_date(0x30+shi);
	write_date(0x30+ge);
}
/*写年月日函数*/
void write_ymd(uchar add,uint num)
{

	write_com(0x80+add);
	temp=num/10;
	write_date(0x30+temp);
	temp=num%10;
	write_date(0x30+temp);

}
/*写星期函数*/
void write_week()
{
	write_com(0x80+0x0d);
	switch(week)
	{
	case 1:write_date('M');
		   write_date('O');
		   write_date('N');
		   break;
	case 2:write_date('T');
		   write_date('U');
	       write_date('E');
		   break;
	case 3:write_date('W');
		   write_date('E');
		   write_date('D');
		   break;
	case 4:write_date('T');
		   write_date('H');
		   write_date('U');
		   break;
	case 5:write_date('F');
		   write_date('R');
		   write_date('I');
		   break;
	case 6:write_date('S');
		   write_date('A');
		   write_date('T');
		   break;
	case 7:write_date(0x53);
		   write_date('U');
		   write_date('N');
		   break;
	}
}		
/*键盘扫描函数*/
void keyscan()
{
	/*若是功能键被按下*/	
	if(s1==0)	
	{
		delay(5);
		if(s1==0)
		{	
			s1num++;
			flag=1;
			while(!s1);
			if(s1num==1)//秒
			{
				write_com(0x80+0x40+10);
				write_com(0x0f);
			}
			if(s1num==2)//分
			{
				write_com(0x80+0x40+7);
			}
			if(s1num==3)//时
			{
				write_com(0x80+0x40+4);
			}
			if(flag1==1)s1num=8;//若是设置闹铃,则跳过年月日的设置
			if(s1num==4)//星期
			{
				write_com(0x80+0x0D);
			}
			if(s1num==5)//天
			{
				write_com(0x80+0x0a);
			}
			if(s1num==6)//月
			{
				write_com(0x80+0x07);
			}
			if(s1num==7)//年
			{
				write_com(0x80+0x04);
			}
			if(s1num==8)//退出
			{
				s1num=0;
				write_com(0x0c);
				flag=0;//退出功能键
				if(flag1==0)//若不是设置闹铃
				{
					lcd_reset();
					init();
					write_ds(0,miao);
					write_ds(2,fen);
					write_ds(4,shi);
					write_ds(6,week);
					write_ds(7,day);
					write_ds(8,month);
					write_ds(9,year);
				}
				else//若是闹铃设置,则将设定的时间付给闹铃时间变量,回到初始化界面
				{
					write_ds(1,miao);//写入闹铃单元
					write_ds(3,fen);
					write_ds(5,shi);
					lcd_reset();
					init();
				}

			}

		 }//if(s2==0)		
		}//if(s2==0)
		if(s1num!=0)
		{
			//增加键
			if(s2==0)
			{
				delay(1);
				if(s2==0)
				{
					while(!s2);
					if(s1num==1)
					{
						miao++;
						if(miao==60)
							miao=0;
						write_sfm(10,miao);
						write_com(0x80+0x40+10);
					}
					if(s1num==2)
					{
						fen++;
						if(fen==60)
							fen=0;
						write_sfm(7,fen);
						write_com(0x80+0x40+7);
					}
					if(s1num==3)
					{
						shi++;
						if(shi==24)
							shi=0;
						write_sfm(4,shi);
						write_com(0x80+0x40+4);
					}
					if(s1num==4)
					{
						week++;
						if(week==8)week=1;
						write_week();
						write_com(0x80+0x0D);
					}
					if(s1num==5)//日
					{
						day++;
						if(day==31)day=1;
						write_ymd(0x0a,day);
						write_com(0x80+0x0a);
					}
					if(s1num==6)//月
					{
						month++;
						if(month==12)month=1;
						write_ymd(7,month);
						write_com(0x80+0x07);
					}
					if(s1num==7)//年
					{
						year++;
						if(year==100)year=0;
						write_ymd(4,year);
						write_com(0x80+0x04);
					}
				}//if(s2==0)
			}//if(s2==0)
			//减少键
			if(s3==0)
			{
				delay(5);
				if(s3==0)
				{
					while(!s3);
					if(s1num==1)
					{
						if(miao==0)
						{
							miao=59;
							write_sfm(10,miao);
							write_com(0x80+0x40+10);
						}
						miao--;
						if(miao==-1)
							miao=59;
						write_sfm(10,miao);
						write_com(0x80+0x40+10);
					}
					if(s1num==2)
					{
						fen--;
						if(fen==-1)
							fen=59;
						write_sfm(7,fen);
						write_com(0x80+0x40+7);
					}
					if(s1num==3)
					{
						shi--;
						if(shi==-1)
							shi=23;
						write_sfm(4,shi);
						write_com(0x80+0x40+4);
					}
					if(s1num==4)//
					{
						week--;
						if(week==-1)week=7;
						write_week();
						write_com(0x80+0x0C);
					}
					if(s1num==5)//日
					{
						day--;
						if(day==-1)day=31;
						write_ymd(0x0a,day);
						write_com(0x80+0x0a);
					}
					if(s1num==6)//月
					{
						month--;
						if(month==-1)month=12;
						write_ymd(7,month);
						write_com(0x80+0x07);
					}
					if(s1num==7)//年
					{
						year--;
						if(year==-1)year=99;
						write_ymd(4,year);
						write_com(0x80+0x04);
					}
				}//if(s3==0)
			}//if(s3==0)
			//闹铃键
			if(s4==0)
			{
				delay(5);
				if(s4==0)
				{
					flag1=1;//闹铃设置开启
					while(!s4);
					/*在液晶上显示设置闹铃的界面*/
					lcd_reset();
					write_com(0x80);
					while(table2[i]!='\0')
					{
						write_date(table2[i]);
						i++;
					}
					i=0;
					write_com(0x80+0x40);
				   	while(table1[i]!='\0')
					{
						write_date(table1[i]);
						i++;
					}
					write_sfm(10,miao);
					write_sfm(7,fen);
					write_sfm(4,shi);
					/*显示出光标*/
					write_com(0x80+0x40+10);
					write_com(0x0f);
					i=0;
				}//if(s4==0)
			}//if(s4==0)
		}//if(s1num!=0)
		/*若闹铃响则按下闹铃键取消*/
		if(flag2) 
		{
				if(s4==0)
				{
					delay(5);
					if(s4==0)
					{
						while(!s4);
						flag2=0;
					}
				}
		}
} 
/*写数据函数*/
void write_ds(uchar add,uchar date)
{
	dscs=0;
	dsas=1;
	dsds=1;
	dsrw=1;
	P0=add;
	dsas=0;
	dsrw=0;
	P0=date;
	dsrw=1;
	dsas=1;
	dscs=1;				
}
/*读数据函数*/
uchar read_ds(uchar add)
{
 	uchar ds_date;
	dsas=1;
	dsds=1;
	dsrw=1;
	dscs=0;
	P0=add;
	dsas=0;
	dsds=0;
	P0=0xff;
	ds_date=P0;
	dsds=1;
	dsas=1;
	dscs=1;
	return ds_date;	
}
/*液晶初始化函数*/
void lcd_reset()
{
	delay(15);
	write_com(0x38);
	delay(5);
	write_com(0x38);
	delay(5);
	write_com(0x38);
	delay(5);
	write_com(0x08);
	write_com(0x01);
	write_com(0x06);
	write_com(0x0c);	
}
/*主函数*/
void main()
{
	lcd_reset();
	init();
	while(1)
	{
		keyscan();
		asec=read_ds(1);//读出闹铃时间
		amin=read_ds(3);
		ahour=read_ds(5);
		if(flag==0)//若是功能键未被按下,则运行时钟
		{
			miao=read_ds(0);	
			fen=read_ds(2);
			shi=read_ds(4);
			week=read_ds(6);
			day=read_ds(7);
			month=read_ds(8);
			year=read_ds(9);
			if(asec==miao&&amin==fen&&ahour==shi)flag2=1;//闹铃标志
			write_sfm(10,miao);
			write_sfm(7,fen);
			write_sfm(4,shi);
			write_week();
			write_ymd(0x0a,day);
			write_ymd(7,month);
			write_ymd(4,year);
			
		}
		if(flag2)music();//闹铃响
	}
}
/*外部中断函数*/
void exter() interrupt 0
{	
	uchar c;
	c=read_ds(0x0c);
}





作者:QiangWaiQiang 发表于2013-5-29 0:12:18 原文链接
阅读:121 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>