新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 用DS1302做的可調(diào)時(shí)鐘

用DS1302做的可調(diào)時(shí)鐘

作者: 時(shí)間:2012-09-05 來(lái)源:網(wǎng)絡(luò) 收藏

///////////////////////////////////////////////////////////////////
/*名稱:?jiǎn)纹瑱C(jī)讀取芯片,帶調(diào)整功能 /
/* /
///////////////////////////////////////////////////////////////////*/
#include reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define xiemiao 0x80
#define xiefen 0x82
#define xieshi 0x84
#define xieri 0x86
#define xieyue 0x88
#define xiezhou 0x8a
#define xienian 0x8c
#define dumiao 0x81
#define dufen 0x83
#define dushi 0x85
#define duri 0x87
#define duyue 0x89
#define duzhou 0x8b
#define dunian 0x8d
#define kongzhi 0x8e

本文引用地址:http://butianyuan.cn/article/170910.htm

uchar rem[8]={0,3,10,5,1,10,5,1};
uchar bcd[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar HSHI,LSHI,moshi,shanjishu;
uint j;


bit SR_1,S_1,aj_1,shan_1;

sbit CS1=P1^0; ////顯示器選擇線
sbit CS2=P1^1;
sbit E=P1^2;

sbit SCLK=P2^0; ////管腳定義
sbit DIO=P2^1;
sbit DCE=P2^2;

sbit SB1=P2^7; ////按鍵定義:時(shí)日選擇鍵
sbit SB2=P2^6; ////調(diào)整模式鍵
sbit SB3=P2^5; ////加加鍵
sbit SB4=P2^4; ////減減鍵

sbit ACC_7=ACC^7;

/////////////////////////////
yanshi(uint kk)
{
while(kk--);
}
///////////////////////顯示
xianshi()
{
uchar i;
for(i=0;i8;i++)
{
if(shan_1)if(i==moshi)i=i+2;
P0=~(0x01i);
E=0;
CS2=0;
E=1;
CS2=1;

P0=bcd[rem[i]];
E=0;
CS1=0;
E=1;
CS1=1;
yanshi(10);

P0=0xff;
E=0;
CS1=0;
E=1;
CS1=1;
}
shanjishu++;
if(shanjishu==200)
{
shanjishu=0;
shan_1=~shan_1;
}
}
////////////////////////////向寫數(shù)據(jù)
xieds1302(uchar dizhi,shuju)
{uchar i,tem;
DCE=0;
SCLK=0;
DCE=1;
for(i=0;i8;i++) //地址傳輸
{
SCLK=0;
tem=dizhi;
DIO=(bit)(tem0x01);
dizhi>>=1;
SCLK=1;
}
for(i=0;i8;i++) ///數(shù)據(jù)傳送
{
SCLK=0;
tem=shuju;
DIO=(bit)(tem0x01);
shuju>>=1;
SCLK=1;
}
DCE=0;
}
/////////////////////////////////////讀取DS1302的數(shù)據(jù)
duds1302(uchar dizhi)
{
uchar i,tem,temp;
temp=0;
DCE=0;
SCLK=0;
DCE=1;
for(i=0;i8;i++) ////傳送地址
{
SCLK=0;
tem=dizhi;
DIO=(bit)(tem0x01);
dizhi>>=1;
SCLK=1;
}
for(i=0;i8;i++) ////讀取數(shù)據(jù)
{
ACC_7=DIO;
SCLK=0;
ACC>>=1;
SCLK=1;
}
DCE=0;
temp=ACC;
LSHI=temp0x0f;
HSHI=(temp0xf0)>>4;
}
/////////////////////////////////讀取時(shí)間
dushijian()
{
duds1302(dumiao); ///讀秒
rem[0]=LSHI;
rem[1]=HSHI;
duds1302(dufen); ///讀分
rem[3]=LSHI;
rem[4]=HSHI;
duds1302(dushi); ///讀時(shí)
rem[6]=LSHI;
rem[7]=HSHI;
}
//////////////////////////////讀取日期
duriqi()
{
duds1302(duri); ///// 讀日
rem[0]=LSHI;
rem[1]=HSHI;
duds1302(duyue); //////////讀月
rem[3]=LSHI;
rem[4]=HSHI;
duds1302(dunian); /////////讀年
rem[6]=LSHI;
rem[7]=HSHI;
}
//////////////////////////////////////
xieshijian(uchar wei,hwei)
{
uchar he;
xieds1302(kongzhi,0x00);
he=rem[wei]+rem[hwei]*16;
if(wei==0)xieds1302(xiemiao,he);
if(wei==3)xieds1302(xiefen,he);
if(wei==6)xieds1302(xieshi,he);
xieds1302(kongzhi,0x80);

}
////////////////////////////
xieriqi(uchar wei,hwei)
{uchar he;
xieds1302(kongzhi,0x00);
he=rem[wei]+rem[hwei]*16;
if(wei==0)xieds1302(xieri,he);
if(wei==3)xieds1302(xieyue,he);
if(wei==6)xieds1302(xienian,he);
xieds1302(kongzhi,0x80);
}
///////////////////////////////////////
tiaozheng(uchar wei)
{
uchar hwei;
hwei=wei+1; //每數(shù)兩位,低位是wei高位是hwei
if(aj_1)
{
if(SB3==0) ////是否SB3按下,加加鍵
{
xianshi();
if(SB3==0)
{
aj_1=0; ////
while(SB3==0)xianshi();
if(SR_1) ////是不是日期
{
rem[wei]++; ////低位加加
if(rem[wei]>9)
{
rem[hwei]++; //高位加加
rem[wei]=0;
if(rem[hwei]>9)rem[hwei]=0;
}
if(wei==0)if(rem[1]==3)if(rem[0]==2)rem[0]=rem[1]=0;//日夠31歸零調(diào)整
if(wei==3)if(rem[4]==1)if(rem[3]>2)rem[3]=rem[4]=0;//月到12歸零調(diào)整
xieriqi(wei,hwei);
}
else
{
rem[wei]++;
if(rem[wei]>9)
{
rem[wei]=0;
rem[hwei]++;
if(rem[hwei]==6)rem[hwei]=0; ///是分秒時(shí)夠60歸零
}
if(wei==6)if(rem[7]==2)if(rem[6]>4)rem[7]=rem[6]=0;//是時(shí)時(shí)夠24小時(shí)歸零
xieshijian(wei,hwei); //時(shí)間寫入DS1302
}
}
}
if(SB4==0)
{
xianshi();
if(SB4==0) ///減法鍵
{
aj_1=0;
while(SB4==0)xianshi();
if(SR_1) /////是否顯示日期
{
if(rem[wei]==0) ///低位是否為零
{
if(rem[hwei]==0) /////高位是否為零
{
if(wei==0){rem[1]=3;rem[0]=1;} ///是日歸31
if(wei==3){rem[3]=2;rem[4]=1;} ///是月歸12
if(wei==6){rem[6]=rem[7]=9;} /////是年歸99
}
else {rem[hwei]--;rem[wei]=9;} ///高位減減低位變9
}
else rem[wei]--;
xieriqi(wei,hwei); //日期寫入DS1302
}
else
{
if(rem[wei]==0)
{
if(rem[hwei]==0)
{
if(wei==6){rem[6]=4;rem[7]=2;}
else {rem[wei]=9;rem[hwei]=5;}
}
else {rem[hwei]--;rem[wei]=9;}
}
else rem[wei]--;
xieshijian(wei,hwei);
}
}
}
}
}
///////////////////////////////////////////按鍵
anjian1() ///按鍵1
{
if(SB1)if(SB2)if(SB3)if(SB4)aj_1=1;
if(aj_1)if(SB1==0)
{
aj_1=0;
xianshi();
if(SB1==0)
{
while(SB1==0)xianshi();
SR_1=~SR_1;
}
}
}
////////////
anjian2()
{
if(aj_1)if(SB2==0)
{
aj_1=0;
xianshi();
if(SB2==0)
{
while(SB2==0)xianshi();
moshi=moshi-3; //模式減三,正好是時(shí),分,秒,日,月,年的低位
if(moshi>6)moshi=9;
}
}
}
// /////////////////////////
void main (void)
{
SB1=SB2=SB3=SB4=1;
moshi=9;
xieds1302(kongzhi,0x80);
DCE=0;
while(1)
{
if(moshi==9)
{
if(SR_1)duriqi();
else dushijian();
}
xianshi();
anjian1();//模式時(shí)日選擇
anjian2();//調(diào)整模式
tiaozheng(moshi); //調(diào)整時(shí)間
}
}
/////



關(guān)鍵詞: 時(shí)鐘 可調(diào) DS1302

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉