求助,我用51做ds1302时钟,时钟不走只显示85!
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
sbit io =P3^2; //数据脚
sbit sclk=P3^3; //时钟脚
sbit rst =P3^4; //复位脚
sbit ACC0=ACC^0;//累加器
sbit ACC7=ACC^7;
sbit duan=P3^6;
sbit wei =P3^7;
unsigned char digtalnum[10]={0x3f,0x30,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char now_time[7];//秒、分、时、日、月、周、年
void write_byte(unsigned char da)
{
unsigned char i;
io=0;
ACC=da;
for(i=0;i<8;i++)
{
io=ACC0;
sclk=0;
sclk=1;
ACC=ACC>>1;
}
}
unsigned char read_byte() //
{
unsigned char i;
io=1;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
sclk=1;
sclk=0;
ACC7=io;
}
return(ACC);
}
void write_ds1302(unsigned char add,unsigned char dat)
{
rst=0;
sclk=0;
rst=1;
write_byte(add);
write_byte(dat);
rst=0;
sclk=1;
}
unsigned char read_ds1302(unsigned char add)
{
unsigned char temp;
rst=0;
sclk=0;
rst=1;
write_byte(add);
temp=read_byte();
rst=0;
sclk=1;
return (temp);
}
void init()
{
EA=0;
TMOD=0x01;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
ET0=1;
TR0=1;
}
void timer0() interrupt 1 //中断
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
}
void set_time()
{
write_ds1302(0x8e,0x00);//关闭写保护
write_ds1302(0x80,0x01);
write_ds1302(0x82,0x36);
write_ds1302(0x84,0x06);
write_ds1302(0x8e,0x80);//开启写保护
}
void get_time()
{
unsigned char i,read_add,r_bcd;
read_add=0x81;//赋地址
for(i=0;i<7;i++)
{
r_bcd=read_ds1302(read_add);//从地址读数据
read_add=read_add+2; //地址+2
now_time[i]=(r_bcd>>4)*10+(r_bcd%16);//BCD码转成10进制
}
}
void display()
{
duan=1;
P0=digtalnum[now_time[0]/10];
duan=0;
wei=1;
P0=0xfe;
wei=0;
duan=1;
P0=digtalnum[now_time[0]%10];
duan=0;
wei=1;
P0=0xfd;
wei=0;
}
void main()
{
set_time();
while(1)
{
get_time();
display();
}
}
sbit io =P3^2; //数据脚
sbit sclk=P3^3; //时钟脚
sbit rst =P3^4; //复位脚
sbit ACC0=ACC^0;//累加器
sbit ACC7=ACC^7;
sbit duan=P3^6;
sbit wei =P3^7;
unsigned char digtalnum[10]={0x3f,0x30,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char now_time[7];//秒、分、时、日、月、周、年
void write_byte(unsigned char da)
{
unsigned char i;
io=0;
ACC=da;
for(i=0;i<8;i++)
{
io=ACC0;
sclk=0;
sclk=1;
ACC=ACC>>1;
}
}
unsigned char read_byte() //
{
unsigned char i;
io=1;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
sclk=1;
sclk=0;
ACC7=io;
}
return(ACC);
}
void write_ds1302(unsigned char add,unsigned char dat)
{
rst=0;
sclk=0;
rst=1;
write_byte(add);
write_byte(dat);
rst=0;
sclk=1;
}
unsigned char read_ds1302(unsigned char add)
{
unsigned char temp;
rst=0;
sclk=0;
rst=1;
write_byte(add);
temp=read_byte();
rst=0;
sclk=1;
return (temp);
}
void init()
{
EA=0;
TMOD=0x01;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
ET0=1;
TR0=1;
}
void timer0() interrupt 1 //中断
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
}
void set_time()
{
write_ds1302(0x8e,0x00);//关闭写保护
write_ds1302(0x80,0x01);
write_ds1302(0x82,0x36);
write_ds1302(0x84,0x06);
write_ds1302(0x8e,0x80);//开启写保护
}
void get_time()
{
unsigned char i,read_add,r_bcd;
read_add=0x81;//赋地址
for(i=0;i<7;i++)
{
r_bcd=read_ds1302(read_add);//从地址读数据
read_add=read_add+2; //地址+2
now_time[i]=(r_bcd>>4)*10+(r_bcd%16);//BCD码转成10进制
}
}
void display()
{
duan=1;
P0=digtalnum[now_time[0]/10];
duan=0;
wei=1;
P0=0xfe;
wei=0;
duan=1;
P0=digtalnum[now_time[0]%10];
duan=0;
wei=1;
P0=0xfd;
wei=0;
}
void main()
{
set_time();
while(1)
{
get_time();
display();
}
}
我的也是这样 不知道怎么回事