標(biāo)題:
基于單片機(jī)數(shù)碼管的可調(diào)數(shù)字鐘鬧鐘Proteus仿真程序
[打印本頁(yè)]
作者:
njjm
時(shí)間:
2020-7-9 10:02
標(biāo)題:
基于單片機(jī)數(shù)碼管的可調(diào)數(shù)字鐘鬧鐘Proteus仿真程序
這款鬧鐘具備整點(diǎn)報(bào)時(shí),設(shè)置多個(gè)鬧鐘,能夠調(diào)時(shí)等功能,非常全面,分享一下。
51hei.png
(16.66 KB, 下載次數(shù): 82)
下載附件
2020-7-9 22:12 上傳
單片機(jī)源程序如下:
#include<reg51.h>
#include<intrins.h>
//****************************宏定義********************************
typedef unsigned char uchar;
typedef unsigned int uint;
//****************************位聲明********************************
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
sbit beep=P3^0;
//************************數(shù)碼管顯示的數(shù)值**************************
uchar code table[]= {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x40,0x00
};
//****************************函數(shù)聲明******************************
void jia();
void jian();
//********************數(shù)組定義,數(shù)組內(nèi)含有8個(gè)數(shù)值******************
uchar table1[8],table2[8],table3[8],table4[8];
//**************************時(shí)間顯示初始值**************************
uchar shi=12,fen=0,miao=0;
//**************************定義全局變量****************************
uchar shi1,fen1,miao1,shi2,fen2,miao2,shi3,fen3,miao3;
uchar shi4,fen4,miao4;
uchar flag, flag1, wss, cnt, cnt1, alm1, alm2, alm3;
// 1秒 等時(shí) 位閃 次數(shù) 校時(shí) 鬧1 鬧2 鬧3
uint flag2;
// 蜂鳴
//*********************延時(shí)函數(shù),用于動(dòng)態(tài)掃描數(shù)碼管*****************
void delay(uchar i)
{
uchar x,y;
for(x=i;x>0;x--)
for(y=120;y>0;y--);
}
//*******************************初始化函數(shù)*************************
void init()
{
TMOD=0x01; //工作方式1
TH0=0x3c; //定時(shí)時(shí)間為:50ms (65536-50000)/256
TL0=0x0b0; //(65536-50000)%256
EA=1; //開總中斷
ET0=1; //打開定時(shí)器
TR0=1; //啟動(dòng)定時(shí)器
beep=0;
}
//********************顯示子函數(shù),用于顯示時(shí)間數(shù)值*****************
void display()
{
uchar i,j;
if(cnt!=10||wss==0)
{
table1[0]=miao%10; //分離秒的個(gè)位與十位
table1[1]=miao/10;
}
else
table1[0]=table1[1]=11;
if(cnt!=11||wss==0)
{
table1[3]=fen%10; //分離分的個(gè)位與十位
table1[4]=fen/10;
}
else
table1[3]=table1[4]=11;
if(cnt!=12||wss==0)
{
table1[6]=shi%10; //分離時(shí)的個(gè)位與十位
table1[7]=shi/10;
}
else
table1[6]=table1[7]=11;
table1[2]=table1[5]=10;
j=0xfb;
for(i=0;i<=7;i++) //從秒到時(shí)的掃描
{
P2=j;
P0=table[table1[i]]; //顯示數(shù)值
delay(10);
j=_cror_(j,1); //循環(huán)右移
}
}
//*******************顯示子函數(shù),用于顯示定時(shí)1時(shí)間*****************
void display1()
{
uchar i,j;
if(alm1==0)
{
if(cnt!=1||wss==0)
{
table2[0]=miao1%10; //以下含義同上
table2[1]=miao1/10;
}
else
table2[0]=table2[1]=11;
if(cnt!=2||wss==0)
{
table2[3]=fen1%10;
table2[4]=fen1/10;
}
else
table2[3]=table2[4]=11;
if(cnt!=3||wss==0)
{
table2[6]=shi1%10;
table2[7]=shi1/10;
}
else
table2[6]=table2[7]=11;
}
else
table2[0]=table2[1]=table2[3]=table2[4]=table2[6]=table2[7]=10;
table2[2]=table2[5]=10;
j=0xfb;
for(i=0;i<=7;i++)
{
P2=j;
P0=table[table2[i]];
delay(10);
j=_cror_(j,1);
}
}
//********************顯示子函數(shù),用于顯示定時(shí)2時(shí)間******************
void display2()
{
uchar i,j;
if(alm2==0)
{
if(cnt!=4||wss==0)
{
table3[0]=miao2%10; //以下含義同上
table3[1]=miao2/10;
}
else
table3[0]=table3[1]=11;
if(cnt!=5||wss==0)
{
table3[3]=fen2%10;
table3[4]=fen2/10;
}
else
table3[3]=table3[4]=11;
if(cnt!=6||wss==0)
{
table3[6]=shi2%10;
table3[7]=shi2/10;
}
else
table3[6]=table3[7]=11;
}
else
table3[0]=table3[1]=table3[3]=table3[4]=table3[6]=table3[7]=10;
table3[2]=table3[5]=10;
j=0xfb;
for(i=0;i<=7;i++)
{
P2=j;
P0=table[table3[i]];
delay(10);
j=_cror_(j,1);
}
}
//***************顯示子函數(shù),用于顯示定時(shí)3時(shí)間數(shù)值****************//
void display3()
{
uchar i,j;
if(alm3==0)
{
if(cnt!=7||wss==0)
{
table4[0]=miao3%10; //分離秒的個(gè)位與十位
table4[1]=miao3/10;
}
else
table4[0]=table4[1]=11;
if(cnt!=8||wss==0)
{
table4[3]=fen3%10; //分離分的個(gè)位與十位
table4[4]=fen3/10;
}
else
table4[3]=table4[4]=11;
if(cnt!=9||wss==0)
{
table4[6]=shi3%10; //分離時(shí)的個(gè)位與十位
table4[7]=shi3/10;
}
else
table4[6]=table4[7]=11;
}
else
table4[0]=table4[1]=table4[3]=table4[4]=table4[6]=table4[7]=10;
table4[2]=table4[5]=10;
j=0xfb; //從秒到時(shí)的掃描
for(i=0;i<=7;i++)
{
P2=j;
P0=table[table4[i]]; //顯示數(shù)值
delay(10);
j=_cror_(j,1); //循環(huán)右移
}
}
//***********************時(shí)間子函數(shù) *****************************//
void shijian()
{
if(flag>=20) //判斷是否到一秒
{
wss=~wss;
flag=0; //到了,則標(biāo)志位清零
if(cnt1!=0)
{
miao4++; //秒加1
if(miao4>59) //判斷秒是否到60s
{
miao4=0; //到了,則清零
fen4++; //分加1
if(fen4>59) //以下含義同上
{
fen4=0;
shi4++;
if(flag1>1) //等時(shí)方波驅(qū)動(dòng)蜂鳴器
{ beep=~beep;
flag1=0;
}
if(shi4>23)
shi4=0;
}
}
}
else
{
miao++; //秒加1
if(miao>59) //判斷秒是否到60s
{
miao=0; //到了,則清零
fen++; //分加1
if(fen>59) //以下含義同上
{
fen=0;
shi++;
if(shi>23)
shi=0;
}
}
}
}
}
//**************************鍵盤掃描子函數(shù)************************//
void key_scan()
{
if(key1==0)
{
while(!key1) //防止掉顯
{
if(cnt==1||cnt==2||cnt==3)
display1();
if(cnt==4||cnt==5||cnt==6)
display2();
if(cnt==7||cnt==8||cnt==9)
display3();
if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
display();
}
cnt++; //記下按鍵key1按下的次數(shù)
if(cnt==10&&cnt1==0)
{
miao4=miao;
fen4=fen;
shi4=shi;
cnt1++;
}
if(cnt==13)
{
cnt=0;
if(cnt1==1)
{
miao=miao4;
fen=fen4;
shi=shi4;
}
cnt1=0;
}
}
if(key2==0) //判斷key2是否按下
{
while(!key2) //防止掉顯
{
if(cnt==1||cnt==2||cnt==3)
display1();
if(cnt==4||cnt==5||cnt==6)
display2();
if(cnt==7||cnt==8||cnt==9)
display3();
if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
display();
}
jia();
}
if(key3==0) //判斷key3是否按下
{
while(!key3) //防止掉顯
{
if(cnt==1||cnt==2||cnt==3)
display1();
if(cnt==4||cnt==5||cnt==6)
display2();
if(cnt==7||cnt==8||cnt==9)
display3();
if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
display();
}
jian(); //調(diào)用減1子函數(shù)
}
if(key4==0) //判斷key4是否按下
{
while(!key4) //防止掉
{
if(cnt==1||cnt==2||cnt==3)
{
alm1=~alm1;
display1();
}
if(cnt==4||cnt==5||cnt==6)
{
alm2=~alm2;
display2();
}
if(cnt==7||cnt==8||cnt==9)
{
alm3=~alm3;
display3();
}
if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
display();
}
}
}
//****************************加1子函數(shù)***************************
void jia()
{
if(cnt==1) //判斷key1按下的次數(shù)是否為1
{
miao1++; //是,則秒加1
if(miao1>59) //判斷秒是否大于60,是,則秒清零
miao1=0;
}
if(cnt==2) //以下含意同上
{
fen1++;
if(fen1>59)
fen1=0;
}
if(cnt==3)
{
shi1++;
if(shi1>23)
shi1=0;
}
if(cnt==4)
{
miao2++;
if(miao2>59)
miao2=0;
}
if(cnt==5)
{
fen2++;
if(fen2>59)
fen2=0;
}
if(cnt==6)
{
shi2++;
if(shi2>23)
shi2=0;
}
if(cnt==7)
{
miao3++;
if(miao3>59)
miao3=0;
}
if(cnt==8)
{
fen3++;
if(fen3>59)
fen3=0;
}
if(cnt==9)
{
shi3++;
if(shi3>23)
shi3=0;
}
if(cnt==10)
{
miao++;
if(miao>59)
miao=0;
cnt1++;
}
if(cnt==11)
{
fen++;
if(fen>59)
fen=0;
cnt1++;
}
if(cnt==12)
{
shi++;
if(shi>23)
shi=0;
cnt1++;
}
}
//***************************減1子函數(shù)**************************//
void jian()
{
if(cnt==1) //判斷key1按下的次數(shù)是否為1,是則秒減1
{
miao1--;
if(miao1==255) //判斷秒是否減到255,是,則秒置59
miao1=59;
}
if(cnt==2) //以下含意同上
{
fen1--;
if(fen1==255)
fen1=59;
}
if(cnt==3)
{
shi1--;
if(shi1==255)
shi1=23;
}
if(cnt==4)
{
miao2--;
if(miao2==255)
miao2=59;
}
if(cnt==5)
{
fen2--;
if(fen2==255)
fen2=59;
}
if(cnt==6)
{
shi2--;
if(shi2==255)
shi2=23;
}
if(cnt==7)
{
miao3--;
if(miao3==255)
miao3=59;
}
if(cnt==8)
{
fen3--;
if(fen3==255)
fen3=59;
}
if(cnt==9)
{
shi3--;
if(shi3==255)
shi3=23;
}
if(cnt==10)
{
miao--;
if(miao==255)
miao=59;
cnt1++;
}
if(cnt==11)
{
fen--;
if(fen==255)
fen=59;
cnt1++;
}
if(cnt==12)
{
shi--;
if(shi==255)
shi=23;
cnt1++;
}
}
//***************************鬧鈴子函數(shù)***************************//
void clock()
{
//判斷秒的數(shù)值是否相等
if(miao==miao1&&alm1==0||miao==miao2&&alm2==0||miao==miao3&&alm3==0)
//是,在判斷分是否相等
if(fen==fen1&&alm1==0||fen==fen2&&alm2==0||fen==fen3&&alm3==0)
//是,再判斷時(shí)是否相等
if(shi==shi1&&alm1==0||shi==shi2&&alm2==0||shi==shi3&&alm3==0)
{
flag2=0; //是,則標(biāo)志位,flag2清零
while(!(flag2==1200)&&(cnt==0)) //判斷flag2是否到1200且不
{
// 為調(diào)時(shí)狀態(tài)
if(key4==0) //沒有,則繼續(xù)驅(qū)動(dòng)蜂鳴器響
{
while(!key4)
{
flag2=1200;
cnt==1;
miao1 =miao1-1;fen1=fen1-1; shi1=shi1+3;
}
}
if(flag1>1) //等時(shí)方波驅(qū)動(dòng)蜂鳴器
{
beep=~beep;
flag1=0;
}
shijian(); //調(diào)用時(shí)間子函數(shù)
display(); //調(diào)用顯示子函數(shù)
}
beep=0; //關(guān)閉蜂鳴器
}
}
//**************************主函數(shù)********************************//
void main()
{
init(); //調(diào)用初始化子函數(shù)
while(1)
{
clock(); //鬧鐘子函數(shù)
if(cnt==1||cnt==2||cnt==3) //顯示子函數(shù)
display1();
if(cnt==4||cnt==5||cnt==6)
display2();
if(cnt==7||cnt==8||cnt==9)
display3();
if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
display();
shijian(); //調(diào)用時(shí)間子函數(shù)
key_scan(); //調(diào)用鍵盤掃描子函數(shù)
if(miao==0&&fen==0)
{
if(flag1>1) //等時(shí)方波驅(qū)動(dòng)蜂鳴器
{
beep=~beep;
flag1=0;
}
}
}
}
//**************************定時(shí)中斷******************************//
void t0() interrupt 1
{
TH0=0x3c; //初值50ms (65536-50000)/256
TL0=0x0b0; // (65536-50000)%256
flag++; //標(biāo)志位
flag1++;
flag2++; }
復(fù)制代碼
所有資料51hei提供下載:
可調(diào)數(shù)字鐘鬧鐘整點(diǎn)報(bào)時(shí)功能.zip
(135.18 KB, 下載次數(shù): 71)
2020-7-9 10:02 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1