最近写了个1602液晶的显示和调整程序,有时间和年月日,星期,可是星期的调整总是出现问题,其他都好,想请大虾给指导指导,谢谢哦!
*******************************************
*******************************************
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]=" 2010-05-19 WEN";//15个字符,首字母空白1个
uchar code table1[]=" 00:00:00";//11个字符,首字母前空白3个
uchar code table2[]="MON";
uchar code table3[]="TUS";
uchar code table4[]="WEN";
uchar code table5[]="TUR";
uchar code table6[]="FRI";
uchar code table7[]="SAT";
uchar code table8[]="SUN";
sbit en=P3^4;
sbit rs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
sbit s1=P3^6;//功能键
sbit s2=P3^7;//数字调整键(增大)
uchar num,count,s1num,num2,temp;
char year,mon,day,shi,fen,miao,day1,week;
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);
en=1;//开启使能端
delay(5);
en=0;
}
void write_date(uchar date)//写数据函数
{
rs=1;//选择写数据方式
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void write_sfm(uchar add,uchar date)//写时分秒函数
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);//设置显示位置
write_date(0x30+shi);//送去液晶显示十位
write_date(0x30+ge);
}
void write_week(uchar num)
{
write_com(0x80+12);
switch(num)
{
case 1:for(num=0;num<3;num++)
{
write_date(table2[num]);
delay(20);
};break;
case 2:for(num=0;num<3;num++)
{
write_date(table3[num]);
delay(20);
};break;
case 3:for(num=0;num<3;num++)
{
write_date(table4[num]);
delay(20);
};break;
case 4:for(num=0;num<3;num++)
{
write_date(table5[num]);
delay(20);
};break;
case 5:for(num=0;num<3;num++)
{
write_date(table6[num]);
delay(20);
};break;
case 6:for(num=0;num<3;num++)
{
write_date(table7[num]);
delay(20);
};break;
case 7:for(num=0;num<3;num++)
{
write_date(table8[num]);
delay(20);
};break;
}
}
void init()
{
shi=0;
fen=0;
miao=0;
mon=1;
day=1;
count=0;
dula=0;
wela=0;
en=0;
write_com(0x38);//设置开1602显示器
write_com(0x0c);//设置开显示,且显示光标
write_com(0x06);//写一个字符后地址指针自动加1
write_com(0x01);//显示清0,数据指针清0
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(20);
}
write_com(0x80+0x40);
for(num=0;num<11;num++)
{
write_date(table1[num]);
delay(20);
}
TMOD=0x01;//设置定时器0工作模式1
TH0=(65536-45872)/256;//定时器装初值
TL0=(65536-45872)%256;
EA=1;//开总中断
ET0=1;//开定时器0中断
TR0=1;//启动定时器0
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+9);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+6);
}
if(s1num==3)
{
write_com(0x80+0x40+3);
}
if(s1num==4)
{
write_com(0x80+12);
}
if(s1num==5)
{
write_com(0x80+9);
}
if(s1num==6)
{
write_com(0x80+6);
}
if(s1num==7)
{
write_com(0x80+3);
}
if(s1num==8)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);//
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(0x40+9,miao);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(0x40+6,fen);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(0x40+3,shi);
}
if(s1num==4)
{
num2=1;
num2++;
if(num2==8)
num2=1;
write_week(num2);
write_com(0x80+12);
}
if(s1num==5)
{
day++;
if(day==31)
day=1;
write_sfm(9,day);
}
if(s1num==6)
{
mon++;
if(mon==13)
mon=1;
write_sfm(6,mon);
}
if(s1num==7)
{
year++;
if(year==100)
year=1;
write_sfm(3,year);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void time0()interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
day++;
day1++;
if(day==30)
{
day=0;
mon++;
if(mon==12)
{
mon=0;
year++;
if(year==99)
year=01;
write_sfm(3,year);
}
write_sfm(6,mon);
}
temp=day1%7;
write_week(temp);
write_sfm(9,day);
}
write_sfm(0x40+3,shi);
}
write_sfm(0x40+6,fen);
}
write_sfm(0x40+9,miao);
}
}
*************************************************
************************************************* |