微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > DS1302控制代码

DS1302控制代码

时间:12-01 来源:互联网 点击:

seconds%10;

write_command(0x06);

write_command(0x80+0x46);

write_date(shi+48);

write_date(ge+48);

}

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

年份显示

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

void DisplayYear(uchar years)

{

uchar shi,ge;

shi=years/10;

ge=years%10;

write_command(0x06);

write_command(0x80+0x00);

write_date(shi+48);

write_date(ge+48);

write_date(-);

}

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

月份显示

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

void DisplayMonth(uchar month)

{

uchar shi,ge;

shi=month/10;

ge=month%10;

write_command(0x06);

write_command(0x80+0x03);

write_date(shi+48);

write_date(ge+48);

write_date(-);

}

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

日显示

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

void DisplayDay(uchar day)

{

uchar shi,ge;

shi=day/10;

ge=day%10;

write_command(0x06);

write_command(0x80+0x06);

write_date(shi+48);

write_date(ge+48);

}

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

礼拜显示

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

void DisplayWeek(uchar week)

{

uchar i;

i=week;

write_command(0x06);

write_command(0x80+0x09);

switch(i)

{

case 1:write_date(M);write_date(O);write_date(N);

break;

case 2:write_date(T);write_date(H);write_date(E);

break;

case 3:write_date(W);write_date(E);write_date(D);

break;

case 4:write_date(T);write_date(H);write_date(U);

break;

case 5:write_date(F);write_date(R);write_date(I);

break;

case 6:write_date(S);write_date(A);write_date(T);

break;

case 7:write_date(S);write_date(U);write_date(N);

break;

}

}

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

读取日期

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

void read_date()

{

uchar ReadValue;

ReadValue = read_1302(0x81);

seconds=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = read_1302(0x83);

minutes=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = read_1302(0x85);

hours=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = read_1302(0x87);

day=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = read_1302(0x89);

month=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = read_1302(0x8d);

year=((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue=read_1302(0x8b);

week=ReadValue&0x07;

DisplaySeconds(seconds);

DisplayMinutes(minutes);

DisplayHours(hours);

DisplayDay(day);

DisplayMonth(month);

DisplayYear(year);

DisplayWeek(week);

}

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

修改日期

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

void change()

{

uchar temp,nian,yue,ri,zhou,shi,fen,miao;

if(K1==0)

{

delay(5);

if(K1==0)//证明功能键确实按下

{

cnt++;//计数器,计算按下的次数

write_1302(0x8e,0x00);//关闭写保护

}

}

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

修改年份

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

if(cnt==1)

{

temp=read_1302(0x8d);

nian=((temp&0x70)>>4)*10+(temp&0x0F);

if(K2==0)

{

delay(5);

if(K2==0)//K2键实现加

{

while(!K2);

delay(5);

while(!K2);//确保K2键松开

nian++;

if(nian>=100)

{

nian=0;

}

write_1302(0x8e,0x00);

write_1302(0x8c,(nian/10)<4|(nian%10));

DisplayYear(nian);

write_1302(0x8e,0x80);

}

}

if(K3==0)

{

delay(5);

if(K3==0)//K3键实现减

{

while(!K3);

delay(5);

while(!K3);//确保K3键松开

nian--;

if(nian==-1)

{

nian=99;

}

write_1302(0x8e,0x00);

write_1302(0x8c,(nian/10)<4|(nian%10));

DisplayYear(nian);

write_1302(0x8e,0x80);

}

}

}

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

修改月份

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

if(cnt==2)

{

temp=read_1302(0x89);

yue=((temp&0x70)>>4)*10+(temp&0x0F);

if(K2==0)

{

delay(5);

if(K2==0)//K2键实现加

{

while(!K2);

delay(5);

while(!K2);//确保K2键松开

yue++;

if(yue>=12)

{

yue=1;

}

write_1302(0x8e,0x00);

write_1302(0x88,(yue/10)<4|(yue%10));

DisplayMonth(yue);

write_1302(0x8e,0x80);

}

}

if(K3==0)

{

delay(5);

if(K3==0)//K3键实现减

{

while(!K3);

delay(5);

while(!K3);//确保K3键松开

yue--;

if(yue==0)

{

yue=12;

}

write_1302(0x8e,0x00);

write_1302

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top