只是简单的实现、在数码管上显示时分秒、那位大侠帮看看哪里有问题、编译没有错误、上电后数码管为0不记时、
#include
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
***it du=P2^6;
***it we=P2^7;
***it SCLK=P2^1;
***it SDA=P2^0;
***it RST=P2^4;
uchar hour,minute,second,h1,h0,m1,m0,s1,s0;
#define write_hour 0x84
#define write_minute 0x82
#define write_second 0x80
#define read_hour 0x85
#define read_minute 0x83
#define read_second 0x81
void delay()
{
uchar x,y;
for(x=1;x>0;x--)
for(y=110;y>0;y--);
}
void write_byte(uchar date)
{
uchar i;
RST=1;
SCLK=0;
for(i=0;i<8;i++)
{
date>>=1;
SDA=date&0x01;
SCLK=1;
delay();
SCLK=0;
}
RST=0;
}
uchar read_byte()
{
uchar i,date=0;
RST=1;
for(i=0;i<8;i++)
{
SCLK=1;
date=date>>1;
if(SDA==1)
date=date|0x80;
else
date=date|0x00;
SCLK=0;
delay();
}
return (date);
RST=0;
}
void reset()
{
RST=0;
SCLK=0;
RST=1;
}
void write_add(uchar address,uchar date)
{
reset();
RST=1;
SCLK=0;
write_byte(address);
write_byte(date);
SDA=0;
SCLK=1;
RST=0;
}
uchar read_add(uchar address)
{
uchar date;
reset();
RST=1;
SCLK=0;
write_byte(address);
SCLK=1;
delay();
SCLK=1;
date=read_byte();
SCLK=0;
date=date/16*10+date%16;
SDA=0;
RST=0;
return (date);
}
void clear_WP()
{
reset();
RST=1;
write_add(0x8e,0x00);
//SDA=0;
}
void set_WP()
{
reset();
RST=1;
write_add(0x8e,0x80);
SDA=0;
RST=0;
}
void init()
{
clear_WP();
write_add(write_hour,0x01);
write_add(write_minute,0x05);
write_add(write_second,0x00);
// write_add(write_second,0x80);
set_WP();
}
void read_time()
{
hour=read_add(read_hour);
minute=read_add(read_minute);
second=read_add(read_second);
}
void display(uchar h1,uchar h0,uchar m1,uchar m0,uchar s1,uchar s0)
{
h1=hour/10;
h0=hour%10;
we=1;
P0=0xfe;
we=0;
P0=0xff;
du=1;
P0=table[h1];
du=0;
delay();
we=1;
P0=0xfd;
we=0;
du=1;
P0=table[h0];
du=0;
delay();
m1=minute/10;
m0=minute%10;
we=1;
P0=0xfb;
we=0;
P0=0xff;
du=1;
P0=table[m1];
du=0;
delay();
we=1;
P0=0xf7;
we=0;
P0=0xff;
du=1;
P0=table[m0];
du=0;
delay();
s1=second/10;
s0=second%10;
we=1;
P0=0xef;
we=0;
P0=0xff;
du=1;
P0=table[s1];
du=0;
delay();
we=1;
P0=0xdf;
we=0;
P0=0xff;
du=1;
P0=table[s0];
du=0;
delay();
}
void main()
{
init();
while(1)
{
read_time();
display(h1,h0,m1,m0,s1,s0);
}
}
|