1
今天刚刚看完郭老师的一个视频,照着视频自己编写的时钟函数。由于水平不高,导致程序太大,有一部分没有写全。希望各位高手指点一下,怎么节省内存空间。
#include
#define uchar unsigned char
#define uint unsigned int
***it dula=P2^6;
***it wela=P2^7;
***it rs=P3^5;
***it lcden=P3^4;
***it s1=P3^0;
***it s2=P3^1;
***it s3=P3^2;
***it rd=P3^7;
***it s4=P3^3;
uchar count,shi,fen,miao,s1num,week=0,i,day=1,yue=1,year1=0,year2,year3,year4;
uchar code table1[]=" 0000-01-01 MON"; //初始显示
uchar code table2[]=" 00:00:00";
uchar code table3[7][3]={"MON","TUE","WED","THU","FRI","STA","SUN"};//星期的编码
//*****延时函数
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//****写命令函数
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//****写入数据函数
void write_data(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//****初始化函数
void init()
{
uchar num;
// year=year1+year2*10+year3*100+year4*1000;
wela=0;
dula=0;
lcden=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(20);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_data(table2[num]);
delay(20);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
//*****显示时分秒
void write_sfm(uchar add,uchar date1)
{
uchar shi,ge;
shi=date1/10;
ge=date1%10;
write_com(add+0x80+0x40);
write_data(0x30+shi);
write_data(0x30+ge);
}
//*****显示年月日
void write_sfm1(uchar add,uchar date1)
{
uchar shi,ge;
shi=date1/10;
ge=date1%10;
write_com(add+0x80);
write_data(0x30+shi);
write_data(0x30+ge);
}
void key2();
void key3();
//******键盘扫描函数
void keyscan()
{
rd=0;
if(s1==0)//第一个键被按下
{
delay(5); //************去除抖动
if(s1==0)
{
s1num++; //***第一个键没被按下一次,s1num就加1
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) //被按下第四次,光标停在显示星期的位上
{
write_com(0x80+15);
}
if(s1num==5) //被按下第五次,光标停在显示日子的位上
{
write_com(0x80+10);
}
if(s1num==6) //被按下第六次,光标停在显示月的位上
{
write_com(0x80+7);
}
if(s1num==7) //被按下第七次,光标停在显示年的个位上
{
write_com(0x80+4);
}
if(s1num==8) //被按下第八次,光标停在十位上
{
write_com(0x80+3);
}
if(s1num==9) //被按下第九次,光标停在百位上
{
write_com(0x80+2);
}
if(s1num==10) //被按下第十次,光标停在千位上
{
write_com(0x80+1);
}
if(s1num==11) //被按下第十一次,时钟从设置的时刻开始
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if(s4==0) //第四个键被按下,时钟重新开始走
{
s1num=0;
write_com(0x0c);
TR0=1;
}
if(s1num!=0)
{
key2();
key3();
}
}
void key2() //调整时间加
{
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(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
week++;
switch(week)
{
case 0:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 1:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 2:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 3:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 4:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 5:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 6:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 7:write_com(0x80+12); week=0;
for(i=0;i<3;i++) write_data(table3[week]); break;
}
}
if(s1num==5)
{
if((yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||12)&&day==31)
day=0;
if((yue==2||yue==4||yue==6||yue==9||yue==11)&&day==30)
day=0;
day++;
write_sfm1(9,day);
write_com(0x80+10);
}
if(s1num==6)
{
if(yue==12)
yue=0;
yue++;
write_sfm1(6,yue);
write_com(0x80+7);
}
if(s1num==7)
{
if(year1==9)
{
year1=0;
write_com(0x80+4);
write_data(0x30+year1);
write_com(0x80+4);
}
else
{
year1++;
write_com(0x80+4);
write_data(0x30+year1);
write_com(0x80+4);
}
}
if(s1num==8)
{
if(year2==9)
{
year2=0;
write_com(0x80+3);
write_data(0x30+year2);
write_com(0x80+3);
}
else
{
year2++;
write_com(0x80+3);
write_data(0x30+year2);
write_com(0x80+3);
}
}
if(s1num==9)
{
if(year3==9)
{
year3=0;
write_com(0x80+2);
write_data(0x30+year3);
write_com(0x80+2);
}
else
{
year3++;
write_com(0x80+2);
write_data(0x30+year3);
write_com(0x80+2);
}
}
if(s1num==10)
{
if(year4==9)
{
year4=0;
write_com(0x80+1);
write_data(0x30+year4);
write_com(0x80+1);
}
else
{
year4++;
write_com(0x80+1);
write_data(0x30+year4);
write_com(0x80+1);
}
}
}
}
}
void key3() //调整时间减
{
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
if(miao==0)
{
miao=60;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
miao--;
write_sfm(10,miao);
write_com(0x80+0x40+11);
}
if(s1num==2)
{
if(fen==0)
{
fen=60;
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
fen--;
write_sfm(7,fen);
write_com(0x80+0x40+8);
}
if(s1num==3)
{
if(shi==0)
{
shi=24;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
shi--;
write_sfm(4,shi);
write_com(0x80+0x40+5);
}
if(s1num==4)
{
if(week==0)
{
week=7;
write_com(0x80+12);
for(i=0;i<3;i++)
write_data(table3[week]);
}
week--;
switch(week)
{
case 0:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 1:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 2:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 3:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 4:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 5:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 6:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
}
}
if(s1num==5)
{
day--;
if((yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||12)&&day==0)
{
day=31;
write_sfm1(9,day);
write_com(0x80+10);
}
else if((yue==2||yue==4||yue==6||yue==9||yue==11)&&day==0)
{
day=30;
write_sfm1(9,day);
write_com(0x80+10);
}
write_sfm1(9,day);
write_com(0x80+10);
}
if(s1num==6)
{
yue--;
if(yue==0)
{
yue=12;
write_sfm1(6,yue);
write_com(0x80+7);
}
write_sfm1(6,yue);
write_com(0x80+7);
}
if(s1num==7)
{
if(year1==0)
{
year1=9;
write_com(0x80+4);
write_data(0x30+year1);
write_com(0x80+4);
}
else
{
year1--;
write_com(0x80+4);
write_data(0x30+year1);
write_com(0x80+4);
}
}
if(s1num==8)
{
if(year2==0)
{
year2=9;
write_com(0x80+3);
write_data(0x30+year2);
write_com(0x80+3);
}
else
{
year2--;
write_com(0x80+3);
write_data(0x30+year2);
write_com(0x80+3);
}
}
if(s1num==9)
{
if(year3==0)
{
year3=9;
write_com(0x80+2);
write_data(0x30+year3);
write_com(0x80+2);
}
else
{
year3--;
write_com(0x80+2);
write_data(0x30+year3);
write_com(0x80+2);
}
}
if(s1num==10)
{
if(year4==0)
{
year4=9;
write_com(0x80+1);
write_data(0x30+year4);
write_com(0x80+1);
}
else
{
year4--;
write_com(0x80+1);
write_data(0x30+year4);
write_com(0x80+1);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
//定时器零的中断函数
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
day++;
/* week++;
switch(week)
{
case 0:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 1:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 2:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 3:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 4:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 5:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 6:write_com(0x80+12);
for(i=0;i<3;i++) write_data(table3[week]); break;
case 7:write_com(0x80+12); week=0;
for(i=0;i<3;i++) write_data(table3[week]); break;
}*/
if(((yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||12)&&(day==32))||((yue==2||yue==4||yue==6||yue==9||yue==11)&&(day==31)))
{
day=1;
yue++;
if(yue==13)
{
yue=1;
year1++;
if(year1==10)
{
year1=0;
year2++;
if(year2==10)
{
year2=0;
year3++;
if(year3==10)
{
year3=0;
year4++;
write_com(0x80+1);
write_data(0x30+year4);
}
write_com(0x80+2);
write_data(0x30+year3);
}
write_com(0x80+3);
write_data(0x30+year2);
}
write_com(0x80+4);
write_data(0x30+year1);
}
write_sfm1(6,yue);
}
write_sfm1(9,day);
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
|
|