誰能幫我看看程序 按鍵的時候 有問題 不顯示時間#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar hour= 23,min=59,sec=30;
uint aa;
uint bzw=0;
sbit k1 = P3^0;
sbit k2 = P3^1;
sbit k3 = P3^2;
sbit k4 = P3^3;
sbit k5 = P3^4;
sbit DELL=P1^5;
uchar code seg_data[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5c,0x79,0x71,0X40};
/********以下是延時函數********/
void Delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--) //i=xms即延時約xms毫秒
for(j=110;j>0;j--);
}
void beep()
{
int i;
for(i=0;i<500;i++)
{
DELL=~DELL;
Delay(1);
}
}
void Display()
{
P0=0x00;
P0=seg_data[sec%10];
P2=0xfe;
Delay(1);
P0=0x00;
P0=seg_data[sec/10];
P2=0xfd;
Delay(1);
P0=0x40;
P2=0xfb; // 顯示--
Delay(1);
P0=0x00;
P0=seg_data[min%10];
P2=0xf7;
Delay(1);
P0=0x00;
P0=seg_data[min/10];
P2=0xef;
Delay(1);
P0=0x40;
P2=0xdf; // 顯示--
Delay(1);
P0=0x00;
P0=seg_data[hour%10];
P2=0xbf;
Delay(1);
P0=0x00;
P0=seg_data[hour/10];
P2=0x7f;
Delay(1);
}
void tiao()
{
TR0=0;
if(k2==0) //分加
{
Display(); //代替延時
if(k2==0)
Display();
while(!k2);
Display();min++;
if(min==60)
min=0;
}
if(k3==0) //時加
{
Display();
if(k3==0)
Display();
while(!k3);
hour++;Display();
if(hour==24)
hour=0;
}
if(k4==0) //啟動TR0
{
Display();
if(k4==0)
Display();
while(!k4);
Display();
TR0=1;
bzw=0;
}
}
void init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void tirme0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
aa=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
sec=0;min=0;hour=0;
}
}
}
}
}
void main()
{
init();
while(1)
{
if(k1==0)
{
Display();
if(k1==0)
Display();
while(!k1)
Display();bzw=1;
}
if(bzw==1) tiao();
Display();
}
}
|