新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 时间可调的定时闹钟C51程序

时间可调的定时闹钟C51程序

作者:时间:2012-10-31来源:网络收藏

/*
程序效果:时钟,包括,定时到了闹钟响,用蜂鸣器,本程序已经调试通过
您可从http://www.51hei.com/ziliao/file/naozhong.rar下载本程序的代码.
喇叭接在p2.0上.端口均可自行更改.
*/
#includereg52.h> //头文件
#includeintrins.h>
#define uchar unsigned char//宏定义
#define uint unsigned int
sbit key1=P3^5; //位声明
sbit key2=P3^6;
sbit key3=P3^7;
sbit fmq=P2^0;
uchar code table[]={0x3f,0x06,0x5b,//数码管显示的数值
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,
0xbf,0x86,0xdb,//带小数点的数值
0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
void jia(); //函数声明
void jian();
uchar table_1[6]; //定义数组,数组内含有6个数值
uchar table_2[6];
uchar shi=23,fen=59,miao=50; //显示初始值
uchar shi1,fen1,miao1,shi2,fen2,miao2,shi3,fen3,miao3;//定义全局变量
uchar flag,flag1,cnt,count;//定义全局变量
void delay(uchar i) //延时函数,用于动态扫描数码管
{
uchar x,y;
for(x=i;x>0;x--)
for(y=110;y>0;y--);
}
void init() //初始化函数
{
TMOD=0X01; //工作方式1
TH0=(65536-50000)/256; //定时时间为:50ms
TL0=(65536-50000)%256;
ET0=1; //打开定时器
EA=1; //开总中断
TR0=1; //启动定时器
}
void display() //显示子函数,用于显示时间数值
{
uchar i,j;
table_1[0]=miao%10; //分离秒的各位与十位
table_1[1]=miao/10;
table_1[2]=fen%10+11; //分离分的各位与十位
table_1[3]=fen/10;
table_1[4]=shi%10+11; //分离时的各位与十位
table_1[5]=shi/10;
j=0x7f; //从秒到时的扫描
for(i=0;i6;i++)
{
P2=j;
P0=table[table_1[i]];//显示数值
delay(10);
j=_cror_(j,1);//循环右移
}
}
void display_1() //显示子函数,用于显示定时时间
{
uchar i,j;
table_2[0]=miao2%10; //以下含义同上
table_2[1]=miao2/10;
table_2[2]=fen2%10+11;
table_2[3]=fen2/10;
table_2[4]=shi2%10+11;
table_2[5]=shi2/10;
j=0x7f;
for(i=0;i6;i++)
{
P2=j;
P0=table[table_2[i]];
delay(10);
j=_cror_(j,1);
}
}
void shijian() //时间子函数
{
if(flag>=20) //判断是否到一秒
{
flag=0; //到了,则标志位清零
miao++; //秒加1
if(miao>=60) //判断秒是否到60s
{
miao=0;//到了,则清零
fen++; //分加1
if(fen>=59) //以下含义同上
{
fen=0;
shi++;
if(shi>23)
shi=0;
}
}
}
}
void key_scan() //键盘扫描子函数
{
uchar i; //定义局部变量
if(key1==0)
{
while(!key1) //防止掉显
{
if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8)
{
display();
}
if(cnt==5||cnt==6||cnt==7)
{
display_1();
}
}
cnt++; //记下按键key1按下的次数
if(cnt==1) //第一次按下,停止计数
TR0=0;
if(cnt==2) //第二次按下
{
miao1=miao; //保存秒的数值
miao=99;//显示99,表示可以调节秒的数值了
for(i=0;i100;i++)
display(); //显示99
miao=miao1; //恢复前一刻秒的数值
}
if(cnt==3) //以下含义同上
{
fen1=fen;
fen=99;
for(i=0;i100;i++)
display();
fen=fen1;
}
if(cnt==4)
{
shi1=shi;
shi=99;
for(i=0;i100;i++)
display();
shi=shi1;
}
if(cnt==5)
{
miao1=miao2;
miao2=88;
for(i=0;i100;i++)
display_1();
miao2=miao1;
}
if(cnt==6)
{
fen1=fen2;
fen2=88;
for(i=0;i100;i++)
display_1();
fen2=fen1;
}
if(cnt==7)
{
shi1=shi2;
shi2=88;
for(i=0;i100;i++)
display_1();
shi2=shi1;
}
if(cnt==8) //第八次按下
{
TR0=1; //开始计数
cnt=0; //按下次数清零
}
}
if(key2==0) //判断key2是否按下
{
while(!key2) //防止掉显
{
if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8)
{
display();
}
if(cnt==5||cnt==6||cnt==7)
{
display_1();
}
}
jia();//调用加1的子函数
}
if(key3==0) //判断key3是否按下
{
while(!key3) //防止掉显
{
if(cnt==0||cnt==1||cnt==2||cnt==3||cnt==4||cnt==8)
{
display();
}
if(cnt==5||cnt==6||cnt==7)
{
display_1();
}
}
jian(); //调用减1子函数
}
}


上一页 1 2 下一页

评论


相关推荐

技术专区

关闭