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

📄 main.c

📁 51写的一个定时时钟
💻 C
字号:
#include<reg52.h>
#include<LCD12864.H>

/****定义按键接口**************/
sbit Mode=P2^6;
sbit Add=P2^7;
sbit Dec=P1^5;
sbit Ent=P1^6;
sbit led1=P1^1; 
sbit led2=P1^0;
sbit led3=P1^4;
sbit SPK=P1^3;

unsigned char code para_month[13]={0,0,3,3,6,1,4,6,2,5,0,3,5};	//星期月参变数

unsigned char cnt,counter,counter1;
 
unsigned char id,id1,word;

void Add_key1();
void Set_time(unsigned char sel,bit sel_1);
void Add_key2();
void Set_bell(unsigned char sel,bit sel_1);
bit leap_year();
unsigned char week_proc();

extern void Write_24c04( unsigned char add,unsigned char dat);
extern unsigned char Read_24C04(unsigned char add);

/*******************按键加一减一程序*******************/
void Add_key1()
  {
    if(Add==0)
	{
	  Delay12864(20);
	  if(Add==0)
	  { 
	    Set_time(id,0);
		while(Add==0);
		}
	  }
    if(Dec==0)
	{
	   Delay12864(20);
	   if(Dec==0)
	   {
	     Set_time(id,1);
		while(Dec==0);
		}
	  }
	}

 /***************设定时间**********/
void Set_time(unsigned char sel, bit sel_1) //根据选择调整相应项目并写入DS1302
{
  if(sel==1){
       if(sel_1==0){year++;if(year>99)year=0;}
	    else { year--;if(year==0)year=99;}}
  if(sel==2){
       if(sel_1==0){month++;  if(month>12)month=1;}
       else {month--;if(month==0)month=12;}}
   if(sel==3){
       if(sel_1==0){date++;if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
					if (date>31) date=1;			//大月31天
				if (month==4||month==6||month==9||month==11)		
				 	if (date>30) date=1;			//小月30天
				if (month==2)			
					{if(leap_year())				//闰年的条件
						{if (date>29) date=1;}		//闰年2月为29天
					 else
						{if (date>28) date=1;}}
						}		//平年2月为28天
	else {date--;if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
					if (date==0) date=31;			//大月31天
				if (month==4||month==6||month==9||month==11)		
				 	if (date==1) date=30;			//小月30天
				if (month==2)			
					{if(leap_year())				//闰年的条件
						{if (date==0) date=29;}		//闰年2月为29天
					 else
						{if (date==0) date=28;}}	//平年2月为28天
						   }
						}
	 if(sel==4){
	        if(sel_1==0){week++;if(week>7)week=1;}
			else{week--;if(week==0)week=7;}}
	if(sel==5){
	        if(sel_1==0){hour++;if(hour>23)hour=0;}
			else{hour--;if(hour==0)hour=23;}}
	if(sel==6){
	        if(sel_1==0){min++;if(min>59)hour=0;}
			else{min--;if(min==0)min=59;}}
	if(sel==7){
	        if(sel_1==0){sec++;if(hour>59)sec=0;}
			else{sec--;if(sec==0)sec=59;}}
 // write_clock(0x8e,0x00);//允许写操作
 // write_clock(address,(item/10)*16+item%10);//转换成16进制写入1302
 // write_clock(0x8e,0x80);//写保护,禁止写操作 
  } 
/**************设定脑铃*******************/
/*******************按键加一减一程序*******************/
void Add_key2()
  {
    if(Add==0)
	{
	  Delay12864(20);
	  if(Add==0)
	  { 
	    Set_bell(id1,0);
		while(Add==0);
		}
	  }
    if(Dec==0)
	{
	   Delay12864(20);
	   if(Dec==0)
	   {
	     Set_bell(id1,1);
		while(Dec==0);
		}
	  }
	}

 /***************设定时间**********/
void Set_Bell(unsigned char sel, bit sel_1)//根据选择调整相应项目并写入24 
{
  signed char address,item;
  signed char max,mini;
  if(sel==7)  {address=0x0e; max=59;mini=0;}    //秒
  if(sel==6)  {address=0x0d; max=59;mini=0;}    //分
  if(sel==5)  {address=0x0c; max=23; mini=0;}   //时
  if(sel==4)  {address=0x0b; max=59;mini=0;}    //秒
  if(sel==3)  {address=0x0a; max=59;mini=0;}    //分
  if(sel==2)  {address=0x09; max=23; mini=0;}   //时
  if(sel==1)  
  {
  //	address=0x08;
	if(Add ==0 || Dec ==0)
	Bell_Swh=~Bell_Swh;
	Write_24c04(0x08,Bell_Swh);
  }    //开关

  item = Read_24C04(address);
  if(sel_1==0) 
  	item++;  
  else
  	item--;
  if(item>max) item=mini;   
  if(item<mini) item=max;

  Write_24c04(address,item);//写入24
}

  //闰年的计算
bit leap_year()
{
	bit leap;
	if((year%4==0&&year%100!=0)||year%400==0)//闰年的条件
		leap=1;
	else
		leap=0;
	return leap;
}

//星期的自动运算和处理
unsigned char week_proc()
{	unsigned char num_leap;	
	unsigned char c;
	num_leap=year/4-year/100+year/400;//自00年起到year所经历的闰年数
	if( leap_year()&& month<=2 )	  //既是闰年且是1月和2月	
		c=5;
	else 
		c=6;
	week=(year+para_month[month]+date+num_leap+c)%7;//计算对应的星期
	return week;
}
/******************做日期运算*****************/
//时间和日期处理程序
void pro_timedate()
{	
	sec++;
	if(sec > 59)
		{sec = 0;
		 min++;
		 if(min>59)
			{min=0;
			 hour++;
			 if(hour>23)
				{hour=0;
				 date++;
				 if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
					 if (date>31) {date=1;month++;}					//大月31天
				 if (month==4||month==6||month==9||month==11)		
				 	 if (date>30) {date=1;month++;}					//小月30天
				 if (month==2)			
					 {if( leap_year())								//闰年的条件
						{if (date>29) {date=1;month++;}}			//闰年2月为29天
					 else
						{if (date>28) {date=1;month++;}}			//平年2月为28天	
					 }				
				 if (month>12) {month=1;year++;}
				 if (year>99) year=0;
				}
			}
		}
	week_proc();
	 Write_24c04( 0x01,0);
 Write_24c04( 0x02,year);
 Write_24c04( 0x03,month);
 Write_24c04( 0x04,date);
 Write_24c04( 0x05,hour);
 Write_24c04( 0x06,min);
 Write_24c04( 0x07,sec);
				//闹钟启用时,报警时间到,启动Timer1
}
/********定时器0。做时钟用*****************/
void timer0() interrupt 1 using 0
{
  TH0=0x3c;
  TL0=0xb0;
  cnt++;
//	if((word==1)&&(Bell_Swh == 1))
//	{
//	  TR1=0;
//	  }
  if(cnt==20)
   {
     led1^=1;
     cnt=0;
    pro_timedate();
    }
  }
/********定时器0。做时钟用*****************/
void timer1() interrupt 3 using 0
{
	TH1=0x3c;				
	TL1=0xb0;
	counter1++;
	counter++;
 //  if(Bell_Swh == 1)counter1++;//闹钟为

⌨️ 快捷键说明

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