|
自己做的第一個(gè)數(shù)字鐘,有鬧鐘功能。新手五分,求大神指導(dǎo)
Altium Designer畫(huà)的原理圖和PCB圖如下:(51hei附件中可下載工程文件)
0.png (64.67 KB, 下載次數(shù): 180)
下載附件
2017-6-2 20:03 上傳
0.png (70.89 KB, 下載次數(shù): 153)
下載附件
2017-6-2 20:04 上傳
0.png (55.76 KB, 下載次數(shù): 160)
下載附件
2017-6-2 20:03 上傳
板子不超過(guò)10*8cm
不能用芯片,用三極管控制數(shù)碼管
有鬧鐘功能
顯示時(shí)分秒
有蜂鳴器
按鍵不超過(guò)六個(gè)(除復(fù)位外)
進(jìn)入設(shè)置時(shí)時(shí)鐘停止
使用12分頻
單片機(jī)源代碼如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define numport P0 //段選
- #define placeport P2 //位選
- uint hour=23,minut=59,second=59; //時(shí),分,秒
- uint hour1=0,minut1=0; //鬧鐘
- uint tc0time; //定時(shí)計(jì)數(shù)器0溢出次數(shù)
- uint numcode[]={0xC0,0xF9,0xA4, 0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf}; //零到九
- uint numplace[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //數(shù)碼管數(shù)字從右到左第一至四位
- uint keyt;
- uchar pattern=0; //0:時(shí)鐘模式 ,1:設(shè)置時(shí),2設(shè)置分
- uchar npattern=0; //0正常1鬧鐘時(shí)2鬧鐘分
- uchar alt=0,altt=0;
- sbit buzzer=P1^0;
- sbit key1=P1^4; //時(shí)鐘
- sbit key2=P1^5; //減一
- sbit key3=P1^6; //加一
- sbit key4=P1^7; //鬧鐘
- sbit led1=P1^1;
- sbit led2=P1^2;
- sbit led3=P1^3;
- bit al=0;
- bit alttt;
- void key(void) //按鍵判斷
- {
- if(key1==0)
- {
- keyt++;
- if(keyt==5)
- {
- keyt=0;
- if(buzzer==1&&al==1){buzzer=0;al=0;led1=1;led2=1;led3=1;}else{
- if(npattern==0)
- {
- pattern++;
- if(pattern!=0){led1=0;};
- if(pattern==3){pattern=0;led1=1;led2=1;led3=1;second=0;};
- if(pattern==1){led2=0;led3=1;}
- if(pattern==2){led3=0;led2=1;}
- }else{al=0;npattern=0;led2=1;led3=1;}
- }
- }
- }
- if(key2==0)
- {
- keyt++;
- if(keyt==5)
- {
- keyt=0;
- if(buzzer==1&&al==1){buzzer=0;al=0;led1=1;led2=1;led3=1;}else{
- if(pattern==1){hour++;};
- if(pattern==2){minut++;};
- if(npattern==1){hour1++;if(hour1==24){hour1=0;};};
- if(npattern==2){minut1++;if(minut1==60){minut1=0;};};
- }
- }
- }
- if(key3==0)
- {
- keyt++;
- if(keyt==5)
- {
- keyt=0;
- if(buzzer==1&&al==1){buzzer=0;al=0;led1=1;led2=1;led3=1;}else{
- if(pattern==1)
- {
- if(hour==0)
- {
- hour=23;
- }else{hour--;}
- }
- if(pattern==2)
- {
- if(minut==0)
- {
- minut=59;
- }else{minut--;}
- }
- if(npattern==1)
- {
- if(hour1==0)
- {
- hour1=23;
- }else{hour1--;}
- }
- if(npattern==2)
- {
- if(minut1==0)
- {
- minut1=59;
- }else{minut1--;}
- }
- }
- }
- }
- if(key4==0)
- {
- keyt++;
- if(keyt==5)
- {
- keyt=0;
- if(buzzer==1&&al==1){buzzer=0;al=0;led1=1;led2=1;led3=1;}else{
- if(pattern==0)
- {
- npattern++;
- if(npattern==3)
- {
- npattern=0;
- led2=1;led3=1;
- al=1;
- }
- if(npattern==1){led2=0;led3=1;}
- if(npattern==2){led3=0;led2=1;}
- }
- }
- }
- }
- }
- void delay (unsigned int a) //1ms延時(shí)
- {
- while(a--)
- {
- unsigned char i, j;
- i = 12;
- j = 169;
- do
- {
- while (--j);
- }while (--i);
- };
- }
- void delayu (unsigned int a) //@12.000MHz
- {
- while(a--)
- {
- unsigned char i, j;
- i = 2;
- j = 39;
- do
- {
- while (--j);
- } while (--i);
- };
- }
-
- void Timer0Init(void) //1毫秒@12.000MHz
- {
- IE=0x83; //中斷開(kāi)關(guān)設(shè)定:打開(kāi)總中斷開(kāi)關(guān),允許定時(shí)計(jì)數(shù)器中斷0工作
- TCON=0x10; //中斷控制寄存器設(shè)定:允許T/C0產(chǎn)生中斷,關(guān)閉其他中斷
- TMOD=0x01; //設(shè)置T/C0的工作方式為16位計(jì)數(shù)器
- TL0 = 0x18; //設(shè)置定時(shí)初值
- TH0 = 0xfc; //設(shè)置定時(shí)初值
- }
-
- void numprintf(uchar a,uchar b,uchar c) //數(shù)碼管輸出函數(shù)
- {
- uint i=0;
- uint f[8];
- f[2]=10;
- f[5]=10; //一到四位數(shù)字
- f[0]=a/10;f[1]=a%10; //時(shí)位的數(shù)字
- f[3]=b/10;f[4]=b%10; //分位的數(shù)字
- f[6]=c/10;f[7]=c%10;
- while(i<=7)
- {
- placeport=numplace[i];
- numport=numcode[f[i]];
- delayu(1);
- numport=0xff;
- i++;
- }
- }
- void time(void) //時(shí)間計(jì)算函數(shù)
- {
- if(second>=60){second=0;minut++;};
- if(minut>=60){minut=0;hour++;};
- if(hour>=24){hour=0;};
- if(tc0time==200){second++;tc0time=0;}
- tc0time++;
- }
- void alarm(void)
- {
- if(al==1)
- {
- if(hour==hour1&&minut==minut1){buzzer=1;}
- else{buzzer=0;}
- }
- }
- void tc0(void) interrupt 1 using 1 //1ms級(jí)別定時(shí),1t模式
- {
- TL0 = 0xb0; //重新裝初始值
- TH0 = 0x3c; //重新裝初始值
- time();
- if(pattern==1||pattern==2){tc0time=0;};
- alarm();
- key();
- if(buzzer==1&&al==1)
- {
- alt++;
- altt=alt/5;
- if(altt/2==0){alttt=0;}else{alttt=1;};
- if(alttt==0)
- {
- led1=0;
- led2=0;
- led3=0;
- }
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
鬧鐘.zip
(10.74 MB, 下載次數(shù): 167)
2017-6-2 19:47 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|