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

📄 key.c

📁 万年历系统
💻 C
字号:
#include <reg51.h>
#include <absacc.h>
#include <intrins.h>
#include "lcd.h"
#include "ds1302.h"
#include "lcdshow.h"
#include "key.h"

bit CLKSTOP;
uchar data count,temp,up_flag,down_flag,done;
uint data year;

/*********************************************/
sbit Set = P3^1;       //模式切换键
sbit Up = P3^2;        //加法按钮
sbit Down = P3^3;      //减法按钮
sbit out = P3^4;       //立刻跳出调整模式按钮
/*********************************************/
void mdelay(uint delay)
{	uint i;
 	for(;delay>0;delay--)
   		{for(i=0;i<62;i++) //1ms延时.
       		{;}
   		}
}




/*********************************************/

void setting()	//选中后反白显示
{  
 	uchar i;
	switch(count)
	{
	case 1:   //year
	r_ShowNumber(0,24,date_buf1[0]);
	r_ShowNumber(0,32,date_buf1[1]);
	i = date_buf[6]>>4;
	i &= 0x0f;
	r_ShowNumber(0,40,i);
    i = date_buf[6] & 0x0f;
	r_ShowNumber(0,48,i);
    break;

	case 2:	 //month
	show_date();
	i = date_buf[4]>>4;
	i &= 0x01;
	r_ShowNumber(0,64,i);
	i = date_buf[4] & 0x0f;
	r_ShowNumber(0,72,i);    
	break;

	case 3://day
	show_date();
	i = date_buf[3]>>4;
 	i &= 0x03;
	r_ShowNumber(0,88,i);
	i = date_buf[3] & 0x0f;
	r_ShowNumber(0,96,i);
	break;

	case 4://week
	i=date_buf[5]&0x0f;
	r_ShowChina(4,72,i);
    break;


	case 5://hour
	show_date();
	i = date_buf[2]>>4;
	i &= 0x03;
	r_ShowNumber(2,16,i);
	i = date_buf[2] & 0x0f;
	r_ShowNumber(2,24,i);
	break;

	case 6://min
	show_time();
	i = date_buf[1]>>4;
	i &= 0x07;
	r_ShowNumber(2,48,i);
	i = date_buf[1] & 0x0f;
	r_ShowNumber(2,56,i);
    break;


	case 7://sec
	show_time();
	i = date_buf[0]>>4;
 	i &= 0x07;
	r_ShowNumber(2,80,i);
	i = date_buf[0] & 0x0f;
	r_ShowNumber(2,88,i);
    break;

	default:
	show_date();
	show_time();
	break;
	}


}
void Setkey()
{
 	Set=1;
		if(Set==0)
	    { 
		   mdelay(8);
           count=count+1;	 //Setkey按一次,count就加1
		   done=1;			 //进入调整模式
		   setting();
           while(Set==0);	//等待按键释放
		 }
}

void outkey()                    //跳出调整模式,返回默认显示
{ uchar Second;
  if(out==0)         
  { count=0;
	Second=read_all_1302(DS1302_SECOND);
    write_all_1302(0x8e,0x00); //写入允许
	write_all_1302(0x80,Second&0x7f);
	write_all_1302(0x8E,0x80);          //禁止写入
	done=0;
	ClearScreen(1);
    ClearScreen(2);
    mdelay(2);
	show_date();
	show_time();
	show_hz();
	show_temp();    
	while(out==0);
	//TR0=1;
  }
}
/***************************************************************************************************/

void addtime()
{			 Up=1;
			if(Up==0)
			{	    mdelay(8);
				       switch(count)
				          {
				           case 1:
                                  temp=read_all_1302(DS1302_YEAR);  //读取年数
								  temp=temp+1;  //年数加1
								  if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
								 
								  up_flag=1;
								  if(temp>0x85)
								  temp=0;
								  break;
				           case 2:
                                  temp=read_all_1302(DS1302_MONTH);  //读取月数
								  temp=temp+1;  //月数加1
								  if (temp==0x0a) temp=0x10;
                                  up_flag=1;
								  if(temp>0x12)
								  temp=1;
								  break;
				           case 3:
                                  temp=read_all_1302(DS1302_DAY);  //读取日数
								  temp=temp+1;  //日数加1
								  if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
                                  up_flag=1;
								  if(temp>0x31)
								  temp=1;
								  break;
				           case 4:
                                  temp=read_all_1302(DS1302_WEEK);  //读取星期数
								  temp=temp+1;  //星期数加1
								  if (temp==0x0a) temp=0x10;
                                  up_flag=1;
								  if(temp>0x7)  
								  temp=1;
								  break;
				           case 5:
                                  temp=read_all_1302(DS1302_HOUR);  //读取小时数
								  temp=temp+1;  //小时数加1
								  if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
                                  up_flag=1;
								  if(temp>0x23)   //超过23小时,清零
								  temp=0;
								  break;
				           case 6:
                                  temp=read_all_1302(DS1302_MINUTE);  //读取分数
								  temp=temp+1;  //分数加1
								  if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
                                  up_flag=1;
								  if(temp>0x59)          //超过59分,清零
								  temp=0;
								  break;
                           case 7:
                                  temp=read_all_1302(DS1302_SECOND);  //读取秒数
								  temp=temp+1;  //秒数加1
								  if((temp&0x0f)==0x0a) temp=(((temp>>4)&0x0f)+1)*16;
                                  up_flag=1;    //数据调整后更新标志
								  if((temp&0x7f)>0x59)   //超过59秒,清零
                                  temp=0;								  
								  break;
					       default:break;
						   }
			  while(Up==0);
		  }
// flag=1;
}

/***************************************************************************************************/


void subtime()
{		 Down=1;
			if(Down==0)
			{	   mdelay(8);
				     switch(count)
				          {
				          case 1:
                                  temp=read_all_1302(DS1302_YEAR);  //读取年数
								  temp=temp-1;  //年数减1
			     				  if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=8)) temp=temp&0x0f0+0x09;
							      down_flag=1;
								  if(temp==-1)
								  temp=0x85;
								  break;
				           case 2:
                                  temp=read_all_1302(DS1302_MONTH);  //读取月数
								  temp=temp-1;  //月数减1
								  if(temp==0x0f) temp=0x09;
                                  down_flag=1;
								  if(temp==0)
								  temp=0x12;
								  break;
				           case 3:
                                  temp=read_all_1302(DS1302_DAY);  //读取日数
								  temp=temp-1;  //日数减1
								  if((temp&0x0f)==0x0f) temp=(((temp>>4)&0x0f))*16+9;
                                  down_flag=1;
								  if(temp==0)
								  temp=0x31;
								  break;
				           case 4:
                                  temp=read_all_1302(DS1302_WEEK);  //读取星期数
								  temp=temp-1;  //星期数减1
								  //if (temp==0x0f) temp=0x09;
                                  down_flag=1;
								  if(temp==0)
								  temp=0x7;
								  break;
				           case 5:
                                  temp=read_all_1302(DS1302_HOUR);  //读取小时数
								  temp=temp-1;  //小时数减1
								  if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=2)) temp=(((temp>>4)&0x0f)-1)*16+9;
                                  down_flag=1;
								  if(temp==-1)
								  temp=0x23;
								  break;
    			           case 6:
                                  temp=read_all_1302(DS1302_MINUTE);  //读取分数
								  temp=temp-1;  //分数减1
								  if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=5)) temp=(((temp>>4)&0x0f))*16+9;
                                  down_flag=1;
								  if(temp==-1)
								  temp=0x59;      //小于0秒,返回59秒
								  break;
						  case 7:
                                  temp=read_all_1302(DS1302_SECOND);  //读取秒数
								  temp=temp-1;						    //秒数减1
								  if(((temp&0x0f)==0x0f)&&(((temp>>4)&0x0f)>=0&&((temp>>4)&0x0f)<=5)) temp=(((temp>>4)&0x0f))*16+9;
                                  down_flag=1;       //数据调整后更新标志
								  if(temp==0x7f)     //小于0秒,返回59秒
								  temp=0x59;
								  break;
					      default:break;
				         }
			 while(Down==0);
			 }
// flag1=1;
}
/*****************************************************************************************/
void Deal_With_Time()
{	
		 uchar second;
         if(CLKSTOP==0)    //关闭时钟,停止计时
         { write_all_1302(0x8e,0x00); //写入允许
           temp=read_all_1302(0x80);
           write_all_1302(0x80,temp|0x80);
	       write_all_1302(0x8e,0x80); //禁止写入
           CLKSTOP=1;
         }
		 Setkey();
  		 switch(count)
		 { 
		  case 1:do					        //count=7,调整年
		          {
 				   outkey();
				   addtime();
				   subtime();
				   if(up_flag==1||down_flag==1)
				   {
				   write_all_1302(0x8e,0x00); //写入允许
				   write_all_1302(0x8c,temp); //写入新的年数
				   write_all_1302(0x8e,0x80); //禁止写入
				   up_flag=0;
				   down_flag=0;
				   }
				  setting();
				  }while(count==2);break;
		  case 2:do					        //count=6,调整月
		          {
				   outkey();
				   addtime();
				   subtime();
				   if(up_flag==1||down_flag==1)
				   {
				   write_all_1302(0x8e,0x00); //写入允许
				   write_all_1302(0x88,temp); //写入新的月数
				   write_all_1302(0x8e,0x80); //禁止写入
				   up_flag=0;
				   down_flag=0;
				   }
				   setting();
				  }while(count==3);break;
		  case 3:do					        //count=5,调整日
		          {
				   outkey();
				   addtime();
				   subtime();
				   if(up_flag==1||down_flag==1)
				   {
				   write_all_1302(0x8e,0x00); //写入允许
				   write_all_1302(0x86,temp); //写入新的日数
				   write_all_1302(0x8e,0x80); //禁止写入
				   up_flag=0;
				   down_flag=0;
				   }
				   setting();
				  }while(count==4);break;
		  case 4:do					        //count=4,调整星期
		          {
				   outkey();
				   addtime();
				   subtime();
				   if(up_flag==1||down_flag==1)
				   {
				   write_all_1302(0x8e,0x00); //写入允许
				   write_all_1302(0x8a,temp); //写入新的星期数
				   write_all_1302(0x8e,0x80); //禁止写入
				   up_flag=0;
				   down_flag=0;
				   }
				   setting();
				  }while(count==5);break;
		  case 5:do					        //count=3,调整小时
		          {
				   outkey();
				   addtime();
				   subtime();
				   if(up_flag==1||down_flag==1)
				   {
				   write_all_1302(0x8e,0x00); //写入允许
				   write_all_1302(0x84,temp); //写入新的小时数
				   write_all_1302(0x8e,0x80); //禁止写入
				   up_flag=0;
				   down_flag=0;
				   }
				   setting();
				  }while(count==6);break;
		  case 6:do					        //count=2,调整分
		          {
				   outkey();
				   addtime();
				   subtime();
				   if(temp>0x60)
				     temp=0;
				   if(up_flag==1||down_flag==1)
				   {
				   write_all_1302(0x8e,0x00); //写入允许
				   write_all_1302(0x82,temp); //写入新的分数
				   write_all_1302(0x8e,0x80); //禁止写入
				   up_flag=0;
				   down_flag=0;
				   }
				   setting();
				  }while(count==7);break;
          case 7:do					        //count=1,调整秒
		          {
                   outkey();
				   addtime();
				   subtime();
				   if(up_flag==1||down_flag==1)  //数据更新,重新写入新的数据
				   {
				   write_all_1302(0x8e,0x00); //写入允许
				   write_all_1302(0x80,temp|0x80); //写入新的秒数
				   write_all_1302(0x8e,0x80); //禁止写入
				   up_flag=0;
				   down_flag=0;
				   }
				   setting();
                 }while(count==8);break;				  				  				  
		  case 8: count=0;  //count8, 跳出调整模式,返回默认显示状态
	              second=read_all_1302(DS1302_SECOND);
                  write_all_1302(0x8e,0x00); //写入允许
	              write_all_1302(0x80,second&0x7f);
	              write_all_1302(0x8E,0x80);          //禁止写入
				  // ET0=1;
				  break; //count=7,开启中断,标志位置0并退出
		  default:break;

		 }
}


		 

⌨️ 快捷键说明

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