51i单片机C语言编写电子时钟,求大神给看看哪里错了?
时间:10-02
整理:3721RD
点击:
我是单片机新手,用的是郭天祥的单片机,写了一个简单可调的电子时钟,可是我不能调,用了好久找错误都无果,求大神看看,你们分分钟就搞定了。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit rs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
//sbit rd=P3^7;
uchar code table[]=" 2015-05-16 SAT";
uchar code table1[]=" 00:00:00";
uchar num,s1num;
char shi,fen,miao;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
rs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
// s1=1;
// s2=1;
// s3=1;
dula=0;
wela=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_data(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_data(table1[num]);
delay(5);
}
TMOD=0X01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=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 T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
void keyscan()
{
RD=0;
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
s1num=0;
TR0=1;
write_com(0x0c);
}
}
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
{
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
}
if(s1num==2)
{
fen++;
if(fen==60)
{
fen=0;
write_sfm(10,fen);
write_com(0x80+0x40+8);
}
}
if(s1num==3)
{
shi++;
if(shi==24)
{
shi=0;
write_sfm(10,shi);
write_com(0x80+0x40+5);
}
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3)
{
if(s1num==1)
{
miao--;
if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen--;
if(fen==0)
{
fen=59;
write_sfm(10,fen);
write_com(0x80+0x40+8);
}
write_sfm(10,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi--;
if(shi==0)
{
shi=23;
write_sfm(10,shi);
write_com(0x80+0x40+5);
}
write_sfm(10,shi);
write_com(0x80+0x40+5);
}
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit rs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
//sbit rd=P3^7;
uchar code table[]=" 2015-05-16 SAT";
uchar code table1[]=" 00:00:00";
uchar num,s1num;
char shi,fen,miao;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
rs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
// s1=1;
// s2=1;
// s3=1;
dula=0;
wela=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_data(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_data(table1[num]);
delay(5);
}
TMOD=0X01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=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 T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
void keyscan()
{
RD=0;
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
s1num=0;
TR0=1;
write_com(0x0c);
}
}
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
{
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
}
if(s1num==2)
{
fen++;
if(fen==60)
{
fen=0;
write_sfm(10,fen);
write_com(0x80+0x40+8);
}
}
if(s1num==3)
{
shi++;
if(shi==24)
{
shi=0;
write_sfm(10,shi);
write_com(0x80+0x40+5);
}
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3)
{
if(s1num==1)
{
miao--;
if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
fen--;
if(fen==0)
{
fen=59;
write_sfm(10,fen);
write_com(0x80+0x40+8);
}
write_sfm(10,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi--;
if(shi==0)
{
shi=23;
write_sfm(10,shi);
write_com(0x80+0x40+5);
}
write_sfm(10,shi);
write_com(0x80+0x40+5);
}
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
怎么木有人
同时菜鸟想问一下怎样不能调节?