//*************************C語言寫的可調數字時鐘顯示 *************************
//*******************************2009-09-13***********************************
//***************************可調數字鐘的功能介紹*******************************
//K1:adjtime K2:hour K3:min K4:OK
//K2:lookalarm K4:OK
//K3:adjalarm K2:hh K3:mm K4:OK
//K4:SET alarm OFF/ON
//****************************************************************************
#include"reg51.h"
unsigned char ss=0,hour=12,min=0,hh=7,mm=30; //初始化 顯示12:00鬧鈴:7:30
unsigned char code leds_a[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0-9
unsigned char code leds_b[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //帶點的0-9
sbit P37=P3^7; //蜂鳴器開關
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3; //數碼管選位
#define K1 0xde
#define K2 0xee
#define K3 0xf6
#define K4 0xdd //按鍵的分別代碼
unsigned char key;
TIMER_INTO();
unsigned char kbscan(); //鍵盤掃描
void lookalarm(); //查看鬧鈴
void disp_off(); //鬧鈴關的顯示
void process(); //有按鍵按下的處理
void disp_on(); //鬧鈴開的顯示
void alarm(); //鬧鈴
unsigned char flag_alarm=1; //鬧鐘標志位,默認鬧鈴關
static unsigned int count=0; //計數器初始
unsigned char dema=0; //數碼管顯示的延時標中斷志
unsigned char atemp=0;
unsigned char demb=0; //按鍵的延時中斷標志
unsigned char btemp=0;
main() //主程序
{
TIMER_INTO(); //定時 中斷 初始化
while(1)
{
key=kbscan();//掃描鍵盤
process();
if(flag_alarm==1)
{
disp_off();//關閉鬧鐘的顯示
}
else
{
disp_on(); //鬧鐘開啟 的顯示
}
alarm(); //鬧鈴
}
}
void process() //按鍵的處理
{
if(key!=0)
{
if(key==K1)
{
TR0=0;
while((key=kbscan())!=K4)
{
disp_off();
if((key=kbscan())==K2)
{
hour++;
disp_off();
if(hour>=24)
{
hour=0;
}
}
if((key=kbscan())==K3)
{
min++;
if(min>=60)
{
min=0;
}
}
}
TR0=1;
}
else if(key==K2)
{
while(((key=kbscan())!=K4))
{
lookalarm();
}
}
else if(key==K3)
{
while((key=kbscan())!=K4)
{
lookalarm();
if((key=kbscan())==K2)
{
hh++;
lookalarm();
if(hh>=24)
{
hh=0;
}
}
if((key=kbscan())==K3)
{
mm++;
if(mm>=60)
{
mm=0;
}
}
}
}
else if(key==K4)
{
flag_alarm=~flag_alarm;
}
}
}
TIMER_INTO() //中斷初始化
{
TMOD=0X01;
TH0=0Xf8;
TL0=0X30; //2mS初值
TR0=1;
ET0=1;
EA=1;
}
timer0() interrupt 1 //中斷進程
{
TR0=0;
TH0=0Xf8;
TL0=0X30; //重裝初值
TR0=1;
count++;
dema++;
demb++;
if(dema>1)
{
dema=0; //2mS延時
atemp=1;
}
if(demb>=15)
{
demb=0; //30mS延時
btemp=1;
}
if(count>=500) //1S延時
{
count=0;
ss++;
if(ss>=60)
{
ss=0;
min++;
if(min>=60)
{
min=0;
hour++;
if(hour>=24)
{
hour=0;
}
}
}
}
}
void disp_off() //鬧鈴關的走時
{
P23=0;
P0=leds_a[min%10];
dema = 0;
atemp=0;
while(atemp!=1);
P23=1;
P22=0;
P0=leds_a[min/10];
dema = 0;
atemp=0;
while(atemp!=1);
P22=1;
P21=0;
P0=leds_b[hour%10];
dema = 0;
atemp=0;
while(atemp!=1);
P21=1;
P20=0;
P0=leds_a[hour/10];
dema = 0;
atemp=0;
while(atemp!=1);
P20=1;
}
void disp_on() //鬧鈴開的走時
{
P23=0;
P0=leds_b[min%10];
dema = 0;
atemp=0;
while(atemp!=1);
P23=1;
P22=0;
P0=leds_a[min/10];
dema = 0;
atemp=0;
while(atemp!=1);
P22=1;
P21=0;
P0=leds_b[hour%10];
dema = 0;
atemp=0;
while(atemp!=1);
P21=1;
P20=0;
P0=leds_a[hour/10];
dema = 0;
atemp=0;
while(atemp!=1);
P20=1;
}
void lookalarm() //查看鬧鐘
{
P23=0;
P0=leds_a[mm%10];
dema = 0;
atemp=0;
while(atemp!=1);
P23=1;
P22=0;
P0=leds_a[mm/10];
dema = 0;
atemp=0;
while(atemp!=1);
P22=1;
P21=0;
P0=leds_b[hh%10];
dema = 0;
atemp=0;
while(atemp!=1);
P21=1;
P20=0;
P0=leds_a[hh/10];
dema = 0;
atemp=0;
while(atemp!=1);
P20=1;
}
unsigned char kbscan() //掃描鍵盤
{
unsigned char sccode,recode,temp;
P1=0XF8;
temp=P1;
if(temp!=0xf8)
{
while(btemp!=1);
btemp=0;
if(temp==P1)
{
sccode=0xfe;
while((sccode&0xf7)!=0)
{
P1=sccode;
if((P1&0xf8)!=0xf8)
{
recode=P1&0Xf8;
sccode=sccode&0xc7;
return(recode|sccode);
}
else
sccode=sccode<<1|0x01;
}
}
}
return(0);
}
void alarm()//鬧鈴
{
if((min==mm)&&(hour==hh)&&(flag_alarm==1))
{
P37=0;
}
}
問題是這樣的:1、下載到實驗板上時間都正常走時但現象是只有當K1按下后數碼管就黑屏,只有復位后正常,其他按鍵都正常工作;
2、當時間走到河設置的鬧鈴時間一樣后,鬧鈴不響,沒任何反映,鬧鈴的標志位都正常;
想了很久都沒找到問題的原因,懇請高人指點,謝謝了~~~~