实钟程序,通过89C52让1602液晶显示出实钟,通过key1键(功能键)设置日期,时间,key2键为按一次时间或日期加1,key3为按一次时间或日期减1,现在,更改日期时间还有时间走秒走分走时都没问题,就是小时每到24小时后星期不知道要怎么显示往前进一天,我只能实现经过24小时后星期往前进一天,但是再过24后,星期就不往前进一天了,不知道要嵌套在哪里,有没有帮帮忙的,程序如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit rs=P3^5;
sbit lcden=P3^4;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit rd=P3^7;
uchar tt,num,a,b,k1num;
char miao,shi,fen,nian,ye,ri;
uchar code table[]=" 2000-01-01 SAR";
uchar code table1[]=" 00:00:00";
uchar code table2[]="SUN";
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;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
uchar num;
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%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_date(0x30+shi);
write_date(0x30+ge);
}
void write_nyr(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 keyscan( )
{
rd=0;
if(key1==0)
{
delay(5);
if(key1==0)
{ k1num++;
while(!key1);
if(k1num==1)
{
TR0=0;
write_com(0x80+5);
write_com(0x0f);
}
if(k1num==2)
{
write_com(0x80+8);
}
if(k1num==3)
{
write_com(0x80+11);
}
if(k1num==4)
{
write_com(0x80+0x40+10);
}
if(k1num==5)
{
write_com(0x80+0x40+7);
}
if(k1num==6)
{
write_com(0x80+0x40+4);
}
if(k1num==7)
{
k1num=0;
write_com(0x0c);
TR0=1;
}
}
}
if(k1num!=0)
{
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(k1num==1)
{
nian++;
write_nyr(4,nian);
write_com(0x80+5);
}
if(k1num==2)
{
ye++;
if(ye==13)
ye=1;
write_nyr(7,ye);
write_com(0x80+8);
}
if(k1num==3)
{
ri++;
if(ri==32)
ri=1;
write_nyr(10,ri);
write_com(0x80+11);
}
if(k1num==4)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(k1num==5)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(k1num==6)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
if(k1num==2)
{
ye--;
if(ye==0)
ye=12;
write_nyr(7,ye);
write_com(0x80+8);
}
if(k1num==3)
{
ri--;
if(ri==0)
ri=31;
write_nyr(10,ri);
write_com(0x80+11);
}
if(k1num==4)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(k1num==5)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(k1num==6)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt==18)
{
tt=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
ri++;
write_com(0x80+13);
for(a=0;a<3;a++)
{
write_date(table2[a]);
delay(5);
}
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
}
此帖出自
小平头技术问答
LZ,你这程序有好几个问题
1、数据没初始化,你LCD是1月1日,但是ye(不应该是yue吗)和ri的值却是0,其实建议变量用英文别用中文拼音
2、调日期没法自动对准星期,
3、你直接默认当天是SAR(星期六不是SAT吗),下一天是SUN了,所以你table2只有SUN啊,你怎么可能输出其余的日期·····
4、竟然每个月都31天,这个问题都没注意,估计调整年月可能出现的日溢出也没考虑吧
5、不知道为啥你年不设置减的,不减就不减吧,也没事
6、时钟过0点,日期不会变
修改程序如下:
[ 本帖最后由 孤獨の快樂 于 2011-10-17 13:56 编辑 ]一周热门 更多>