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

📄 12887.c

📁 ds12887 时钟芯片 源代码 51单片机源程序
💻 C
字号:
/*************************
DS12877A管脚配置:
MOT: 接地
CS:  接地  地址:0xFF00
AS:  接单片机 ALE
R/W: 接 RW
DS:     接 RD
RESET:  接高
IRQ: 空
SQW:    空
**************************/

//包含头文件
#include <reg52.h>
#include <absacc.h>

//定义定时器0重装常数
#define RELOAD_H 0xfd;
#define RELOAD_L 0x78;

//DS12877需要的常量
#define REG_A      XBYTE[0xFF0a] 
#define REG_B      XBYTE[0xFF0b]
#define REG_C      XBYTE[0xFF0c]
#define REG_D      XBYTE[0xFF0d]

//显示需要的常量
#define REG0 T1
#define REG1 T0
#define REG2 INT1
#define REG3 INT0
#define REG4 TXD
#define REG5 RXD

sbit KEYA=P1^6;
sbit KEYB=P1^7;

//定义变量
unsigned char i,k,m;
unsigned int j;

unsigned char calendar_time[7]; 	/* 05/8/16 18:58:00 星期二*/    
code unsigned char at[7]={9,8,7,4,2,0,6};   			/* 年、月、日、时、分、秒 、星期*/
unsigned char xdata *calendar_address=0xff00;
unsigned char disp_buf[6];
bit time5s,time10ms,time100ms;

//定义函数
void sys_init(void);
void Read_calendar(void);
void Start_calendar(void);
//void Set_calendar(void);

void chk_key(void);

void time2buf(void);
void day2buf(void);

bit chkeya(void);
bit chkeyb(void);

//主程序
main()
{
	sys_init();
	
	while(1){
		Read_calendar();
		time2buf();
		chk_key();
	}
}

//初始化函数       
void sys_init(void){
	EA=1;				//总中断允许
	ET0=1;				//定时器0中断允许
	TMOD=0x01;			//定时器0工作于定时方式
	TH0=RELOAD_H;
	TL0=RELOAD_L;
	TR0=1;
	Start_calendar();
}

//显示中断函数
void disp(void) interrupt 1{
	TH0=RELOAD_H;
	TL0=RELOAD_L;

	P3=P3|0x3f;		//关闭所有显示
	P1=(disp_buf[i]&0x0f)|0xf0;
	if(i==0){REG0=0;}
	else if(i==1){REG1=0;}
	else if(i==2){REG2=0;}
	else if(i==3){REG3=0;}
	else if(i==4){REG4=0;}
	else if(i==5){REG5=0;}
	i++;
	if(i>=6){i=0;}
	j++;
	if(j>=5000){
		time5s=1;
		j=0;
	}
	k++;
	if(k>=10){
		time10ms=1;
		k=0;
	}
	m++;
	if(m>=225){
		time100ms=1;
	 	m=0;
	}
}

void time2buf(void){
	unsigned char i;
	for(i=0;i<6;i=i+2){
		disp_buf[i]=calendar_time[i/2+3]/10;
		disp_buf[i+1]=calendar_time[i/2+3]%10;
	}
}

void day2buf(void){
	unsigned char i;
	for(i=0;i<6;i=i+2){
		disp_buf[i]=calendar_time[i/2]/10;
		disp_buf[i+1]=calendar_time[i/2]%10;
	}
}

//启动时钟
void Start_calendar(void) 
{
	REG_A=0x20;
	REG_B=0x06;
}

//读取系统时间
void Read_calendar(void) 
{
 	unsigned char  temp,i;  
 	REG_B=0x06;
 	do{ temp=REG_A;} 
 		while(temp&0x80);
 	for(i=0;i<7;i++)
		calendar_time[i]=*(calendar_address+at[i]);    
}

/*
//设置系统时间
void Set_calendar(void) 
{
 	unsigned char i;
 	REG_B=0x80; 
	for(i=0;i<9;i++)
 	*(calendar_address+at[i])=calendar_time[i];
}
*/

//查键
void chk_key(void){
	if(chkeya()){						//如果按了SET键
		while(!time5s){				//5秒内
			Read_calendar();
			disp_buf[2]=0x0f;
			disp_buf[3]=0x0f;
			disp_buf[4]=0x0f;
			disp_buf[5]=0x0f;
			disp_buf[0]=calendar_time[0]/10;
			disp_buf[1]=calendar_time[0]%10;
			if(chkeyb()){
				calendar_time[0]++;
				if(calendar_time[0]>=100){
					calendar_time[0]=0;
				}
				*(calendar_address+at[0])=calendar_time[0];
			}	
			if(chkeya()){				//第2次按了SET键
				while(!time5s){		//5秒内
					Read_calendar();
					disp_buf[0]=0x0f;
					disp_buf[1]=0x0f;
					disp_buf[4]=0x0f;
					disp_buf[5]=0x0f;
					disp_buf[2]=calendar_time[1]/10;
					disp_buf[3]=calendar_time[1]%10;
					if(chkeyb()){
						calendar_time[1]++;
						if(calendar_time[1]>=13){
							calendar_time[1]=1;
						}
						*(calendar_address+at[1])=calendar_time[1];
					}
					if(chkeya()){		//第3次
						while(!time5s){
							Read_calendar();
							disp_buf[0]=0x0f;
							disp_buf[1]=0x0f;
							disp_buf[2]=0x0f;
							disp_buf[3]=0x0f;
							disp_buf[4]=calendar_time[2]/10;
							disp_buf[5]=calendar_time[2]%10;
							if(chkeyb()){
								calendar_time[2]++;

								
								if(									//设置日期时,根据不同月份、年,自动调整最大日期整
									(
										(
											(calendar_time[1]==1)||
											(calendar_time[1]==3)||
											(calendar_time[1]==5)||
											(calendar_time[1]==7)||
											(calendar_time[1]==8)||
											(calendar_time[1]==10)||
											(calendar_time[1]==12)
										)
										&&
										(calendar_time[2]>=32)
									)
									||
									(
										(
											(calendar_time[1]==4)||
											(calendar_time[1]==6)||
											(calendar_time[1]==9)||
											(calendar_time[1]==11)
										)
										&&
										(calendar_time[2]>=31)
									)
									||
									(
										(calendar_time[1]==2)&&
										(calendar_time[0]%4==0)&&
										(calendar_time[2]>=30)
									)
									||
									(
										(calendar_time[1]==2)&&
										(calendar_time[0]%4!=0)&&
										(calendar_time[2]>=29)
									)
								)

								{
									calendar_time[2]=1;
								}
								
								
								
								*(calendar_address+at[2])=calendar_time[2];
							}
							if(chkeya()){						//第4次
								while(!time5s){
									Read_calendar();
									disp_buf[2]=0x0f;
									disp_buf[3]=0x0f;
									disp_buf[4]=0x0f;
									disp_buf[5]=0x0f;
									disp_buf[0]=calendar_time[3]/10;
									disp_buf[1]=calendar_time[3]%10;
									if(chkeyb()){
										calendar_time[3]++;
										if(calendar_time[3]>=24){
											calendar_time[3]=0;
										}
										*(calendar_address+at[3])=calendar_time[3];
									}
									if(chkeya()){				//第5次
										while(!time5s){
											Read_calendar();
											disp_buf[0]=0x0f;
											disp_buf[1]=0x0f;
											disp_buf[4]=0x0f;
											disp_buf[5]=0x0f;
											disp_buf[2]=calendar_time[4]/10;
											disp_buf[3]=calendar_time[4]%10;
											if(chkeyb()){
												calendar_time[4]++;
												if(calendar_time[4]>=60){
													calendar_time[4]=0;
												}
												*(calendar_address+at[4])=calendar_time[4];
											}
											if(chkeya()){			//第6次
												while(!time5s){
													Read_calendar();
													disp_buf[0]=0x0f;
													disp_buf[1]=0x0f;
													disp_buf[2]=0x0f;
													disp_buf[3]=0x0f;
													disp_buf[4]=calendar_time[5]/10;
													disp_buf[5]=calendar_time[5]%10;
													if(chkeyb()){
														calendar_time[5]++;
														if(calendar_time[5]>=60){
															calendar_time[5]=0;
														}
														*(calendar_address+at[5])=calendar_time[5];
													}
													if(chkeya()){
														return;
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if(chkeyb()){
		while(!time5s){
			day2buf();
			if(chkeyb()){
				return;
			}
		}
	}
}

bit chkeya(void){
	if(!KEYA){
		time10ms=0;
		k=0;
		while(!time10ms);
		if(!KEYA){
			while(!KEYA);
			time5s=0;
			j=0;
			return(1);
		}
	}
	return(0);
}

bit chkeyb(void){
	if(!KEYB){
		time10ms=0;
		k=0;
		while(!time10ms);
		if(!KEYB){
			m=0;
			time100ms=0;
			while(!time100ms);
//			while(!KEYB);
			time5s=0;
			j=0;
			return(1);
		}
	}
	return(0);
}

⌨️ 快捷键说明

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