用DS1302做的可调时钟
扫描二维码
随时随地手机看文章
///////////////////////////////////////////////////////////////////
/*名称:单片机读取DS1302时钟芯片,带调整功能 /
/* /
///////////////////////////////////////////////////////////////////*/
#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
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; ////DS1302管脚定义
sbit DIO=P2^1;
sbit DCE=P2^2;
sbit SB1=P2^7; ////按键定义:时日选择键
sbit SB2=P2^6; ////调整模式键
sbit SB3=P2^5; ////加加键
sbit SB4=P2^4; ////减减键
sbit ACC_7=ACC^7;
/////////////////////////////
yanshi(uint kk)
{
while(kk--);
}
///////////////////////显示
xianshi()
{
uchar i;
for(i=0;i<8;i++)
{
if(shan_1)if(i==moshi)i=i+2;
P0=~(0x01<<i);
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;
}
}
////////////////////////////向DS1302写数据
xieds1302(uchar dizhi,shuju)
{uchar i,tem;
DCE=0;
SCLK=0;
DCE=1;
for(i=0;i<8;i++) //地址传输
{
SCLK=0;
tem=dizhi;
DIO=(bit)(tem&0x01);
dizhi>>=1;
SCLK=1;
}
for(i=0;i<8;i++) ///数据传送
{
SCLK=0;
tem=shuju;
DIO=(bit)(tem&0x01);
shuju>>=1;
SCLK=1;
}
DCE=0;
}
/////////////////////////////////////读取DS1302的数据
duds1302(uchar dizhi)
{
uchar i,tem,temp;
temp=0;
DCE=0;
SCLK=0;
DCE=1;
for(i=0;i<8;i++) ////传送地址
{
SCLK=0;
tem=dizhi;
DIO=(bit)(tem&0x01);
dizhi>>=1;
SCLK=1;
}
for(i=0;i<8;i++) ////读取数据
{
ACC_7=DIO;
SCLK=0;
ACC>>=1;
SCLK=1;
}
DCE=0;
temp=ACC;
LSHI=temp&0x0f;
HSHI=(temp&0xf0)>>4;
}
/////////////////////////////////读取时间
dushijian()
{
duds1302(dumiao); ///读秒
rem[0]=LSHI;
rem[1]=HSHI;
duds1302(dufen); ///读分
rem[3]=LSHI;
rem[4]=HSHI;
duds1302(dushi); ///读时
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; //每数两位,低位是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归零调整
if(wei==3)if(rem[4]==1)if(rem[3]>2)rem[3]=rem[4]=0;//月到12归零调整
xieriqi(wei,hwei);
}
else
{
rem[wei]++;
if(rem[wei]>9)
{
rem[wei]=0;
rem[hwei]++;
if(rem[hwei]==6)rem[hwei]=0; ///是分秒时够60归零
}
if(wei==6)if(rem[7]==2)if(rem[6]>4)rem[7]=rem[6]=0;//是时时够24小时归零
xieshijian(wei,hwei); //时间写入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; //模式减三,正好是时,分,秒,日,月,年的低位
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();//模式时日选择
anjian2();//调整模式
tiaozheng(moshi); //调整时间
}
}
/////