Clock display program (adjustable, with timed alarm, display can be turned off) c language.doc
#include
#define KEY_IO P3
#define XS_IO P0
sbit dx=P2^6;
sbit wx=P2^7;
sbit bell=P2^3;
bit xs_z_kg=1,fs_kg=1,a_kg=0,a_z_kg=0;
bit sd_z_kg=0,sd_sec=1,sd_min=1,sd_hou=1;
unsigned char sec=0,sec_temp,min=0,hou=0,dsxh=17,key,tz_wx=0;
unsigned char sec_xs=0,min_xs=0,hou_xs=0,a_sec=0,a_min=0,a_hou=0;
unsigned char sd_ds=5;
//data sheet
unsigned char code dxtab[]={0x3F,0x6,0x5B,0x4F,0x66,0x6D,0x7D,0x7,0x7F,0x6F};
unsigned char code wxtab[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
//function declaration
void delay(unsigned char);
void disp();
void keyscan();
void fun();
void alarm();
void ts_sub();
void ad_sub();
void main()
{
TMOD=0x01;
EA = 1;
ET0 = 1;
TH0=0x3c;
TL0=0xb0;
TR0 = 1;
bell=1;
while(1)
{
disp ();
keyscan();
if(key!=0)
fun();
}
}
void fun()//Function selection
{
a_z_kg = 0;
bell=1;
a_kg=0;
if(key==1)
{
while(key)
{
disp ();
keyscan();
}
alarm();
ts_sub();
}
if(key==4)
{
while(key)
{
disp ();
keyscan();
}
xs_z_kg=~xs_z_kg;
XS_IO=0xff;
dx=1;dx=0;
wx=1;wx=0;
}
}
void alarm()//闹钟
{
fs_kg=0;
sd_z_kg=1;
sec_xs=a_sec;
min_xs=a_min;
hou_xs=a_hou;
ad_sub();
if((a_sec!=sec_xs) || (a_min!=min_xs) || (a_hou!=hou_xs)) //判断定时时间是否改变
a_z_kg=1; //打开定时开关
a_sec=sec_xs;
a_min=min_xs;
a_hou=hou_xs;
tz_wx=0;
}
void ts_sub()//调时
{
sec_xs=sec;
min_xs=min;
hou_xs=hou;
sec_temp=sec_xs;
ad_sub();
if(sec_temp!=sec_xs)
{
sec=sec_xs;
min=min_xs;
hou=hou_xs;
}
min=min_xs;
hou=hou_xs;
sd_z_kg=0;
fs_kg=1;
tz_wx=0;
sd_sec=1;
sd_min=1;
sd_hou=1;
}
void ad_sub()//加减程序
{
while(1)
{
while(key==0)
{
disp();
keyscan();
}
if(key==1)
{
while(key)
{
disp();
keyscan();
}
return;
}
if(key==2)
{
while(key)
{
disp();
keyscan();
}
switch(tz_wx)
{
case 0:
if(sec_xs>0)
sec_xs--;