|
老師布置的作業,整了一天沒弄明白,是不是缺少刷新啊?
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
uchar num=0,temp=0,count=0;
uchar aa;
uchar hour,min,sec;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
void delay(uint z);
void display(uchar a,uchar b,uchar ab,uchar c,uchar d,uchar cd,uchar e,uchar f,uchar aa);
void read_key();
void time_change();
/***************主函數***************/
void main()
{
P2=0xff,
hour=00;
min=0;
sec=0;
TMOD=0x1l;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
aa=0xff;
while(1)
{
display(table[hour/10],table[hour%10],table[10],table[min/10],
table[min%10],table[10],table[sec/10],table[sec%10],0Xff);
time_change();
}
}
/****************顯示函數*****************/
void display(uchar a,uchar b,uchar ab,uchar c,uchar d,uchar cd,uchar e,uchar f,uchar aa)
{
if(num==1){P1=0x01 &aa;P0=a;delay(2);}else{P1=0x01;P0=a;delay(2);} //hour
if(num==1){P1=0x02&aa;P0=b;delay(2);}else{P1=0x02;P0=b;delay(2);} //hour
P1=0x04;P0=ab;delay(2);
if(num==2){P1=0x08&aa;P0=c;delay(2);}else{P1=0x08;P0=c;delay(2);} //min
if(num==2){P1=0x10&aa;P0=d;delay(2);}else{P1=0x10;P0=d;delay(2);} //min
P1=0x20;P0=cd;delay(2);
if(num==3){P1=0x40&aa;P0=e;delay(2);}else{P1=0x40;P0=e;delay(2);} //sec
if(num==3){P1=0x80&aa;P0=f;delay(2);}else{P1=0x80;P0=f;delay(2);} //sec
}
/************定時器0中斷函數************/
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
temp++;
}
/**************定時器1中斷函數************/
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
count++;
if(count>=20)
count=0;
}
/**************延時子函數***************/
void delay(uint z)
{
uint j,k;
for(j=z;j>0;j--)
for(k=120;k>0;k--);
}
/***************讀按鍵函數***************/
void read_key()
{
if(key1=0)
{
delay(100);
if(key1==0)
{
delay(100);
num++;
if(num>3){num=0;}
while(1)
{
if(key1==0)
{
delay(10);
if(key1=0)
{
num++;
if(num>3){num=0;break;}
}
while(!key1);
delay(10);
while(!key1);
}
if(key2==0)
{
delay(100);
if(key2==0)
{
if(num==1){hour++;if(hour==24)hour=0;}
if(num==2){min++;if( min==60)min=0;}
if(num==3){sec++;if(sec==60)sec=0;}
}
while(!key1);
delay(100);
while(!key1);
}
if(key3==0)
{
delay(100);
if(key3==0)
{
if(num==1){hour--;if(hour==0)hour=23;}
if(num==2){min--;if(min==0)min=59;}
if(num==3){sec--;if(sec==0)sec=59;}
}
while(!key1);
delay(10);
while(!key1);
}
if(count<=15)
display(table[hour/10],table[hour%10],table[10],table[min/10],
table[min%10],table[10],table[sec/10],table[sec%10],0xff);
if(count>15)
display(table[hour/10],table[hour%10],table[10],table[min/10],
table[min%10],table[10],table[sec/10],table[sec%10],0x00);
}
}
while(!key1);
delay(10);
while(!key1);
}
}
/*************時間調整函數**************/
void time_change()
{
read_key0();
if(temp>=20)
{
temp=0;
sec++;
if(sec>=60)
{
sec=0;
min++;
if(min>=60)
{
min=0;
hour++;
if(hour>=24)
{
hour=0;
}
}
}
}
}
|
|