自己做的第一个数字钟,有闹钟功能。新手五分,求大神指导
Altium Designer画的原理图和PCB图如下:(51hei附件中可下载工程文件)
板子不超过10*8cm
不能用芯片,用三极管控制数码管
有闹钟功能
显示时分秒
有蜂鸣器
按键不超过六个(除复位外)
进入设置时时钟停止
使用12分频
单片机源代码如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define numport P0 //段选
- #define placeport P2 //位选
- uint hour=23,minut=59,second=59; //时,分,秒
- uint hour1=0,minut1=0; //闹钟
- uint tc0time; //定时计数器0溢出次数
- uint numcode[]={0xC0,0xF9,0xA4, 0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf}; //零到九
- uint numplace[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //数码管数字从右到左第一至四位
- uint keyt;
- uchar pattern=0; //0:时钟模式 ,1:设置时,2设置分
- uchar npattern=0; //0正常1闹钟时2闹钟分
- uchar alt=0,altt=0;
- sbit buzzer=P1^0;
- sbit key1=P1^4; //时钟
- 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延时
- {
- 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; //中断开关设定:打开总中断开关,允许定时计数器中断0工作
- TCON=0x10; //中断控制寄存器设定:允许T/C0产生中断,关闭其他中断
- TMOD=0x01; //设置T/C0的工作方式为16位计数器
- TL0 = 0x18; //设置定时初值
- TH0 = 0xfc; //设置定时初值
- }
-
- void numprintf(uchar a,uchar b,uchar c) //数码管输出函数
- {
- uint i=0;
- uint f[8];
- f[2]=10;
- f[5]=10; //一到四位数字
- f[0]=a/10;f[1]=a%10; //时位的数字
- f[3]=b/10;f[4]=b%10; //分位的数字
- 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) //时间计算函数
- {
- 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级别定时,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;
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
闹钟.zip
(10.74 MB, 下载次数: 167)
|