#include
#include
#define uchar unsigned char
unsigned char dspcom = 0;
unsigned char dspbuf[8] = {10,10,10,10,10,10,10,10};
code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0xff,0xbf};
sbit DSIO=P2^3;
sbit RST=P1^3;
sbit SCLK=P1^7;
uchar Num=0;
void Ds1302Write(uchar addr, uchar dat);
uchar Ds1302Read(uchar addr); // 初始化
void Ds1302Init(); //读DS1302.
void Ds1302ReadTime();
void delay();
uchar KeyScan();
uchar TIME[7];
uchar code DIG_CODE[17]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
uchar TIME[7] = {0x00, 0x04, 0x11, 0x15, 0x01, 0x07, 0x17};
uchar DisplayData[8];
void display(void);
//向DS1302命令(地址+数据)
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;
}
//初始化DS1302.
void Ds1302Init()
{
uchar n;
Ds1302Write(0x8E,0X00); //禁止写保护,就是关闭写保护功能
for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80); //打开写保护功能
}
//读取时钟信息
void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++) //读取7个字节的时钟信号:分秒时日月周年
{
TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
}
}
void main()
{ TMOD |= 0x01; //?????????
TH0 = (65536-2000)/256;
TL0 = (65536-2000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
Ds1302Init();
while(1)
{ Ds1302ReadTime();
dspbuf[0]=TIME[2]/16;
dspbuf[1]=TIME[2]&0x0f;
dspbuf[2]=11;
dspbuf[3]=TIME[1]/16;
dspbuf[4]=TIME[1]&0x0f;
dspbuf[5]=11;
dspbuf[6]=TIME[0]/16;
dspbuf[7]=TIME[0]&0x0f;
}
}
void display(void)
{
P2 = ((P2&0x1f)|0xE0);
P0 = 0xff;
P2 &= 0x1f;
P2 = ((P2&0x1f)|0xC0);
P0 = 1<