#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar xingqi,nian,yue,ri,xiaoshi,miao;
uchar dd;
uchar x,y,c;
sbit clk=P1^0;
sbit dat=P1^1;
sbit rst=P1^2;
sbit A0=ACC^0;
sbit A1=ACC^1;
sbit A2=ACC^2;
sbit A3=ACC^3;
sbit A4=ACC^4;
sbit A5=ACC^5;
sbit A6=ACC^6;
sbit A7=ACC^7;
sbit p30=P3^0;
sbit p31=P3^1;
sbit p32=P3^2;
sbit p33=P3^3;
sbit p34=P3^4;
sbit p35=P3^5;
sbit p36=P3^6;
sbit p37=P3^7;
sbit date=P2^0; //max7219
sbit load=P2^1;
sbit clkk=P2^2; //max7219
uchar bdata bitmsb;
sbit m7=bitmsb^7;
bit flag=0;
uchar code tabc[]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b};
uchar code taba[]={0xff,0xf9,0xA4,0xb0,0x99,0x92,0x82,0xf8};
uchar code tab1[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,
0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,
0x24,0x25,0x26,0x27,0x28,0x29,0x30,0x31,
0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
0x48,0x49,0x50,0x51,0x52,0x53,0x54,0x55,
0x56,0x57,0x58,0x59,0x60};
void send(void)
{
uchar i;
uchar count;
bitmsb=x;
for(count=0;count<8;count++)
{
if(m7)
{
clkk=0;
date=1;
for(i=0;i<1;i++);//延时
clkk=1;
}
else
{
clkk=0;
date=0;
for(i=0;i<1;i++);
clkk=1;
}
bitmsb<<=1;
}
}
void wr(void)
{
load=0;
send();
x=y;
send();
load=1;
}
void ready(void)
{
x=0xfb;
y=0x07;
wr();
x=0xf9;
y=0x00;
wr();
x=0xfa;
y=0x0c;
wr();
x=0xfc;
y=0x01;
wr();
}
void inputbyte(uchar dd)
{
uchar i;
ACC=dd;
for(i=8;i>0;i--)
{
dat=A0; //dat=(bit)(ACC&0x01);不能用这样的语句
clk=1;
clk=0;
ACC=ACC>>=1;
}
}
void outputbyte(void)
{
uchar i;
dat=1;
for(i=8;i>0;i--)
{
ACC=ACC>>=1; //⒂乙埔晃桓持?
A7=dat; //dat=p1.1,先读出的是低位数据
clk=1;
clk=0;
}
dd=ACC;
}
void write(uchar addr,uchar num)
{
rst=0;
clk=0;
rst=1;
inputbyte(addr);
inputbyte(num);
clk=1;
rst=0;
}
void read(uchar addr)
{
rst=0;
clk=0;
rst=1;
inputbyte(addr);
outputbyte();
clk=1;
rst=0;
}
void panduan(int m,int n)
{
if((n%2!=0&&n<=7)||(n%2==0&&n>7))
{if(ri>=32)
ri=1;
}
else if(n==2&&m%4==0&&m%100!=0)
{if(ri>=30)
ri=1;
}
else if(n==2)
{if(ri>=29)
ri=1;
}
else
{if(ri>=31)
ri=1;
}
}
void main(void)
{
uchar i,a=1;
uchar fen1,fen2,fen3,fen4;
P3=0xff;
ready();
c=1;
x=1;
for(i=0;i<9;i++)
{
y=tabc[i];
x=c;
wr();
c++;
for(i=0;i<200;i++);
for(i=0;i<200;i++);
for(i=0;i<200;i++);
for(i=0;i<200;i++);
}
write(0x8e,0x00);//允许写
write(0x80,0x00); //秒
write(0x82,0x10);//分
write(0x84,0x16); //时
write(0x86,0x08); // 天
write(0x88,0x12);//
write(0x8c,0x08);//年
write(0x8a,0x05);
while(1)
{
for(i=0;i<200;i++);
for(i=0;i<200;i++);
for(i=0;i<200;i++);
for(i=0;i<200;i++);
read(0x8b);
xingqi=dd;
P0=taba[xingqi];
if(p30==0)
if(flag)
{
read(0x8b);
xingqi=dd;
xingqi++;
if(xingqi>=8)
xingqi=1;
write(0x8a,xingqi);
while(p30==0);
}
read(0x85);
fen1=dd;
fen2=fen1&0x0f;
fen3=fen1&0xf0;
fen3=fen3>>=4;
xiaoshi=fen3*10+fen2;
y=tabc[fen3];x=1;wr();
y=tabc[fen2];x=2;wr();
y=0x01,x=3;wr();
if(p34==0)
if(flag)
{
xiaoshi++;
if(xiaoshi>=24)
xiaoshi=0;
write(0x84,tab1[xiaoshi]);
while(p34==0);
}
read(0x83);
fen1=dd;
fen2=fen1&0x0f;
fen3=fen1&0xf0;
fen3=fen3>>=4;
fen4=fen3*10+fen2;
y=tabc[fen3];x=4;wr();
y=tabc[fen2];x=5;wr();
y=0x01,x=6;wr();
if(p35==0)
if(flag)
{
fen4++;
if(fen4>=60)
fen4=0;
write(0x82,tab1[fen4]);
while(p35==0);
}
read(0x81);
fen1=dd;
fen2=fen1&0x0f;
fen3=fen1&0xf0;
miao=fen3>>=4;
y=tabc[fen3];x=7;wr();
y=tabc[fen2];x=8;wr();
if(p36!=a)
{ read(0x8d);
fen1=dd;
fen2=fen1&0x0f;
fen3=fen1&0xf0;
fen3=fen3>>=4;
nian=fen3*10+fen2;
y=tabc[fen3];x=7;wr();
y=tabc[fen2];x=8;wr();
y=0x01,x=6;wr();
if(p31==0)
if(flag)
{
nian++;
if(nian>=31)
nian=8;
write(0x8c,tab1[nian]);
while(p31==0);
}
read(0x89);
fen1=dd;
fen2=fen1&0x0f;
fen3=fen1&0xf0;
fen3=fen3>>=4;
yue=fen3*10+fen2;
y=tabc[fen3];x=4;wr();
y=tabc[fen2];x=5;wr();
y=0x01,x=3;wr();
if(p32==0)
if(flag)
{
yue++;
if(yue>=13)
yue=1;
write(0x88,tab1[yue]);
while(p32==0);
}
read(0x87);
fen1=dd;
fen2=fen1&0x0f;
fen3=fen1&0xf0;
fen3=fen3>>=4;
ri=fen3*10+fen2;
y=tabc[fen3];x=1;wr();
y=tabc[fen2];x=2;wr();
if(p33==0)
if(flag)
{
ri++;
panduan(nian,yue);
write(0x86,tab1[ri]);
while(p33==0);
}
a=0;
}
else a=1;
if(p37==0)
{
flag=~flag;
if (flag) //加了分号。总是提示句法错误(syntax)
write(0x80,0x80);
else
write(0x80,0x00);
while(p37==0);
}
}
}