//*************************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、当时间走到河设置的闹铃时间一样后,闹铃不响,没任何反映,闹铃的标志位都正常;
想了很久都没找到问题的原因,恳请高人指点,谢谢了~~~~