找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

ds1302初始化不了!!急!求助1

[复制链接]
admin 发表于 2012-9-2 13:44:15 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本人菜鸟级别,写了个1302时钟的,可是怎么也初始化不了 时钟!!!求助于高手!
#include<reg52.h>
#include <intrins.h>

/*******************************
该程序最大的问题是1302写不进去,初始化不了!!!


*******************************/

#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit lcdrs=P3^5;

sbit key3=P3^3;
sbit key4=P3^6;
sbit key5=P3^7;

#define write_second 0x80//1302写的宏定义
#define write_minute 0x82
#define write_hour 0x84
#define write_day 0x86
#define write_month 0x88
#define write_week 0x8a
#define write_year 0x8c


#define read_second 0x81//1302读的宏定义
#define read_minute 0x83
#define read_hour 0x85
#define read_day 0x87
#define read_month 0x89
#define read_week 0x8b
#define read_year 0x8d

#define write_protect 0x8e
#define read_protect 0x8f


sbit ACC_7 = ACC^7;
sbit beep=P2^3;

//管脚定义
sbit SCLK = P2^5; // DS1302时钟信号
sbit DIO= P2^6; // DS1302数据信号
sbit CE = P2^7; // DS1302片选


uchar num,temp;
uchar shi,fen,miao,day,month,year,week;
uchar date;
uchar flag,s1num;

uint count;
uchar code table[]="20 - - ";
uchar code table3[]=" : : ";


void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)//1602写命令
{
lcdrs=0;
P0=com;
delay(1);
lcden=1;
delay(1);
lcden=0;
}

void write_data(uchar date)//1602读命令
{
lcdrs=1;
P0=date;
delay(1);
lcden=1;
delay(1);
lcden=0;
}
void write_1302(uchar addr,uchar dat)//单字节写
{
uchar i,temp;
CE=0;
SCLK=0;
CE=1;
for(i=8;i>0;i--)
{
SCLK=0;
temp=addr;
DIO=(bit)(temp&0x01);

addr>>=1;
SCLK=1;

}
for(i=8;i>0;i--)
{
SCLK=0;
temp=dat;
DIO=(bit)(temp&0x01);

addr>>=1;
SCLK=1;

}
CE=0;
}

unsigned char read_1302(uchar addr)//1302单字节读
{
uchar i;uchar temp;uchar dat1;uchar dat2;
CE=0;
SCLK=0;
CE=1;
for(i=8;i>0;i--)
{
SCLK=0;
temp=addr;
DIO=(bit)(temp&0x01);
addr>>=1;
SCLK=1;

}
for(i=8;i>0;i--)
{
ACC_7=DIO;
SCLK=1;
ACC>>=1;
SCLK=0;
}

CE=0;
dat1=ACC;
dat2=dat1/16;
dat1=dat1%16;
dat1=dat1+dat2*10;
return (dat1);
}
void initial()//初始化DS1302 2010年09月25日 23点56分15秒 周二 就
{
write_1302(write_protect,0x00);//允许写保护
write_1302(write_second,0x15);
write_1302(write_minute,0x56);
write_1302(write_hour,0x23);
write_1302(write_day,0x25);
write_1302(write_month,0x09);
write_1302(write_week,0x02);
write_1302(write_year,0x10);

}

void init()//初始化1602
{
flag=1;
s1num=0;

lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80+0x40);
for(num=0;num<18;num++)
{
write_data(table3[num]);
delay(2);
}
write_com(0x80);
for(num=0;num<14;num++)
{
write_data(table[num]);
delay(2);
}


}
void di()//蜂鸣器响函数
{
beep=0;
delay(100);
beep=1;
}


void write_sfm(uchar add,uchar date)//显示时,分,秒
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}

void display_week(uchar we)//星期函数
{
write_com(0x80+12);
switch(we)
{
case 1: write_data('M');delay(5);
write_data('O');delay(5);
write_data('N');
break;
case 2: write_data('T');delay(5);
write_data('U');delay(5);
write_data('E');
break;
case 3: write_data('W');delay(5);
write_data('E');delay(5);
write_data('D');
break;
case 4: write_data('T');delay(5);
write_data('H');delay(5);
write_data('U');
break;
case 5: write_data('R');delay(5);
write_data('I');delay(5);
write_data('R');
break;
case 6: write_data('S');delay(5);
write_data('A');delay(5);
write_data('T');
break;
case 7: write_data('S');delay(5);
write_data('U');delay(5);
write_data('N');
break;
}
}

void write_nyrz(uchar add,uchar date)//显示年,月,日,周
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}

void keyscan()//按键设置
{
if(key3==0)
{
flag=0;
delay(5);
if(key3==0)
{
s1num++;
while(!key3);
di();
switch(s1num)//光标闪烁定位
{
case 1:write_com(0x80+0x40+0x0b);//定位到秒的位置
write_com(0x0f);
break;

case 2:write_com(0x80+0x40+0x08);//定位到分的位置
write_com(0x0f);
break;

case 3:write_com(0x80+0x40+0x05);//定位到小时的位置
write_com(0x0f);
break;

case 4:write_com(0x80+0x0e);//定位到星期的位置
write_com(0x0f);
break;

case 5:write_com(0x80+0x9);//定位到日的位置
write_com(0x0f);
break;

case 6:write_com(0x80+0x6);//定位到月的位置
write_com(0x0f);
break;

case 7:write_com(0x80+0x3);//定位到年的位置
write_com(0x0f);
break;

case 8://复位,时钟走
s1num=0;
write_com(0x0c);
write_1302(write_protect,0x00);//允许写保护
write_1302(write_second,miao);//将改变的值写入DS1302
write_1302(write_minute,fen);
write_1302(write_hour,shi);
write_1302(write_day,day);
write_1302(write_month,month);
write_1302(write_week,week);
write_1302(write_year,year);
flag=1;
break;
}
}
}
if(s1num!=0)//只有功能键按下后,增大,增小建才有效
{
if(key4==0)//增大建
{
delay(5);
if(key4==0)
{
while(!key4);
di();
switch(s1num)
{
case 1: miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+11);
break;
case 2: fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+8);
break;
case 3: shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+5);
break;
case 4: week++;
if(week==8)
week=1;
display_week(week);
write_com(0x80+14);
break;
case 5: day++;
if(day==31)
day=1;
write_nyrz(8,day);
write_com(0x80+9);
break;
case 6: month++;
if(month==13)
month=1;
write_nyrz(5,month);
write_com(0x80+6);
break;
case 7: year++;
if(year==99)
year=0;
write_nyrz(2,year);
write_com(0x80+3);
break;
}
}
}
if(key5==0)//减小键
{
delay(5);
if(key5==0)
{
while(!key5);
di();
switch(s1num)
{
case 1: miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+11);
break;
case 2: fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+8);
break;
case 3: shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+5);
break;
case 4: week--;
if(week==-1)
week=7;
display_week(week);
write_com(0x80+14);
break;
case 5: day--;
if(day==0)
day=31;
write_nyrz(8,day);
write_com(0x80+9);
break;
case 6: month--;
if(month==0)
month=12;
write_nyrz(5,month);
write_com(0x80+6);
break;
case 7: year--;
if(year==-1)
year=99;
write_nyrz(2,year);
write_com(0x80+3);
break;
}
}
}

}
}


void main()//主函数
{
init();//初始化1602
initial();//初始化1302

while(1)
{
keyscan();//按键扫描
if(flag==1)
{
miao=read_1302(read_second);//显示秒
write_sfm(10,miao);
fen=read_1302(read_minute);//显示分
write_sfm(7,fen);
shi=read_1302(read_hour);//显示小时
write_sfm(4,shi);

day=read_1302(read_day);//显示日
write_nyrz(8,day);

month=read_1302(read_month);//显示月
write_nyrz(5,month);

year=read_1302(read_year);//显示年
write_nyrz(2,year);

week=read_1302(read_week);//显示日期
display_week(week);
}
else
{
write_1302(write_second,0x80);
}
}


}


就是显示void initial()//初始化DS1302 2010年09月25日 23点56分15秒 周二 这里写不进1302,我反复核对时序 也找不到问所在!!!
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-25 03:44 , Processed in 0.048744 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表