DS1302数字钟程序哪里有错?困扰了好几天了,望高手指教
时间:10-02
整理:3721RD
点击:
DS1302数字钟程序哪里有错?刚开始显示正常,一按按钮值就回不来了,求解
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P2^5; //LCD1602
sbit rw=P2^6;
sbit en=P2^7;
sbit sclk=P1^6; //DS1302
sbit ds1302_io=P3^5;
sbit rst=P1^7;
sbit s1=P3^2; //KEY
sbit s2=P3^3;
sbit s3=P3^4;
uchar code table1[]=" 20 - - ";
uchar code table2[]=" : : ";
uchar a,miao,shi,fen,day,month,year,week,flag,s1num,value;
void delay(uint z);
void write_ds1302(uchar command,uchar value);
//sbit ACC0=ACC^0;
//sbit ACC7=ACC^7;
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void init()
{
uchar num;
/*TMOD=0x11;
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;*/
en=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_data(table1[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<10;num++)
{
write_data(table2[num]);
delay(5);
}
}
void write_nyr(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 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 write_temp(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);
write_data(0xdf);
write_data(0x43);
} */
void write_week(uchar week)
{
write_com(0x80+12);
switch(week)
{
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('F');delay(5);
write_data('R');delay(5);
write_data('I');
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 ds1302_init()
{
rst=0;
sclk=0;
write_ds1302(0x8e,0x00);
write_ds1302(0x8e,0x80);
}
void write_ds1302(uchar command,uchar value)
{
uchar i=0;
rst=0;
sclk=0;
rst=1;
for(i=0;i<8;i++)
{
ds1302_io=command&0x01;
sclk=0;
sclk=1;
command>>=1;
}
for(i=0;i<8;i++)
{
ds1302_io=value&0x01;
sclk=0;
sclk=1;
value>>=1;
}
}
uchar read_ds1302(uchar command)
{
uchar i=0;
uchar value=0;
rst=0;
sclk=0;
rst=1;
for(i=0;i<8;i++)
{
ds1302_io=command&0x01;
sclk=0;
sclk=1;
command>>=1;
}
for(i=0;i<8;i++)
{
sclk=1;
sclk=0;
if(ds1302_io)
{
value|=0x01<<i;
}
}
rst=0;
//value = (value /16 * 10) + value %16;
return value;
}
void keyscan()
{
if(s1==0) //功能键按下
{
delay(5);
if(s1==0)
{
s1num++;
flag=1;
while(!s1);
if(s1num==1)
{
// TR0=0;
write_com(0x80+0x40+8); //miao
write_com(0x0f);
value=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,0x80|value);
write_ds1302(0x8e,0x80);
}
if(s1num==2) //fen
{
write_com(0x80+0x40+5);
}
if(s1num==3) //shi
{
write_com(0x80+0x40+2);
}
if(s1num==4) //week
{
write_com(0x80+12);
//write_com(0x0f);
}
if(s1num==5) //day
{
write_com(0x80+9);
}
if(s1num==6) //month
{
write_com(0x80+6);
}
if(s1num==7) //year
{
write_com(0x80+3);
}
if(s1num==8)
{
// TR0=1;
s1num=0;
write_com(0x0c);
flag=0;
value=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,0x80|value);
write_ds1302(0x8e,0x80);
}
}
}
if(s1num!=0)
{
if(s2==0) //数字增加键按下
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(8,miao);
value=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,value);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(5,fen);
value=(fen)/10*16+(fen)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x82,value);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(2,shi);
value=(shi)/10*16+(shi)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x84,value);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+2);
}
if(s1num==4)
{
week++;
if(week==8)
week=1;
write_com(0x80+12);
write_week(week);
value=(week)/10*16+(week)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x8a,value);
write_ds1302(0x8e,0x80);
}
if(s1num==5)
{
day++;
if(day==32)
day=1;
write_nyr(9,day);
value=(day)/10*16+(day)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x86,value);
write_ds1302(0x8e,0x80);
write_com(0x80+9);
}
if(s1num==6)
{
month++;
if(month==13)
month=1;
write_nyr(6,month);
value=(month)/10*16+(month)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x88,value);
write_ds1302(0x8e,0x80);
write_com(0x80+6);
}
if(s1num==7)
{
year++;
if(year==99)
year=0;
write_nyr(3,year);
value=(year)/10*16+(year)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x8c,value);
write_ds1302(0x8e,0x80);
write_com(0x80+3);
}
}
}
}
}
uchar BCD_Decimal(uchar bcd)
{
uchar Decimal;
Decimal=bcd>>4;
return(Decimal=Decimal*10+(bcd&=0x0F));
}
void main()
{
init();
ds1302_init();
while(1)
{
keyscan();
if(flag==0)
{
miao=BCD_Decimal(read_ds1302(0x81));
fen=BCD_Decimal(read_ds1302(0x83));
shi=BCD_Decimal(read_ds1302(0x85));
week=BCD_Decimal(read_ds1302(0x8b));
day=BCD_Decimal(read_ds1302(0x87));
month=BCD_Decimal(read_ds1302(0x89));
year=BCD_Decimal(read_ds1302(0x8d));
write_sfm(8,miao);
write_sfm(5,fen);
write_sfm(2,shi);
write_nyr(9,day);
write_nyr(6,month);
write_nyr(3,year);
write_week(week);
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit rs=P2^5; //LCD1602
sbit rw=P2^6;
sbit en=P2^7;
sbit sclk=P1^6; //DS1302
sbit ds1302_io=P3^5;
sbit rst=P1^7;
sbit s1=P3^2; //KEY
sbit s2=P3^3;
sbit s3=P3^4;
uchar code table1[]=" 20 - - ";
uchar code table2[]=" : : ";
uchar a,miao,shi,fen,day,month,year,week,flag,s1num,value;
void delay(uint z);
void write_ds1302(uchar command,uchar value);
//sbit ACC0=ACC^0;
//sbit ACC7=ACC^7;
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void init()
{
uchar num;
/*TMOD=0x11;
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;*/
en=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_data(table1[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<10;num++)
{
write_data(table2[num]);
delay(5);
}
}
void write_nyr(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 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 write_temp(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);
write_data(0xdf);
write_data(0x43);
} */
void write_week(uchar week)
{
write_com(0x80+12);
switch(week)
{
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('F');delay(5);
write_data('R');delay(5);
write_data('I');
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 ds1302_init()
{
rst=0;
sclk=0;
write_ds1302(0x8e,0x00);
write_ds1302(0x8e,0x80);
}
void write_ds1302(uchar command,uchar value)
{
uchar i=0;
rst=0;
sclk=0;
rst=1;
for(i=0;i<8;i++)
{
ds1302_io=command&0x01;
sclk=0;
sclk=1;
command>>=1;
}
for(i=0;i<8;i++)
{
ds1302_io=value&0x01;
sclk=0;
sclk=1;
value>>=1;
}
}
uchar read_ds1302(uchar command)
{
uchar i=0;
uchar value=0;
rst=0;
sclk=0;
rst=1;
for(i=0;i<8;i++)
{
ds1302_io=command&0x01;
sclk=0;
sclk=1;
command>>=1;
}
for(i=0;i<8;i++)
{
sclk=1;
sclk=0;
if(ds1302_io)
{
value|=0x01<<i;
}
}
rst=0;
//value = (value /16 * 10) + value %16;
return value;
}
void keyscan()
{
if(s1==0) //功能键按下
{
delay(5);
if(s1==0)
{
s1num++;
flag=1;
while(!s1);
if(s1num==1)
{
// TR0=0;
write_com(0x80+0x40+8); //miao
write_com(0x0f);
value=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,0x80|value);
write_ds1302(0x8e,0x80);
}
if(s1num==2) //fen
{
write_com(0x80+0x40+5);
}
if(s1num==3) //shi
{
write_com(0x80+0x40+2);
}
if(s1num==4) //week
{
write_com(0x80+12);
//write_com(0x0f);
}
if(s1num==5) //day
{
write_com(0x80+9);
}
if(s1num==6) //month
{
write_com(0x80+6);
}
if(s1num==7) //year
{
write_com(0x80+3);
}
if(s1num==8)
{
// TR0=1;
s1num=0;
write_com(0x0c);
flag=0;
value=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,0x80|value);
write_ds1302(0x8e,0x80);
}
}
}
if(s1num!=0)
{
if(s2==0) //数字增加键按下
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(8,miao);
value=(miao)/10*16+(miao)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,value);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+8);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(5,fen);
value=(fen)/10*16+(fen)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x82,value);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+5);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(2,shi);
value=(shi)/10*16+(shi)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x84,value);
write_ds1302(0x8e,0x80);
write_com(0x80+0x40+2);
}
if(s1num==4)
{
week++;
if(week==8)
week=1;
write_com(0x80+12);
write_week(week);
value=(week)/10*16+(week)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x8a,value);
write_ds1302(0x8e,0x80);
}
if(s1num==5)
{
day++;
if(day==32)
day=1;
write_nyr(9,day);
value=(day)/10*16+(day)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x86,value);
write_ds1302(0x8e,0x80);
write_com(0x80+9);
}
if(s1num==6)
{
month++;
if(month==13)
month=1;
write_nyr(6,month);
value=(month)/10*16+(month)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x88,value);
write_ds1302(0x8e,0x80);
write_com(0x80+6);
}
if(s1num==7)
{
year++;
if(year==99)
year=0;
write_nyr(3,year);
value=(year)/10*16+(year)%10;
write_ds1302(0x8e,0x00);
write_ds1302(0x8c,value);
write_ds1302(0x8e,0x80);
write_com(0x80+3);
}
}
}
}
}
uchar BCD_Decimal(uchar bcd)
{
uchar Decimal;
Decimal=bcd>>4;
return(Decimal=Decimal*10+(bcd&=0x0F));
}
void main()
{
init();
ds1302_init();
while(1)
{
keyscan();
if(flag==0)
{
miao=BCD_Decimal(read_ds1302(0x81));
fen=BCD_Decimal(read_ds1302(0x83));
shi=BCD_Decimal(read_ds1302(0x85));
week=BCD_Decimal(read_ds1302(0x8b));
day=BCD_Decimal(read_ds1302(0x87));
month=BCD_Decimal(read_ds1302(0x89));
year=BCD_Decimal(read_ds1302(0x8d));
write_sfm(8,miao);
write_sfm(5,fen);
write_sfm(2,shi);
write_nyr(9,day);
write_nyr(6,month);
write_nyr(3,year);
write_week(week);
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
http://bbs.elecfans.com/jishu_295866_1_1.html
我看你的程序,确实太 为难了点,没有备注,好费力啊看了下ds1302的驱动,觉得 读数据那个函数两个for循环之间,需要拉高ds1302_IO,驱动部分,没有发现其他问题.....
顺便问一下, 开始你不按键的情况下, ds1302正常走时么? 在你的ds1302init部分,么有看到你 启用外部晶振啊......应该不会走时, 会定在那儿的。
当你按键之后,你写 ds1302秒寄存器的时候, 这儿加上了 0x80, 即置了秒的自高位,激活了晶振
所以我觉得,你的程序,读出来ds1302后,直接显示秒的值,导致出错, 应该换算一下 ,要忽略 读出来 秒的值 的最高位。miao &=~0x80; 然后在送到显示屏 ,试试。另外,程序要是你自己写的,你应该可以发现ds1302秒寄存器的定义哟。
你的按键接的是单片机的P32和P33还有P34口,可以利用单片机的INT0(P32)和INT1(P33)两个外部中断来写按键处理程序。
还有就是你的DS1302驱动函数和我写的不大一样,能不能运行不好说,直接看看不出来。
给力