================
#include"AT89x52.h"
#include"intrins.h"
#include"math.h"
#define uint unsigned int
#define uchar unsigned char
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define WRITE_PROTECT 0x8E
sbit ACC_7 = ACC^7;
sbit DUAN=P2^6;
sbit WEI=P2^7;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit SCLK = P1^0;
sbit DIO= P1^1;
sbit CE = P2^0;
uchar shi,fen,miao;
uchar Temp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Write1302 ( uchar addr,uchar dat )
{
uchar i,temp;
CE=0; //CE引脚为低,数据传送中止
SCLK=0; //清零时钟总线
CE = 1; //CE引脚为高,逻辑控制有效
//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK = 0;
temp = addr;
DIO = (bit)(temp&0x01); //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}
//发送数据
for ( i=8; i>0; i-- )
{
SCLK = 0;
temp = dat;
DIO = (bit)(temp&0x01);
dat >>= 1;
SCLK = 1;
}
CE = 0;
}
uchar Read1302(uchar addr)
{
uchar i,temp,dat1,dat2;
CE=0; //CE引脚为低,数据传送中止
SCLK=0; //清零时钟总线
CE = 1; //CE引脚为高,逻辑控制有效
//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK = 0;
temp = addr;
DIO = (bit)(temp&0x01); //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}
//读取数据
for ( i=8; i>0; i-- )
{
ACC_7=DIO;
SCLK = 0;
ACC>>=1;
SCLK = 1;
}
CE=0;
dat1=ACC;
dat2=dat1/16; //数据进制转换
dat1=dat1%16; //十六进制转十进制
dat1=dat1+dat2*10;
return (dat1);
}
void get(void)
{
miao=Read1302(WRITE_SECOND);
fen=Read1302(WRITE_MINUTE);
shi=Read1302(WRITE_HOUR);
}
void Initial(void)
{
Write1302 (WRITE_PROTECT,0X00); //禁止写保护
Write1302 (WRITE_SECOND,0x00); //秒位初始化
Write1302 (WRITE_MINUTE,0x00); //分钟初始化
Write1302 (WRITE_HOUR,0x12); //小时初始化
Write1302 (WRITE_PROTECT,0x80); //允许写保护
}
void wei_1(uchar wei)
{
WEI=1;
P0=wei;
WEI=0;
}
void duan_1(uchar duan)
{
DUAN=1;
P0=Temp[duan];
DUAN=0;
}
void jishuan(void)
{
uchar i;
uchar miao_1,miao_2,fen_1,fen_2,shi_1,shi_2;
while(1)
{
get();
/* if(miao>59)
{
miao=0;
if(fen++>=59)
{
fen=0;
if(shi++>=23)
shi=0;
}
} */
if(key1==0) //如果键按下
{
delay(5);
if(key1==0) //延时后在判断键是否按下
{
shi++; //按下一次加1
if(shi>=24)shi=0;
Write1302 (WRITE_HOUR,shi%10+shi/10*16);
}
while(!key1); //松手检测
}
if(key2==0) //如果键按下
{
delay(5);
if(key2==0) //延时后在判断键是否按下
{
fen++; //按下一次加1
if(fen>=60)fen=0;
Write1302 (WRITE_MINUTE,fen%10+fen/10*16);
}
while(!key2); //松手检测
}
if(key3==0) //如果键按下
{
delay(5);
if(key3==0) //延时后在判断键是否按下
{
miao=0;
Write1302 (WRITE_SECOND,0x00);
}
while(!key3); //松手检测
}
miao_1=miao%10;
miao_2=miao/10;
fen_1=fen%10;
fen_2=fen/10;
shi_1=shi%10;
shi_2=shi/10;
duan_1(miao_1);
wei_1(0x7f);
for(i=0;i<150;i++);
duan_1(miao_2);
wei_1(0xbf);
for(i=0;i<150;i++);
duan_1(fen_1);
wei_1(0xEF);
for(i=0;i<150;i++);
duan_1(fen_2);
wei_1(0xF7);
for(i=0;i<150;i++);
duan_1(shi_1);
wei_1(0xFD);
for(i=0;i<150;i++);
duan_1(shi_2);
wei_1(0xFE);
for(i=0;i<150;i++);
duan_1(10);
wei_1(0xDB);
for(i=0;i<150;i++);
}
}
void main()
{
Initial();
jishuan();
}
|