#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define NOP() { _nop_();_nop_();_nop_();_nop_();}
sbit mm=P0^0;
sbit min=P0^1;
sbit hh=P0^2;
sbit zhuokey=P0^3;
sbit rikey=P0^4;
sbit DAT=P1^4;
sbit CLK=P1^5;
sbit stcp=P1^7;
sbit SCLK = P0^7; //1302
sbit DSIO = P0^6; //1302DATA
sbit RST=P0^5; //1302CE
uint num,num1,aa,f;
uchar zhuo,shi,fen,miao;
uchar code taba[]={0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
,0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
uchar code tab11[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
uchar code tab0[]={0,0,0,0,0,0,24,24,36,36,66,66,66,66,66,66,66,66,66,66,66,66,36,36,24,24,0,0,0,0,0,0}; //大字0--9
uchar code tab1[]={0,0,0,0,0,0,8,8,14,14,8,8,8,8,8,8,8,8,8,8,8,8,8,8,62,62,0,0,0,0,0,0};
uchar code tab2[]={0,0,0,0,0,0,60,60,66,66,66,66,64,64,32,32,16,16,8,8,4,4,66,66,126,126,0,0,0,0,0,0};
uchar code tab3[]={0,0,0,0,0,0,60,60,66,66,66,66,32,32,24,24,32,32,64,64,66,66,66,66,60,60,0,0,0,0,0,0};
uchar code tab4[]={0,0,0,0,0,0,16,16,16,16,24,24,20,20,18,18,18,18,126,126,16,16,16,16,120,120,0,0,0,0,0,0};
uchar code tab5[]={0,0,0,0,0,0,126,126,2,2,2,2,58,58,70,70,64,64,64,64,66,66,66,66,60,60,0,0,0,0,0,0};
uchar code tab6[]={0,0,0,0,0,0,56,56,36,36,2,2,2,2,58,58,70,70,66,66,66,66,66,66,60,60,0,0,0,0,0,0};
uchar code tab7[]={0,0,0,0,0,0,126,126,34,34,34,34,16,16,16,16,8,8,8,8,8,8,8,8,8,8,0,0,0,0,0,0};
uchar code tab8[]={0,0,0,0,0,0,60,60,66,66,66,66,66,66,60,60,36,36,66,66,66,66,66,66,60,60,0,0,0,0,0,0};
uchar code tab9[]={0,0,0,0,0,0,28,28,34,34,66,66,66,66,98,98,92,92,64,64,64,64,34,34,30,30,0,0,0,0,0,0};
uchar code tabb[]={0,0,0,0,0,0,0,0,24,0,24,0,0,0,0,0,0,0,0,0,0,0,24,0,24,0,0,0,0,0,0,0}; //:
uchar code tab33[]={0,16,252,63,132,16,228,19,132,16,244,23,4,16,228,19,36,18,36,18,228,19,36,18,2,20,1,8,0,0,0,0}; //周
uchar code tab40[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,24,36,36,36,36,36,36,36,36,36,36,24,24}; //小字0--9
uchar code tab41[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,8,12,12,8,8,8,8,8,8,8,8,28,28};
uchar code tab42[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,24,36,36,32,32,16,16,8,8,4,4,60,60};
uchar code tab43[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,24,36,36,32,32,24,24,32,32,36,36,24,24};
uchar code tab44[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,16,24,24,20,20,18,18,62,62,16,16,16,16};
uchar code tab45[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,60,4,4,28,28,32,32,32,32,36,36,24,24};
uchar code tab46[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,28,28,2,2,2,2,30,30,34,34,34,34,28,28};
uchar code tab47[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,60,16,16,16,16,8,8,8,8,8,8,8,8};
uchar code tab48[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,24,36,36,36,36,24,24,36,36,36,36,24,24};
uchar code tab49[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,24,36,36,36,36,56,56,32,32,32,32,24,24};
uchar code tab50[]={8,0,8,8,248,31,132,0,130,0,249,15,136,0,136,0,136,16,255,63,128,0,128,0,128,0,128,0,0,0,0,0};//年
uchar code tab51[]={0,0,240,15,16,8,16,8,240,15,16,8,16,8,16,8,240,15,16,8,16,8,8,8,4,10,2,4,0,0,0,0};//月
uchar code tab10[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void delay1ms(uint e);
void dsq();
void xianshi();
void xianshi1();
void shijianchuli();
void anjian();
void sendbyte();
void Ds1302Write(uchar addr, uchar dat);
uchar Ds1302Read(uchar addr);
void Ds1302Init();
void Ds1302ReadTime();
extern uchar TIME[7];
uchar DisplayData[8];
uchar code READ_1302_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; //---DS1302写入和读取时分秒的地址命令---//
uchar code WRITE_1302_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //---秒分时日月周年 最低位读写位;-------//
uchar TIME[7] = {0x00, 0x14, 0x21, 0x11, 0x12, 0x06, 0x16}; //---DS1302时钟初始化2013年1月1日星期二12点00分00秒。---////---存储顺序是秒分时日月周年,存储格式是用BCD码---//
void Ds1302Write(uchar addr, uchar dat)
{
uchar n;
RST = 0;
NOP();
SCLK = 0;//先将SCLK置低电平。
NOP();
RST = 1; //然后将RST(CE)置高电平。
NOP();
for (n=0; n<8; n++)//开始传送八位地址命令
{
DSIO = addr & 0x01;//数据从低位开始传送
addr >>= 1;
SCLK = 1;//数据在上升沿时,DS1302读取数据
NOP();
SCLK = 0;
NOP();
}
for (n=0; n<8; n++)//写入8位数据
{
DSIO = dat & 0x01;
dat >>= 1;
SCLK = 1;//数据在上升沿时,DS1302读取数据
NOP();
SCLK = 0;
NOP();
}
RST = 0;//传送数据结束
NOP();
}
uchar Ds1302Read(uchar addr)
{
uchar n,dat,dat1;
RST = 0;
NOP();
SCLK = 0;//先将SCLK置低电平。
NOP();
RST = 1;//然后将RST(CE)置高电平。
NOP();
for(n=0; n<8; n++)//开始传送八位地址命令
{
DSIO = addr & 0x01;//数据从低位开始传送
addr >>= 1;
SCLK = 1;//数据在上升沿时,DS1302读取数据
NOP();
SCLK = 0;//DS1302下降沿时,放置数据
NOP();
}
NOP();
for(n=0; n<8; n++)//读取8位数据
{
dat1 = DSIO;//从最低位开始接收
dat = (dat>>1) | (dat1<<7);
SCLK = 1;
NOP();
SCLK = 0;//DS1302下降沿时,放置数据
NOP();
}
RST = 0;
NOP(); //以下为DS1302复位的稳定时间,必须的。
SCLK = 1;
NOP();
DSIO = 0;
NOP();
DSIO = 1;
NOP();
return dat;
}
//uchar BCD_Decimal(uchar bcd) //BCD码转十进制函数,输入BCD,返回十进制
// {
// uchar Decimal;
// Decimal=bcd>>4;
// return(Decimal=Decimal*10+(bcd&=0x0F));
// }
uchar BCD_Decimal(uchar bcd)
{
return bcd/16*10+bcd%16;
}
void Ds1302Init()
{
uchar n;
Ds1302Write(0x8E,0X00); //禁止写保护,就是关闭写保护功能
for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
{
Ds1302Write(WRITE_1302_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打开写保护功能
}
void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++)//读取7个字节的时钟信号:分秒时日月周年
{
TIME[n] = Ds1302Read(READ_1302_ADDR[n]);
}
}
void main()
{
Ds1302Init();
if(mm==0&min==0&hh==0){Ds1302Init();}
while(1)
{
Ds1302ReadTime();
if((TIME[0]/16*10+TIME[0]%16)%10==5|(TIME[0]/16*10+TIME[0]%16)%10==6|(TIME[0]/16*10+TIME[0]%16)%10==7){xianshi1();}
else{xianshi();}
anjian();
}
}
void sendbyte(uchar num1)
{
for(f=0;f<8;f++)
{
DAT=num1&0x01;
num1>>=1;
CLK=0;
NOP();
CLK=1;
NOP();
}
}
void xianshi()
{
uchar i,temp,temp1;
for(i=0;i<16;i++)
{
temp1=BCD_Decimal(TIME[2]);
temp=temp1/10%10 ;
switch(temp)
{
case 0:sendbyte(tab0[i*2]);break;
case 1:sendbyte(tab1[i*2]);break;
case 2:sendbyte(tab2[i*2]);break;
case 3:sendbyte(tab3[i*2]);break;
case 4:sendbyte(tab4[i*2]);break;
case 5:sendbyte(tab5[i*2]);break;
case 6:sendbyte(tab6[i*2]);break;
case 7:sendbyte(tab7[i*2]);break;
case 8:sendbyte(tab8[i*2]);break;
case 9:sendbyte(tab9[i*2]);break;
}
temp=temp1%10;
switch(temp)
{
case 0:sendbyte(tab0[i*2+1]);break;
case 1:sendbyte(tab1[i*2+1]);break;
case 2:sendbyte(tab2[i*2+1]);break;
case 3:sendbyte(tab3[i*2+1]);break;
case 4:sendbyte(tab4[i*2+1]);break;
case 5:sendbyte(tab5[i*2+1]);break;
case 6:sendbyte(tab6[i*2+1]);break;
case 7:sendbyte(tab7[i*2+1]);break;
case 8:sendbyte(tab8[i*2+1]);break;
case 9:sendbyte(tab9[i*2+1]);break;
}
temp1=BCD_Decimal(TIME[0]);
if(temp1%10%2==0){sendbyte(tabb[i*2]);}
else{
sendbyte(tab10[i*2]);}
temp1=BCD_Decimal(TIME[1]);
temp=temp1/10%10 ;
switch(temp)
{
case 0:sendbyte(tab0[i*2]);break;
case 1:sendbyte(tab1[i*2]);break;
case 2:sendbyte(tab2[i*2]);break;
case 3:sendbyte(tab3[i*2]);break;
case 4:sendbyte(tab4[i*2]);break;
case 5:sendbyte(tab5[i*2]);break;
case 6:sendbyte(tab6[i*2]);break;
case 7:sendbyte(tab7[i*2]);break;
case 8:sendbyte(tab8[i*2]);break;
case 9:sendbyte(tab9[i*2]);break;
}
temp=temp1%10;
switch(temp)
{
case 0:sendbyte(tab0[i*2+1]);break;
case 1:sendbyte(tab1[i*2+1]);break;
case 2:sendbyte(tab2[i*2+1]);break;
case 3:sendbyte(tab3[i*2+1]);break;
case 4:sendbyte(tab4[i*2+1]);break;
case 5:sendbyte(tab5[i*2+1]);break;
case 6:sendbyte(tab6[i*2+1]);break;
case 7:sendbyte(tab7[i*2+1]);break;
case 8:sendbyte(tab8[i*2+1]);break;
case 9:sendbyte(tab9[i*2+1]);break;
}
temp1=BCD_Decimal(TIME[0]);
temp=temp1/10%10;
switch(temp)
{
case 0:sendbyte(tab40[i*2]);break;
case 1:sendbyte(tab41[i*2]);break;
case 2:sendbyte(tab42[i*2]);break;
case 3:sendbyte(tab43[i*2]);break;
case 4:sendbyte(tab44[i*2]);break;
case 5:sendbyte(tab45[i*2]);break;
case 6:sendbyte(tab46[i*2]);break;
case 7:sendbyte(tab47[i*2]);break;
case 8:sendbyte(tab48[i*2]);break;
case 9:sendbyte(tab49[i*2]);break;
}
temp=temp1%10;
switch(temp)
{
case 0:sendbyte(tab40[i*2+1]);break;
case 1:sendbyte(tab41[i*2+1]);break;
case 2:sendbyte(tab42[i*2+1]);break;
case 3:sendbyte(tab43[i*2+1]);break;
case 4:sendbyte(tab44[i*2+1]);break;
case 5:sendbyte(tab45[i*2+1]);break;
case 6:sendbyte(tab46[i*2+1]);break;
case 7:sendbyte(tab47[i*2+1]);break;
case 8:sendbyte(tab48[i*2+1]);break;
case 9:sendbyte(tab49[i*2+1]);break;
}
sendbyte(tab33[i*2]);
sendbyte(tab33[i*2+1]);
temp1=BCD_Decimal(TIME[5]);
temp=temp1%10;
switch(temp)
{
case 7: sendbyte(tab1[i*2+1]);break;
case 1: sendbyte(tab2[i*2+1]);break;
case 2: sendbyte(tab3[i*2+1]);break;
case 3: sendbyte(tab4[i*2+1]);break;
case 4: sendbyte(tab5[i*2+1]);break;
case 5: sendbyte(tab6[i*2+1]);break;
case 6: sendbyte(tab7[i*2+1]);break;
// case 7: sendbyte(tab7[i*2+1]);break;
}
stcp=0;
NOP();
stcp=1;
NOP();
P1=taba[i];
}
}
void xianshi1()
{
uchar i,temp,temp1;
for(i=0;i<16;i++)
{
temp1=BCD_Decimal(TIME[6]);
temp=temp1/10%10 ;
switch(temp)
{
case 0:sendbyte(tab0[i*2]);break;
case 1:sendbyte(tab1[i*2]);break;
case 2:sendbyte(tab2[i*2]);break;
case 3:sendbyte(tab3[i*2]);break;
case 4:sendbyte(tab4[i*2]);break;
case 5:sendbyte(tab5[i*2]);break;
case 6:sendbyte(tab6[i*2]);break;
case 7:sendbyte(tab7[i*2]);break;
case 8:sendbyte(tab8[i*2]);break;
case 9:sendbyte(tab9[i*2]);break;
}
temp=temp1%10;
switch(temp)
{
case 0:sendbyte(tab0[i*2+1]);break;
case 1:sendbyte(tab1[i*2+1]);break;
case 2:sendbyte(tab2[i*2+1]);break;
case 3:sendbyte(tab3[i*2+1]);break;
case 4:sendbyte(tab4[i*2+1]);break;
case 5:sendbyte(tab5[i*2+1]);break;
case 6:sendbyte(tab6[i*2+1]);break;
case 7:sendbyte(tab7[i*2+1]);break;
case 8:sendbyte(tab8[i*2+1]);break;
case 9:sendbyte(tab9[i*2+1]);break;
}
sendbyte(tab50[i*2]);
sendbyte(tab50[i*2+1]);
temp1=BCD_Decimal(TIME[4]);
temp=temp1/10%10 ;
switch(temp)
{
case 0:sendbyte(tab0[i*2]);break;
case 1:sendbyte(tab1[i*2]);break;
case 2:sendbyte(tab2[i*2]);break;
case 3:sendbyte(tab3[i*2]);break;
case 4:sendbyte(tab4[i*2]);break;
case 5:sendbyte(tab5[i*2]);break;
case 6:sendbyte(tab6[i*2]);break;
case 7:sendbyte(tab7[i*2]);break;
case 8:sendbyte(tab8[i*2]);break;
case 9:sendbyte(tab9[i*2]);break;
}
temp=temp1%10;
switch(temp)
{
case 0:sendbyte(tab0[i*2+1]);break;
case 1:sendbyte(tab1[i*2+1]);break;
case 2:sendbyte(tab2[i*2+1]);break;
case 3:sendbyte(tab3[i*2+1]);break;
case 4:sendbyte(tab4[i*2+1]);break;
case 5:sendbyte(tab5[i*2+1]);break;
case 6:sendbyte(tab6[i*2+1]);break;
case 7:sendbyte(tab7[i*2+1]);break;
case 8:sendbyte(tab8[i*2+1]);break;
case 9:sendbyte(tab9[i*2+1]);break;
}
sendbyte(tab51[i*2]);
sendbyte(tab51[i*2+1]);
temp1=BCD_Decimal(TIME[3]);
temp=temp1/10%10;
switch(temp)
{
case 0:sendbyte(tab0[i*2]);break;
case 1:sendbyte(tab1[i*2]);break;
case 2:sendbyte(tab2[i*2]);break;
case 3:sendbyte(tab3[i*2]);break;
case 4:sendbyte(tab4[i*2]);break;
case 5:sendbyte(tab5[i*2]);break;
case 6:sendbyte(tab6[i*2]);break;
case 7:sendbyte(tab7[i*2]);break;
case 8:sendbyte(tab8[i*2]);break;
case 9:sendbyte(tab9[i*2]);break;
}
temp=temp1%10;
switch(temp)
{
case 0:sendbyte(tab0[i*2+1]);break;
case 1:sendbyte(tab1[i*2+1]);break;
case 2:sendbyte(tab2[i*2+1]);break;
case 3:sendbyte(tab3[i*2+1]);break;
case 4:sendbyte(tab4[i*2+1]);break;
case 5:sendbyte(tab5[i*2+1]);break;
case 6:sendbyte(tab6[i*2+1]);break;
case 7:sendbyte(tab7[i*2+1]);break;
case 8:sendbyte(tab8[i*2+1]);break;
case 9:sendbyte(tab9[i*2+1]);break;
}
stcp=0;
NOP();
stcp=1;
NOP();
P1=taba[i];
}
}
void anjian()
{ uchar temp;
if(min==0)
{
delay1ms(5);
if(min==0){num++;}
if(num==20){num=0;fen++;}
if(fen==60){fen=0;}
temp=fen/10*16+fen%10; //10转16进制BCD
Ds1302Write(0x8E,0X00);
Ds1302Write(0x82,temp) ;
Ds1302Write(0x8E,0x80);
}
if(hh==0)
{
delay1ms(5);
if(hh==0){num++;}
if(num==20){num=0;shi++;}
if(shi==24){shi=0;}
temp=shi/10*16+shi%10;
Ds1302Write(0x8E,0X00);
Ds1302Write(0x84,temp) ;
Ds1302Write(0x8E,0x80);
}
if(mm==0)
{
delay1ms(5);
if(mm==0){miao=0;}
Ds1302Write(0x8E,0X00);
Ds1302Write(0x80,miao) ;
Ds1302Write(0x8E,0x80);
}
if(zhuokey==0)
{
delay1ms(30);
if(zhuokey==0){num++;}
if(num==20){num=0;zhuo++;}
if(zhuo==7){zhuo=1;}
Ds1302Write(0x8E,0X00);
Ds1302Write(0x8a,zhuo) ;
Ds1302Write(0x8E,0x80);
}
}
void delay1ms(uint e)
{
uchar a,b,c,d;
for(d=e;d>0;d--)
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
|