当前位置: 代码迷 >> 单片机 >> 关于时钟芯片ds1302的有关问题,纠结几天了
  详细解决方案

关于时钟芯片ds1302的有关问题,纠结几天了

热度:50   发布时间:2016-04-28 15:53:20.0
关于时钟芯片ds1302的问题,纠结几天了。
#include <reg52.h>

#define uchar unsigned char 
#define uint unsigned int

 
#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 SCLK=P2^0;
sbit DIO=P2^1;
sbit RES=P2^2;
sbit dula=P2^6;
sbit wela=P2^7;


uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uchar second,minute,hour,second1,second2,minute1,minute2,hour1,hour2;//second1和second2是second的十位和个位

void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}



void write_ds(uchar addr,uchar dat) //写入1302
{
uchar i,temp;
SCLK=0;
RES=1;
for(i=0;i<8;i++)
{
SCLK=0;
temp=addr;
DIO=(bit)(temp&0x01);
addr>>=1;
SCLK=1;
}
for(i=0;i<8;i++)
{
SCLK=0;
temp=dat;
DIO=(bit)(temp&0x01);
SCLK=1;
dat>>=1;
}
RES=0;
}


uint read_ds(uchar addr) //´读出1302中的数据
{
uchar i,temp,dat;

SCLK=0;
RES=1;
for(i=0;i<8;i++)
{
SCLK=0;
temp=addr;
DIO=(bit)(temp&0x01);
addr>>=1;
SCLK=1;
}
for(i=0;i<8;i++)
{
SCLK=1;
ACC_7=DIO;
ACC>>=1;
SCLK=0; //
}
RES=0;
dat=ACC/16*10+ACC%16;
  return dat;

}


void display() //数码管显示时分秒
{  
second1=second/10;
second2=second%10;
minute1=minute/10;
minute2=minute%10;
hour1=hour/10;
hour2=hour%10;

dula=1;
P0=table[second2];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(1);

dula=1;
P0=table[second1];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(1);

dula=1;
P0=table[minute2];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delayms(1);

dula=1;
P0=table[minute1];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delayms(1);

dula=1;
P0=table[hour2];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delayms(1);

dula=1;
P0=table[hour1];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delayms(1);
}


void init() //配置
{

write_ds(WRITE_PROTECT,0X00);
write_ds(WRITE_SECOND,0X56);
write_ds(WRITE_MINUTE,0X34);
write_ds(WRITE_HOUR,0X12);
write_ds(WRITE_PROTECT,0X80);

}

int main()
{


init();
while(1)
{  
second=read_ds(READ_SECOND);
minute=read_ds(READ_MINUTE);
hour=read_ds(READ_HOUR);
display();
  }
return 0;
}

程序很简单,就显示时分秒,会走就行。不知道为什么,数码管显示676767,而且不动,可是不连接DIO、SLK、CS这三个通道,数码管依然显示676767,分明没受控制,程序我也看了好几遍,都是些最简单的,没有检查出来错误,各位大侠帮帮忙,为什么啊?是不是程序哪不对啊?实在是自己无法解决了。

------解决方案--------------------
一般是时序问题,可先把读取的频率降下来试试看