數字鐘 要求:24小時制時間顯示,可隨時進行時間校對調整,整點報時及鬧鐘功能
51hei.gif (52.3 KB, 下載次數: 35)
下載附件
2021-10-21 17:56 上傳
]_H9TB3([RMPO5K5LMXVLA0.png (56.25 KB, 下載次數: 53)
下載附件
2021-10-21 15:56 上傳
單片機源程序如下:
- #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;
- //************************數碼管顯示的數值**************************
- uchar code table[]= {
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
- 0x7d,0x07,0x7f,0x6f,0x40,0x00
- };
- //****************************函數聲明******************************
- void jia();
- void jian();
- //********************數組定義,數組內含有8個數值******************
- uchar table1[8],table2[8],table3[8],table4[8];
- //**************************時間顯示初始值**************************
- 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秒 等時 位閃 次數 校時 鬧1 鬧2 鬧3
- uint flag2;
- // 蜂鳴
- //*********************延時函數,用于動態掃描數碼管*****************
- void delay(uchar i)
- {
- uchar x,y;
- for(x=i;x>0;x--)
- for(y=120;y>0;y--);
- }
- //*******************************初始化函數*************************
- void init()
- {
- TMOD=0x01; //工作方式1
- TH0=0x3c; //定時時間為:50ms (65536-50000)/256
- TL0=0x0b0; //(65536-50000)%256
- EA=1; //開總中斷
- ET0=1; //打開定時器
- TR0=1; //啟動定時器
- beep=0;
- }
- //********************顯示子函數,用于顯示時間數值*****************
- void display()
- {
- uchar i,j;
- if(cnt!=10||wss==0)
- {
- table1[0]=miao%10; //分離秒的個位與十位
- table1[1]=miao/10;
- }
- else
- table1[0]=table1[1]=11;
- if(cnt!=11||wss==0)
- {
- table1[3]=fen%10; //分離分的個位與十位
- table1[4]=fen/10;
- }
- else
- table1[3]=table1[4]=11;
- if(cnt!=12||wss==0)
- {
- table1[6]=shi%10; //分離時的個位與十位
- table1[7]=shi/10;
- }
- else
- table1[6]=table1[7]=11;
- table1[2]=table1[5]=10;
- j=0xfb;
- for(i=0;i<=7;i++) //從秒到時的掃描
- {
- P2=j;
- P0=table[table1[i]]; //顯示數值
- delay(10);
- j=_cror_(j,1); //循環右移
- }
- }
- //*******************顯示子函數,用于顯示定時1時間*****************
- 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);
- }
- }
- //********************顯示子函數,用于顯示定時2時間******************
- 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);
- }
- }
- //***************顯示子函數,用于顯示定時3時間數值****************//
- void display3()
- {
- uchar i,j;
- if(alm3==0)
- {
- if(cnt!=7||wss==0)
- {
- table4[0]=miao3%10; //分離秒的個位與十位
- table4[1]=miao3/10;
- }
- else
- table4[0]=table4[1]=11;
- if(cnt!=8||wss==0)
- {
- table4[3]=fen3%10; //分離分的個位與十位
- table4[4]=fen3/10;
- }
- else
- table4[3]=table4[4]=11;
- if(cnt!=9||wss==0)
- {
- table4[6]=shi3%10; //分離時的個位與十位
- 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; //從秒到時的掃描
- for(i=0;i<=7;i++)
- {
- P2=j;
- P0=table[table4[i]]; //顯示數值
- delay(10);
- j=_cror_(j,1); //循環右移
- }
- }
- //***********************時間子函數 *****************************//
- void shijian()
- {
- if(flag>=20) //判斷是否到一秒
- {
- wss=~wss;
- flag=0; //到了,則標志位清零
- if(cnt1!=0)
- {
- miao4++; //秒加1
- if(miao4>59) //判斷秒是否到60s
- {
- miao4=0; //到了,則清零
- fen4++; //分加1
- if(fen4>59) //以下含義同上
- {
- fen4=0;
- shi4++;
- 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;
- }
- }
- }
- }
- }
- //**************************鍵盤掃描子函數************************//
- 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按下的次數
- 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(); //調用減1子函數
- }
- 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子函數***************************
- void jia()
- {
- if(cnt==1) //判斷key1按下的次數是否為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子函數**************************//
- void jian()
- {
- if(cnt==1) //判斷key1按下的次數是否為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++;
- }
- }
- //***************************鬧鈴子函數***************************//
- void clock()
- {
- //判斷秒的數值是否相等
- 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)
- //是,再判斷時是否相等
- if(shi==shi1&&alm1==0||shi==shi2&&alm2==0||shi==shi3&&alm3==0)
- {
- flag2=0; //是,則標志位,flag2清零
- while(!(flag2==1200)&&(cnt==0)) //判斷flag2是否到1200且不
- {
- // 為調時狀態
- if(key4==0) //沒有,則繼續驅動蜂鳴器響
- {
- while(!key4)
- flag2=1200;
- }
- if(flag1>1) //等時方波驅動蜂鳴器
- {
- beep=~beep;
- flag1=0;
- }
- shijian(); //調用時間子函數
- display(); //調用顯示子函數
- }
- beep=0; //關閉蜂鳴器
- }
- }
- //**************************主函數********************************//
- void main()
- {
- init(); //調用初始化子函數
- while(1)
- {
- clock(); //鬧鐘子函數
- 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();
- shijian(); //調用時間子函數
- key_scan(); //調用鍵盤掃描子函數
- }
- }
- //**************************定時中斷******************************//
- void t0() interrupt 1
- {
- TH0=0x3c; //初值50ms (65536-50000)/256
- TL0=0x0b0; // (65536-50000)%256
- flag++; //標志位
- flag1++;
- flag2++; }
復制代碼
51hei.png (6.25 KB, 下載次數: 53)
下載附件
2021-10-21 17:57 上傳
仿真代碼下載:
數字鐘設計制作.zip
(63.25 KB, 下載次數: 67)
2021-10-21 15:58 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|