请高手们指点一下,还有什么需要改进的地方,程序如下:
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define TH0_BIT TH0=(65536-46080)/256
#define TL0_BIT TL0=(65536-46080)%256
#define delay_ms delay_1ms(5)//1ms延时函数
***it dula=P2^6;
***it wela=P2^7;
***it rs=P3^5;//液晶的片选
***it lcdwr=P3^6;//液晶的读写
***it lcden=P3^4;//液晶的使能
uchar aa,num,mon,day,year;//定义天,月,年,一个键盘记值
char m,s,f,xq;//定义时分秒和星期
uchar code table[]=" 2000-01-01 MON";//初始液晶显示的东西
uchar code table1[]=" 00:00:00";
uchar code table2[]="MON TUE WED THU FRI SAT SUN";//定义了星期的一个表
void delay_1ms(unsigned int cc)//1ms延时函数
{
unsigned int cd,ci;
for (cd = cc;cd > 0;cd -- )
for (ci = 100;ci > 0;ci -- );
}
void write_com(uchar com)//液晶写命令程序
{
rs=0;
lcdwr=0;
lcden=0;
P0=com;
delay_ms;
lcden=1;
delay_ms;;
lcden=0;
}
void write_date(uchar date)//液晶写数据程序
{
rs=1;
lcdwr=0;
lcden=0;
P0=date;
delay_ms;
lcden=1;
delay_ms;
lcden=0;
}
void init()//初始化液晶和定时器
{
uchar num;
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_date(table[num]);
delay_ms;
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay_ms;
}
TMOD=0x01;
TH0_BIT;
TL0_BIT;
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_date(0x30+shi);
write_date(0x30+ge);
}
void write_xingqi(uchar b)//修改星期的程序
{
uchar num;
write_com(0x80+12);
for(num=b;num<(b+3);num++)
write_date(table2[num]);
}
void write_nyr(uchar ad,uchar dat)//修改年月日的程序
{
uchar sh,g;
sh=dat/10;
g=dat%10;
write_com(0x80+ad);
write_date(0x30+sh);
write_date(0x30+g);
}
key_bit()
{
uchar key,L,R;
P1 = 0x0f;
if(P1 != 0x0f)
{
delay_ms;
if(P1 != 0x0f)
{
L = P1&0x0f;
P1=0xf0;
R=P1&0xf0;
key=L|R;
while(P1 !=0xf0);
switch(key)
{
case 0xee: key=1;break;
case 0xde: key=2;break;
case 0xbe: key=3;break;
case 0x7e: key=4;break;
case 0xed: key=5;break;
case 0xdd: key=6;break;
case 0xbd: key=7;break;
case 0x7d: key=8;break;
case 0xeb: key=9;break;
case 0xdb: key=10;break;
case 0xbb: key=11;break;
case 0x7b: key=12;break;
case 0xe7: key=13;break;
case 0xd7: key=14;break;
case 0xb7: key=15;break;
case 0x77: key=16;break;
default: key=88;break;
}
}
if(key==1)
{
num++;//对功能键值加1
if(num==1)//按下功能键停止记时并分别用光标指向时分秒年月日
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(num==2)
write_com(0x80+0x40+7);
if(num==3)
write_com(0x80+0x40+4);
if(num==4)
write_com(0x80+9);
if(num==5)
write_com(0x80+6);
if(num==6)
write_com(0x80+3);
if(num==7)
write_com(0x80+12);
if(num==8)//最后再按下功能键确认开始重新记时
{
num=0;
write_com(0x0c);
TR0=1;
}
}
if(num!=0)
{
if(key==2)
{
if(num==1)//秒加1
{
m++;
if(m==60)
m=0;
write_sfm(10,m);
write_com(0x80+0x40+10);
}
if(num==2)//分加1
{
f++;
if(f==60)
f=0;
write_sfm(7,f);
write_com(0x80+0x40+7);
}
if(num==3)//小时加1
{
s++;
if(s==24)
s=0;
write_sfm(4,s);
write_com(0x80+0x40+4);
}
if(num==4)//天加1
{
day++;
if(day==31)
day=1;
write_nyr(9,day);
write_com(0x80+9);
}
if(num==5)//月加1
{
mon++;
if(mon==13)
mon=1;
write_nyr(6,mon);
write_com(0x80+6);
}
if(num==6)//年加1
{
year++;
if(year==100)
year=0;
write_nyr(3,year);
write_com(0x80+1);
}
if(num==7)//星期加1
{
xq=xq+4;
if(xq==28)
xq=0;
write_xingqi(xq);
write_com(0x80+12);
}
}
}
if(key==3)
{
if(num==1)//秒减1
{
m--;
if(m==-1)
m=59;
write_sfm(10,m);
write_com(0x80+0x40+10);
}
if(num==2)//分减1
{
f--;
if(f==-1)
f=59;
write_sfm(7,f);
write_com(0x80+0x40+7);
}
if(num==3)//小时减1
{
s--;
if(s==-1)
s=23;
write_sfm(4,s);
write_com(0x80+0x40+4);
}
if(num==4)//天减1
{
day--;
if(day==0)
day=30;
write_nyr(9,day);
write_com(0x80+9);
}
if(num==5)//月减1
{
mon--;
if(mon==0)
mon=12;
write_nyr(6,mon);
write_com(0x80+6);
}
if(num==6)//年减1
{
year--;
if(year==-1)
year=99;
write_nyr(3,year);
write_com(0x80+3);
}
if(num==7)//星期减1
{
xq=xq-4;
if(xq==-4)
xq=24;
write_xingqi(xq);
write_com(0x80+12);
}
}
}
return key; /*返回值为:按键码*/
}
void main ()
{
init();
while(1)
{
key_bit();
}
}
void timer0() interrupt 1//用定时器0来记时
{
TH0_BIT;
TL0_BIT;
aa++;
if(aa==18)//一秒到中断
{
aa=0;
m++;
if(m==60)
{
m=0;
f++;
if(f==60)
{
f=0;
s++;
if(s==24)
{
day++;
xq=xq+4;
s=0;
if(xq==27)
xq=0;
write_xingqi(xq);
if(day==31)
{
mon++;
day=1;
if(mon==12)
{
year++;
if(year==100)
year=0;
mon=1;
write_nyr(3,year);
}
write_nyr(6,mon);
}
write_nyr(9,day);
}
write_sfm(4,s);
}
write_sfm(7,f);
}
write_sfm(10,m);
}
}
|