万年历初始源程序.docx
《万年历初始源程序.docx》由会员分享,可在线阅读,更多相关《万年历初始源程序.docx(20页珍藏版)》请在冰豆网上搜索。
![万年历初始源程序.docx](https://file1.bdocx.com/fileroot1/2022-11/26/944895e3-1994-4f65-b4f6-b689d37522bc/944895e3-1994-4f65-b4f6-b689d37522bc1.gif)
万年历初始源程序
12864、DS1302制作的电子万年历
代码如下:
/********************************************************************
名称:
电子万年历
功能:
自动计时100年,节日、时间提醒
作者:
甘春生
实验板:
KX-1N
/********************************************************************
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitRS=P2^0;
sbitRW=P2^1;
sbitEN=P2^2;
sbitFUN=P1^0;
sbitOK=P1^1;
sbitINC=P1^2;
sbitDEC=P1^3;
sbitRST=P2^5;
sbitIO=P2^6;
sbitSCLK=P2^7;
ucharhour=10;
ucharmin=18;
ucharsec=30;
ucharyear=10;
ucharmon=1;
uchardate=20;
ucharday=3;
ucharcount=0;
ucharflag=0;
ucharnum=0;
uchari=6;
voiddelay(uinti)
{
uintj;
for(;i>0;i--)
for(j=0;j<125;j++);
}
voidWrite_Ds1302_Byte(uchartemp)
{
uchari;
for(i=0;i<8;i++)
{
SCLK=0;
IO=temp&0x01;//只传最后一位
temp>>=1;
SCL=1;
}
}
voidWrite_Ds1302(ucharaddress,uchardat)
{
RST=0;
SCLK=0;
RST=1;
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
SCLK=1;
RST=0;
}
ucharRead_Ds1302(ucharaddress)
{
uchari,temp=0x00;
RST=0;
SCLK=0;
RST=1;
Write_Ds1302_Byte(address);
for(i=0;i<8;i++)
{
temp>>=1;
SCLK=0;
if(IO)
temp=temp|0x80;
SCLK=1;
}
RST=0;
IO=0;
returntemp;
}
voidCheck_Busy(void)
{
RW=1;//RW=1为读
RS=0;//RS=0为指令
EN=1;
while(P2&0X80);
}
voidWrite_com(ucharcom)
{
Check_Busy();
RW=0;
RS=0;
P2=com;
EN=1;
delay
(1);
EN=0;
}
voidWrite_dat(uchardat)
{
Check_Busy();
RW=0;
RS=1;
P2=dat;
EN=1;
delay
(1);
EN=0;
}
voidLcd_Pos(ucharXpos,ucharYpos)
{
uchartmp;
Xpos&=0x0f;
Ypos&=0x03;
tmp=Xpos;
if(Ypos==0)
tmp|=0x80;
if(Ypos==1)
tmp|=0x90;
if(Ypos==2)
tmp|=0x88;
if(Ypos==3)
tmp|=0x98;
Write_com(tmp);
}
voidWrite_LCD_String(ucharx,uchary,uchar*s)
{
Lcd_Pos(x,y);
Lcd_Pos(x,y);
while(*s!
='\0')
{
Write_dat(*s);
s++;
delay
(1);
}
}
voidLCD_Rst(void)
{
Write_com(0x30);
Check_Busy();
Write_com(0x01);
Write_com(0x06);
Write_com(0x0c);
delay(5);
}
voidWrite_day(ucharSel)
{
switch(Sel)
{
case1:
Write_LCD_String(6,1,"一");break;
case2:
Write_LCD_String(6,1,"二");break;
case3:
Lcd_Pos(6,1);Write_dat(0xc8);Write_dat(0xfd);break;
case4:
Write_LCD_String(6,1,"四");break;
case5:
Write_LCD_String(6,1,"五");break;
case6:
Write_LCD_String(6,1,"六");break;
case7:
Write_LCD_String(6,1,"日");break;
default:
break;
}
}
voidUpdat_Time(void)
{
Lcd_Pos(3,0);Write_dat(0x30+(year/10));Write_dat(0x30+(year%10));
Lcd_Pos(0,1);Write_dat(0x30+(mon/10));Write_dat(0x30+(mon%10));
Lcd_Pos(2,1);Write_dat(0x30+(date/10));Write_dat(0x30+(date%10));
Lcd_Pos(0,2);Write_dat(0x30+(hour/10));Write_dat(0x30+(hour%10));
Lcd_Pos(2,2);Write_dat(0x30+(min/10));Write_dat(0x30+(min%10));
Lcd_Pos(4,2);Write_dat(0x30+(sec/10));Write_dat(0x30+(sec%10));
Write_day(day);
}
voidWrite_Time(void)
{
Write_Ds1302(0x8e,0x00);
Write_Ds1302(0x8c,(year/10)*16+year%10);
Write_Ds1302(0x88,(mon/10)*16+mon%10);
Write_Ds1302(0x86,(date/10)*16+date%10);
Write_Ds1302(0x84,(hour/10)*16+hour%10);
Write_Ds1302(0x82,(min/10)*16+min%10);
Write_Ds1302(0x80,(sec/10)*16+sec%10);
Write_Ds1302(0x8a,(day/10)*16+day%10);
Write_Ds1302(0x8e,0x80);
}
voidRead_Time(void)
{
year=(Read_Ds1302(0x8d)>>4)*10+Read_Ds1302(0x8d)%16;
mon=(Read_Ds1302(0x89)>>4)*10+Read_Ds1302(0x89)%16;
date=(Read_Ds1302(0x87)>>4)*10+Read_Ds1302(0x87)%16;
hour=(Read_Ds1302(0x85)>>4)*10+Read_Ds1302(0x85)%16;
min=(Read_Ds1302(0x83)>>4)*10+Read_Ds1302(0x83)%16;
sec=(Read_Ds1302(0x81)>>4)*10+Read_Ds1302(0x81)%16;
day=(Read_Ds1302(0x8b)>>4)*10+Read_Ds1302(0x8b)%16;
Updat_Time();
}
voidSet_Time(void)
{
if(FUN==0)
{
delay(10);
if(FUN==0)
{
while(!
FUN);
flag=1;
num++;
if(num==8)
num=1;
}
}
if(flag)
{
if(num==1)
{
Lcd_Pos(0,2);
Write_com(0x0d);
if(INC==0)
{
delay(10);
if(INC==0)
{
while(!
INC);
hour++;
if(hour==24)
hour=0;
Updat_Time();
}
}
if(DEC==0)
{
delay(10);
if(DEC==0)
{
while(!
DEC);
hour--;
if(hour==-1)
hour=23;
Updat_Time();
}
}
}
if(num==2)
{
Lcd_Pos(2,2);
Write_com(0x0d);
if(INC==0)
{
delay(10);
if(INC==0)
{
while(!
INC);
min++;
if(min==60)
min=0;
Updat_Time();
}
}
if(DEC==0)
{
delay(10);
if(DEC==0)
{
while(!
DEC);
min--;
if(min==-1)
min=59;
Updat_Time();
}
}
}
if(num==3)
{
Lcd_Pos(4,2);
Write_com(0x0d);
if(INC==0)
{
delay(10);
if(INC==0)
{
while(!
INC);
sec++;
if(sec==60)
sec=0;
Updat_Time();
}
}
if(DEC==0)
{
delay(10);
if(DEC==0)
{
while(!
DEC);
sec--;
if(sec==-1)
sec=59;
Updat_Time();
}
}
}
if(num==4)
{
Lcd_Pos(3,0);
Write_com(0x0d);
if(INC==0)
{
delay(10);
if(INC==0)
{
while(!
INC);
year++;
if(year==100)
year=0;
Updat_Time();
}
}
if(DEC==0)
{
delay(10);
if(DEC==0)
{
while(!
DEC);
year--;
if(year==-1)
sec=99;
Updat_Time();
}
}
}
if(num==5)
{
Lcd_Pos(0,1);
Write_com(0x0d);
if(INC==0)
{
delay(10);
if(INC==0)
{
while(!
INC);
mon++;
if(mon==13)
mon=1;
Updat_Time();
}
}
if(DEC==0)
{
delay(10);
if(DEC==0)
{
while(!
DEC);
mon--;
if(mon==0)
mon=12;
Updat_Time();
}
}
}
if(num==6)
{
Lcd_Pos(2,1);
Write_com(0x0d);
if(INC==0)
{
delay(10);
if(INC==0)
{
while(!
INC);
date++;
if(date==32)
date=1;
Updat_Time();
}
}
if(DEC==0)
{
delay(10);
if(DEC==0)
{
while(!
DEC);
date--;
if(date==0)
date=31;
Updat_Time();
}
}
}
if(num==7)
{
Lcd_Pos(6,1);
Write_com(0x0d);
if(INC==0)
{
delay(10);
if(INC==0)
{
while(!
INC);
day++;
if(day==8)
day=1;
Updat_Time();
}
}
if(DEC==0)
{
delay(10);
if(DEC==0)
{
while(!
DEC);
day--;
if(day==0)
day=7;
Updat_Time();
}
}
}
}
i