#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
uchar code table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//后边加的又重新加的0-9是为了搞清楚到底显示什么数。。
sbit sck=P1^7;
sbit io=P2^3;
sbit rst=P1^3;
sbit wr=P3^6;
sbit p25=P2^5;
sbit p26=P2^6;
sbit p27=P2^7;
uchar
time_data[7]={12,12,01,01,01,01,01};//年周月日时分秒
uchar write_data[7]={0x8c,0x8a,0x88,0x86,0xd84,0x82,0x80};
uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar disp[8]={0};
void write_ds1302_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
dat=dat>>1;
sck=0;
// io=dat&0x01;
io=CY;
sck=1;
}
}
/*uchar read_byte()
{
uchar i,k;
scl=0;
for(i=0;i<8;i++)
{
k=k>>1;
if(io)
k=k|0x80;
scl=1;
scl=0;
}
return k;
}*/
void write_ds1302(uchar add,uchar byte)
{
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(byte);
rst=0;
_nop_();
// io=1;
// _nop_();
sck=1;
_nop_();
}
uchar read_ds1302(uchar add)
{
uchar i,value;
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
value=value>>1;
if(io)
value=value|0x80;
sck=1;
sck=0;
}
sck=1;
_nop_();
rst=0;
_nop_();
return value;
}
void time_pros()
{
disp[0]=time_data[6]&0x0f;
time_data[6]=time_data[6]&0x70;
time_data[6]=time_data[6]>>4;
disp[1]=time_data[6]&0x0f;
disp[2]=time_data[5]&0x0f;
time_data[5]=time_data[5]&0x70;
time_data[5]=time_data[5]>>4;
disp[3]=time_data[5]&0x0f;
disp[4]=time_data[4]&0x0f;
time_data[4]=time_data[4]&0x70;
time_data[4]=time_data[4]>>4;
disp[5]=time_data[4]&0x0f;
}
void set_rtc()
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_data[i]/10;
time_data[i]=time_data[i]%10;
time_data[i]=time_data[i]+j*16;
}
write_ds1302(0x8e,0x00);//去除写保护
for(i=0;i<7;i++)
{
write_ds1302(write_data[i],time_data[i]);
}
write_ds1302(0x8e,0x80);//家写保护
}
void read_rtc()
{
uchar i;
for(i=0;i<7;i++)
{
time_data[i]=read_ds1302(read_add[i]);
}
time_pros();
}
void dellay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void display()
{
// wr=0;
/* P0=0xff;
p25=0;
p26=1;
p27=1;
P0=0x80;
p25=1;
p26=1;
p27=1;
P0=table[disp[0]];
dellay(12); */
P0=0xff;
p25=0;
p26=1;
p27=1;
P0=0x40;
p25=1;
p26=1;
p27=1;
P0=table[disp[1]];
dellay(12);
/* P0=0xff;
p25=0;
p26=1;
p27=1;
P0=0x20;
p25=1;
p26=1;
p27=1;
P0=table[disp[2]];
dellay(12);
P0=0xff;
p25=0;
p26=1;
p27=1;
P0=0x10;
p25=1;
p26=1;
p27=1;
P0=table[disp[3]];
dellay(12);
P0=0xff;
p25=0;
p26=1;
p27=1;
P0=0x01;
p25=1;
p26=1;
p27=1;
P0=table[disp[4]];
dellay(12);
P0=0xff;
p25=0;
p26=1;
p27=1;
P0=0x02;
p25=1;
p26=1;
p27=1;
P0=table[disp[5]];
// dellay(1); */
}
void main()
{
uchar x=0;
set_rtc();
while(1)
{
read_rtc();
dellay(200);
dellay(200);
// time_pros();
display();
/* disp[0]++;
if(disp[0]>9)
disp[0]=0; */
}
}
为什么秒的个位总是在每显示显示正常数后回立刻显示5呢??秒的十位回在正常显示后立刻显示7?
一周热门 更多>