求一份用1602,AT89C52,DS1302做的时钟的C语言代码,或者求哪位大侠帮我看看这个程序有什么问题吧。。。

2024-12-20 11:43:53
推荐回答(1个)
回答1:

#include
#define uint unsigned int
#define uchar unsigned char
sbit ACC_7=ACC^7; //位寻址寄存器定义
sbit rs=P3^5;
sbit en=P3^4;
sbit sclk=P2^0;
sbit io=P2^5;
sbit ce=P2^4;
sbit wd18b20=P2^7;
sbit beep=P1^7;
sbit led=P3^7;
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
sbit s4=P1^3;
sbit s5=P1^4;
char yue,ri,shi,fen,miao;
uchar s1num=0,a=0; //a是s1num所对应的结果
uchar b=0x80+0x40;
uchar code table1[5]={
0x56,0x34,0x12,0x22,0x02};
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=114;j>0;j--);
}
void write_com(uchar com) //1602写命令
{
rs=0;
en=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_data(uchar dat) //1602写数据
{
rs=1;
en=0;
P0=dat;
delay(5);
en=1;
delay(5);
en=0;
}
void w1302(uchar dz,uchar sj) //1302写,先地址,后数据
{
uchar i,j;
ce=0;
sclk=0;
ce=1;
for(i=8;i>0;i--)
{
sclk=0;
j=dz;
io=(bit)(j&0x01);
dz>>=1;
sclk=1;
}
for(i=8;i>0;i--)
{
sclk=0;
j=sj;
io=(bit)(j&0x01);
dz>>=1;
sclk=1;
}
ce=0;
}
uchar r1302(uchar dz) //读取数据函数
{
uchar i,j,sj1,sj2;
ce=0;
sclk=0;
ce=1;
for(i=8;i>0;i--)
{
sclk=0;
j=dz;
io=(bit)(j&0x01);
dz>>=1;
sclk=1;
}
for(i=8;i>0;i--)
{

ACC_7=io;
sclk=1;
ACC>>=1;
sclk=0;
}
ce=0;
sj1=ACC;
sj2=sj1/16;
sj1=sj1%16;
sj1=sj1+sj2*10;
return(sj1);
}
void init() //初始化函数
{
uchar a,b=0x80;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01); //1602初始化
write_com(0x80+0x40+2);
write_data('/');
write_com(0x80+0x40+5);
write_data('-');
write_com(0x80+0x40+8);
write_data(':');
write_com(0x80+0x40+11);
write_data(':');
w1302(0x8e,0x00); //1302初始化
for(a=0;a<5;a++)
{
w1302(b,table1[a]);
b+=2;
}
w1302(0x8e,0x80);
}
void shuax(uchar dz1602,uchar dz1302) //刷新时间函数
{
uchar sj,shi,ge;
sj=dz1302;
shi=sj/10;
ge=sj%10;
write_com(dz1602);
write_data(0x30+shi);
write_data(0x30+ge);
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1);
switch(s1num)
{
case 1:a=1;
write_com(0x80);
write_data('y');
break;
case 2:s1num=0;
a=0;
write_com(0x0c);
write_com(0x80);
write_data(' ');
w1302(0x8e,0x00);
w1302(0x80,miao);
w1302(0x82,fen);
w1302(0x84,shi);
w1302(0x86,ri);
w1302(0x88,yue);
w1302(0x8e,0x80);
break;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
b=b+3;
write_com(b);
write_com(0x0f);
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
b=b-3;
write_com(b);
write_com(0x0f);
}
}
if(s4==0)
{
delay(5);
if(s4==0)
{
while(!s4);
switch(b)
{
case 0x80+0x40:yue++;
if(yue==13)
yue=1;
w1302(0x8e,0x00);
w1302(0x88,yue);
w1302(0x8e,0x80);
shuax(0x80+0x40,yue);
break;
case 0x80+0x40+3:ri++;
if(ri==32)
ri=1;
w1302(0x8e,0x00);
w1302(0x86,ri);
w1302(0x8e,0x80);
shuax(0x80+0x40+3,ri);
break;
case 0x80+0x40+6:shi++;
if(shi==24)
shi=0;
w1302(0x8e,0x00);
w1302(0x84,shi);
w1302(0x8e,0x80);
shuax(0x80+0x40+6,shi);
break;
case 0x80+0x40+9:fen++;
if(fen==60)
fen=0;
w1302(0x8e,0x00);
w1302(0x82,fen);
w1302(0x8e,0x80);
shuax(0x80+0x40+9,fen);
break;
case 0x80+0x40+12:miao++;
if(miao==60)
miao=0;
w1302(0x8e,0x00);
w1302(0x80,miao);
w1302(0x8e,0x80);
shuax(0x80+0x40+12,miao);
break;
}
}
}
if(s5==0)
{
delay(5);
if(s5==0)
{
while(!s5);
switch(b)
{
case 0x80+0x40:yue--;
if(yue==0)
yue=12;
shuax(0x80+0x40,yue);
break;
case 0x80+0x40+3:ri--;
if(ri==0)
ri=31;
shuax(0x80+0x40+3,ri);
break;
case 0x80+0x40+6:shi--;
if(shi==-1)
shi=23;
shuax(0x80+0x40+6,shi);
break;
case 0x80+0x40+9:fen--;
if(fen==-1)
fen=59;
shuax(0x80+0x40+9,fen);
break;
case 0x80+0x40+12:miao--;
if(miao==-1)
miao=59;
shuax(0x80+0x40+12,miao);
break;
}
}
}
}
}
void main()
{
init();
while(1)
{

yue=r1302(0x89);
ri=r1302(0x87);
shi=r1302(0x85);
fen=r1302(0x83);
miao=r1302(0x81);
keyscan();
shuax(0x80+0x40,yue);
shuax(0x80+0x40+3,ri);
shuax(0x80+0x40+6,shi);
shuax(0x80+0x40+9,fen);
shuax(0x80+0x40+12,miao);
}
}