实钟程序,通过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);
}
}
此帖出自
小平头技术问答
#include<reg52.h>
#include<24c02.h>
#define uint unsigned int
#define uchar unsigned char
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;
sbit deep=P2^3;
uchar code table[]=" 20 - - ";
uchar code table1[]=" : : ";
uchar t0,num,k1num;
char miao,shi,fen,nian,ye,ri,week;
void display(uchar shi,uchar ge);
void delay1(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;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
void write_sfm(uchar add,uchar date)
{
char 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)
{
char shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_week(char we)
{
write_com(0x80+12);
switch(we)
{
case 1:write_date('M');delay1(5);
write_date('O');delay1(5);
write_date('N');
break;
case 2:write_date('T');delay1(5);
write_date('U');delay1(5);
write_date('E');
break;
case 3:write_date('W');delay1(5);
write_date('E');delay1(5);
write_date('D');
break;
case 4:write_date('T');delay1(5);
write_date('H');delay1(5);
write_date('U');
break;
case 5:write_date('F');delay1(5);
write_date('R');delay1(5);
write_date('I');
break;
case 6:write_date('S');delay1(5);
write_date('A');delay1(5);
write_date('R');
break;
case 7:write_date('S');delay1(5);
write_date('U');delay1(5);
write_date('N');
break;
}
}
void init()
{
uchar num;
dula=0;
wela=0;
lcden=0;
week=1;
ye=1;
ri=1;
t0=0;
k1num=0;
init_24c02( );
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]);
delay1(1);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay1(1);
}
miao=read_add(1);
fen=read_add(2);
shi=read_add(3);
ri=read_add(4);
ye=read_add(5);
nian=read_add(6);
week=read_add(7);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
write_nyr(9,ri);
write_nyr(6,ye);
write_nyr(3,nian);
write_week(week);
TMOD=0x01;
TH0=(65536-46083)/256;
TL0=(65536-46083)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan( )
{
rd=0;
if(key1==0)
{
delay1(5);
if(key1==0)
{
k1num++;
while(!key1);
switch(k1num)
{
case 1: TR0=0;
write_com(0x80+3);
write_com(0x0f);
break;
case 2: write_com(0x80+6);
break;
case 3: write_com(0x80+9);
break;
case 4: write_com(0x80+12);
break;
case 5: write_com(0x80+0x40+10);
break;
case 6: write_com(0x80+0x40+7);
break;
case 7: write_com(0x80+0x40+4);
break;
case 8: k1num=0;
write_com(0x0c);
TR0=1;
break;
}
}
}
if(k1num!=0)
{
if(key2==0)
{
delay1(5);
if(key2==0)
{
while(!key2);
switch(k1num)
{
case 1: nian++;
if(nian==100)
nian=0;
write_nyr(3,nian);
write_com(0x80+3);
write_add(6,nian);
break;
case 2: ye++;
if(ye==13)
ye=1;
write_nyr(6,ye);
write_com(0x80+6);
write_add(5,ye);
break;
case 3: ri++;
if(ri==32)
ri=1;
write_nyr(9,ri);
write_com(0x80+9);
write_add(4,ri);
break;
case 4: week++;
if(week==8)
week=1;
write_week(week);
write_com(0x80+12);
write_add(7,week);
break;
case 5: miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(1,miao);
break;
case 6: fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(2,fen);
break;
case 7: shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(3,shi);
break;
}
}
}
if(key3==0)
{
delay1(5);
if(key3==0)
{
while(!key3);
switch(k1num)
{
case 1: nian--;
if(nian==-1)
nian=99;
write_nyr(3,nian);
write_com(0x80+3);
write_add(6,nian);
break;
case 2: ye--;
if(ye==0)
ye=12;
write_nyr(6,ye);
write_com(0x80+6);
write_add(5,ye);
break;
case 3: ri--;
if(ri==0)
ri=31;
write_nyr(9,ri);
write_com(0x80+9);
write_add(4,ri);
break;
case 4: week--;
if(week==0)
week=7;
write_week(week);
write_com(0x80+12);
write_add(week,7);
break;
case 5: miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(1,miao);
break;
case 6: fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(2,fen);
break;
case 7: shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(3,shi);
break;
}
}
}
}
}
void main( )
{
init( );
while(1)
{
keyscan( );
}
}
void timer0( ) interrupt 1
{
TH0=(65536-46083)/256;
TL0=(65536-46083)%256;
t0++;
write=1;
if(t0==20)
{
t0=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
ri++;
week++;
if(week==8);
week=1;
if(ri==31)
{
ri=1;
ye++;
if(ye==12)
{
ye=1;
nian++;
write_nyr(3,nian);
write_add(6,nian);
}
write_nyr(6,ye);
write_add(5,ye);
}
write_nyr(9,ri);
write_add(4,ri);
write_week(week);
write_add(7,week);
}
write_sfm(4,shi);
write_add(3,shi);
}
write_sfm(7,fen);
write_add(2,fen);
}
write_sfm(10,miao);
write_add(1,miao);
}
}
24c02程序如下:
#define uint unsigned int
#define uchar unsigned char
bit write;
sbit sda=P2^0;
sbit scl=P2^1;
void delay( )
{ ;; }
void start( )
{
sda=1;
delay( );
scl=1;
delay( );
sda=0;
delay( );
}
void stop( )
{
sda=0;
delay( );
scl=1;
delay( );
sda=1;
delay( );
}
void respons( )
{
uchar i;
scl=1;
delay( );
while((sda==1)&&(i<250))i++;
scl=0;
delay( );
}
void init_24c02( )
{
sda=1;
delay( );
scl=1;
delay( );
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
scl=0;
delay( );
for(i=0;i<8;i++)
{
temp=temp<<1;
sda=CY;
delay( );
scl=1;
delay( );
scl=0;
delay( );
}
sda=1;
delay( );
}
uchar read_byte( )
{
uchar i,k;
scl=0;
delay( );
sda=1;
delay( );
for(i=0;i<8;i++)
{
scl=1;
delay( );
k=(k<<1)|sda;
scl=0;
delay( );
}
return k;
}
void write_add(uchar address,uchar date)
{
start( );
write_byte(0xa0);
respons( );
write_byte(address);
respons( );
write_byte(date);
respons( );
stop( );
}
uchar read_add(uchar address)
{
uchar date;
start( );
write_byte(0xa0);
respons( );
write_byte(address);
respons( );
start( );
write_byte(0xa1);
respons( );
date=read_byte( );
stop( );
return date;
}
一周热门 更多>