数码管显示时间.docx
- 文档编号:26123614
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:15
- 大小:17.09KB
数码管显示时间.docx
《数码管显示时间.docx》由会员分享,可在线阅读,更多相关《数码管显示时间.docx(15页珍藏版)》请在冰豆网上搜索。
数码管显示时间
/****************************************************
*
***计时器
***用到138译码器P2^2P2^3P2^4
***数码管接到P0端口
***通过按K4可以切换闹铃与时间显示
***按K1可以调秒数(调设置时间的秒数,后同),按K2可以调分数,K3可以调小时数
*
****************************************************/
#include
#defineuintunsignedint
#defineucharunsignedchar
sbitLSA=P2^2;
sbitLSB=P2^3;
sbitLSC=P2^4;
sbitBeep=P1^5;
sbitK1=P3^1;
sbitK2=P3^0;
sbitK3=P3^2;
sbitK4=P3^3;
#defineFirstLSA=0;LSB=0;LSC=0//138译码器位数设置
#defineSecondLSA=1;LSB=0;LSC=0
#defineThirdLSA=0;LSB=1;LSC=0
#defineFourthLSA=1;LSB=1;LSC=0
#defineFifthLSA=0;LSB=0;LSC=1
#defineSixthLSA=1;LSB=0;LSC=1
#defineSeventhLSA=0;LSB=1;LSC=1
#defineEighthLSA=1;LSB=1;LSC=1
ucharcodeDIG_CODE[16]={
0x03f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
ucharTime=0;
ucharmiao1=0,miao2=0,fen1=0,fen2=0,shi1=0,shi2=0;
charSetmiao1=-1,Setmiao2=0,Setfen1=0,Setfen2=0,Setshi1=0,Setshi2=0;
voidTimerConfiguration();
voiddelayms(ucharx);
voidTimer();//时间显示
voidSetingTime();//时间设置
voidClock();//闹铃
voidSetingClock();//闹铃时间设置
voidSetingClockDisplay();//闹钟设置显示
voidDelay(unsignedinti);//蜂鸣器所用延时
main()
{
ucharNum=1;
Beep=0;
TimerConfiguration();
while
(1)
{
if(K4==0)//通过K4来选择是计时还是设置闹铃
{
delayms
(1);
if(K4==0)
{
Num++;
while(!
K4);
}
}
if(Num==2)
{
Num=0;
}
if(Num)
{
SetingClock();
}
else
{
SetingTime();
Timer();
Clock();
}
}
}
voidSetingTime()//时间设置
{
if(K1==0)
{
delayms
(1);
if(K1==0)
{
miao1++;
while(!
K1);
}
else
{
}
}
if(K2==0)
{
delayms
(1);
if(K2==0)
{
fen1++;
while(!
K2);
}
else
{
}
}
if(K3==0)
{
delayms
(1);
if(K3==0)
{
shi1++;
while(!
K3);
}
else
{
}
}
}
voidTimer()//时间显示
{
if(Time==20||Time>20)//每秒miao1++
{
Time=0;
miao1++;
}
else//显示:
在第一位显示秒的个位
{
P0=0x00;
First;
P0=0x00;
P0=DIG_CODE[miao1];
}
if(miao1==10)
{
miao1=0;
miao2++;
}
else//显示秒的十位
{
P0=0x00;
Second;
P0=0x00;
P0=DIG_CODE[miao2];
}
if(miao2==6)
{
miao2=0;
fen1++;
}
P0=0x00;//显示第三位的“-”
Third;
P0=0x00;
P0=0x40;
if(fen1==10)
{
fen1=0;
fen2++;
}
else//显示分的个位
{
P0=0x00;
Fourth;
P0=0x00;
P0=DIG_CODE[fen1];
}
if(fen2==6)
{
fen2=0;
shi1++;
}
else//分的十位
{
P0=0x00;
Fifth;
P0=0x00;
P0=DIG_CODE[fen2];
}
P0=0x00;//显示第六位的“-”
Sixth;
P0=0x00;
P0=0x40;
if(shi1==10)
{
shi1=0;
shi2++;
}
else//显示时的个位
{
P0=0x00;
Seventh;
P0=0x00;
P0=DIG_CODE[shi1];
//P0=0x00;
}
if(shi2==2)
{
shi2=0;
}
else//显示时的十位
{
P0=0x00;
Eighth;
P0=0x00;
P0=DIG_CODE[shi2];
//P0=0x00;
}
}
voidSetingClock()//闹铃时间设置
{
SetingClockDisplay();//闹钟设置显示
/*************通过按键来设置闹铃时间**********/
if(K1==0)
{
delayms
(1);
if(K1==0)
{
Setmiao1++;
while(!
K1);
}
else
{
}
}
if(K2==0)
{
delayms
(1);
if(K2==0)
{
Setfen1++;
while(!
K2);
}
else
{
}
}
if(K3==0)
{
delayms
(1);
if(K3==0)
{
Setshi1++;
while(!
K3);
}
else
{
}
}
/***********时间的进位设置******************/
if(Setmiao1==10)
{
Setmiao1=0;
Setmiao2++;
}
if(Setmiao2==6)
{
Setmiao2=0;
Setfen1++;
}
if(Setfen1==10)
{
Setfen1=0;
Setfen2++;
}
if(Setfen2==6)
{
Setfen2=0;
Setshi1++;
}
if(Setshi1==10)
{
Setshi1=0;
Setshi2++;
}
if((Setshi2==2)&&(Setshi1==3))//即23时
{
Setshi1=0;
Setshi2=0;
}
}
voidClock()//闹铃
{
if((Setmiao1==miao1)&&(Setmiao2==miao2)&&(Setfen1==fen1)&&(Setfen2==fen2)&&(Setshi1==shi1)&&(Setshi2==shi2))
{
while
(1)
{
Beep=1;
Delay(5);
Beep=0;
Delay(5);
}
}
}
voidSetingClockDisplay()//闹钟设置显示
{
P0=0x00;
First;
P0=0x00;
P0=DIG_CODE[Setmiao1];
P0=0x00;
Second;
P0=0x00;
P0=DIG_CODE[Setmiao2];
P0=0x00;
Fourth;
P0=0x00;
P0=DIG_CODE[Setfen1];
P0=0x00;
Fifth;
P0=0x00;
P0=DIG_CODE[Setfen2];
P0=0x00;
Seventh;
P0=0x00;
P0=DIG_CODE[Setshi1];
P0=0x00;
Eighth;
P0=0x00;
P0=DIG_CODE[Setshi2];
}
voidTimerConfiguration()//中断的设置
{
TMOD=0x01;//选择工作方式1
TH0=0x3C;//设置初始值,定时50MS
TL0=0xB0;
EA=1;//打开总中断
ET0=1;//打开定时器0中断
TR0=1;//启动定时器0
}
/*******************************************************************************
*函数名:
Timer0()
*函数功能:
定时器0中断函数
*输入:
无
*输出:
无
*******************************************************************************/
voidTimer0()interrupt1
{
TH0=0x3C;//设置初始值
TL0=0xB0;
Time++;
}
voiddelayms(ucharx)//x*10毫秒
{
uchara,b,c;
for(c=x;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
voidDelay(unsignedinti)//蜂鸣器用延时函数
{
charj;
for(i;i>0;i--)
for(j=200;j>0;j--);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数码管 显示 时间
![提示](https://static.bdocx.com/images/bang_tan.gif)