DS1302时钟显示代码-可以显示年月日时分秒和星期Word下载.doc
《DS1302时钟显示代码-可以显示年月日时分秒和星期Word下载.doc》由会员分享,可在线阅读,更多相关《DS1302时钟显示代码-可以显示年月日时分秒和星期Word下载.doc(8页珍藏版)》请在冰豆网上搜索。
//voidDisplay_LCD_String(ucharp,uchar*s);
uchartCount=0;
//一年中每个月的天数,2月的天数由年份决定
ucharMonthsDays[]={0,31,0,31,30,31,30,31,31,30,31,30,31};
uchar*WEEK[]={"
SUN"
"
MON"
TUS"
WEN"
THU"
FRI"
SAT"
};
//周日,周一到周六
ucharLCD_DSY_BUFFER1[]={"
Date00-00-00"
//LCD显示缓冲
ucharLCD_DSY_BUFFER2[]={"
00:
00:
00"
ucharDateTime[7];
//所读取的日期时间
charAdjust_Index=-1;
//当前调节的时间:
秒,分,时,日,
ucharChange_Flag[]="
-MHDM-Y"
;
//ucharRead_LCD_State();
//voidLCD_Busy_Wait();
//voidWrite_LCD_Data(uchardat);
//voidWrite_LCD_Command(ucharcmd);
//voidInit_LCD();
//voidSet_LCD_POS(ucharp);
voidDelayMS(uintx)
{
uchari;
while(x--)for(i=0;
i<
120;
i++);
}
ucharRead_LCD_State() //忙检查
ucharstate;
RS=0;
RW=1;
EN=1;
DelayMS
(1);
state=P0;
EN=0;
returnstate;
voidLCD_Busy_Wait() //等待
{
while((Read_LCD_State()&
0x08)==0x80);
DelayMS(5);
}
voidWrite_LCD_Data(uchardat)//写数据
LCD_Busy_Wait();
RS=1;
RW=0;
//EN=0;
P0=dat;
voidWrite_LCD_Command(ucharcmd)//写命令
P0=cmd;
voidInit_LCD() //LCD初始化
Write_LCD_Command(0x38);
DelayMS
(1);
Write_LCD_Command(0x01);
Write_LCD_Command(0x06);
Write_LCD_Command(0x0C);
// Display_LCD_String(0x00,"
zzuli"
);
// DelayMS(1000);
// Display_LCD_String(0x40,"
LCDCLOCK"
// DelayMS(2000);
voidSet_LCD_POS(ucharp) //写命令,设定
Write_LCD_Command(p|0x80);
voidDisplay_LCD_String(ucharp,uchar*s) //显示
uchari;
Set_LCD_POS(p);
for(i=0;
16;
i++)
{
Write_LCD_Data(s[i]);
DelayMS
(1);
}
voidDS1302_Write_Byte(ucharx) //向DS1302写入一字节
uchari;
for(i=0;
8;
{
SDA=x&
1;
CLK=1;
CLK=0;
x>
>
=1;
}
ucharDS1302_Read_Byte() //从DS1302读取一字节
uchari,b,t;
for(i=0;
b>
t=SDA;
b|=t<
<
7;
CLK=1;
CLK=0;
}
returnb/16*10+b%16;
ucharRead_Data(ucharaddr) //从DS1302指定位置数据
uchardat;
RST=0;
RST=1;
DS1302_Write_Byte(addr);
dat=DS1302_Read_Byte();
returndat;
voidWrite_DS1302(ucharaddr,uchardat) //向DS1302某地址写入数据
DS1302_Write_Byte(dat);
voidSET_DS1302()//设置时间
uchari;
Write_DS1302(0x8e,0x00);
Write_DS1302(0x80+2*i,(DateTime[i]/10<
4|(DateTime[i]%10)));
Write_DS1302(0x8e,0x80);
voidGetTime()//读取当前时期时间
{
DateTime[i]=Read_Data(0x81+2*i);
voidFormat_DateTime(uchard,uchar*a) //时间和日期转换成数字字符
a[0]=d/10+'
0'
a[1]=d%10+'
ucharisLeapYear(uinty)//判断是否为闰年
return(y%4==0&
&
y%100!
=0)||(y%400==0);
voidRefreshWeekDay() //星期转换
uinti,d,w=5;
for(i=2000;
2000+DateTime[6];
d=isLeapYear(i)?
366:
365;
w=(w+d)%7;
d=0;
for(i=1;
DateTime[4];
i++)d+=MonthsDays[i];
d+=DateTime[3];
DateTime[5]=(w+d)%7+1;
voidDateTime_Adjust(charx) //年月日时分秒++/--
switch(Adjust_Index)
case6:
//年
if(x==1&
DateTime[5]<
99)DateTime[6]++;
if(x==-1&
DateTime[6]>
0)DateTime[6]--;
MonthsDays[2]=isLeapYear(2000+DateTime[6])?
29:
28;
if(DateTime[3]>
MonthsDays[DateTime[4]])
DateTime[3]=MonthsDays[DateTime[4]];
RefreshWeekDay();
break;
case4:
//月
DateTime[4]<
12)DateTime[4]++;
DateTime[4]>
1)DateTime[4]--;
DateTime[3]=MonthsDays[DateTime[4]];
case3:
//日
DateTime[3]<
MonthsDays[DateTime[4]])DateTime[3]++;
DateTime[3]>
0)DateTime[3]--;
case2:
//时
DateTime[2]<
23)DateTime[2]++;
DateTime[2]>
0)DateTime[2]--;
case1:
//秒
DateTime[1]<
59)DateTime[1]++;
DateTime[1]>
0)DateTime[1]--;
voidT0_INT()interrupt1 //定时器每秒刷新LCD显示
TH0=-50000/256;
TL0=-50000%256;
if(++tCount!
=2)return;
tCount=0;
Format_DateTime(DateTime[6],LCD_DSY_BUFFER1+5);
Format_DateTime(DateTime[4],LCD_DSY_BUFFER1+8);
Format_DateTime(DateTime[3],LCD_DSY_BUFFER1+11);
strcpy(LCD_DSY_BUFFER1+13,WEEK[DateTime[5]-1]);
Format_DateTime(DateTime[2],LCD_DSY_BUFFER2+5);
Format_DateTime(DateTime[1],LCD_DSY_BUFFER2+8);
Format_DateTime(DateTime[0],LCD_DSY_BUFFER2+11);
Display_LCD_String(0x00