数字钟仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h> //包含51单片机的头文件
- unsigned char code teble[6]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//包含数码位码
- unsigned char code table[10]={0Xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0Xf8,0X80,0X90};//数码管断码
- unsigned char shijian[6]={0,0,0,0,0,0};//时间显示
- unsigned char shijian2[6]={0,0,0,0,0,0};//星期显示
- unsigned char nyr[6]={0,0,0,0,0,0}; //年月日显示
- unsigned char nyr1[6]={0,0,0,0,0,0};
- void zh(); //状态函数
- void display(); //显示函数
- void week();//星期显示
- void miaobiao();//秒表显示
- unsigned char keysc();//按键模块
- void mDelay(unsigned int Delay); //延迟函数
- unsigned char hour,sec,minute;//定义小时,分钟,秒
- unsigned char day=22,month=05,year=18; //定义day month ,year
- unsigned char i,w,year1,j,se,fe,mi; //定义i,w,year1,j,se,fe,mi.
- sbit k1=P1^4;//按键k1=P1.0;
- sbit k2=P1^1;//按键k1=P1.1;
- sbit k3=P1^2; //按键k1=P1.21;
- sbit k4=P1^3;//按键k1=P1.3;
- unsigned char display_state=0;
-
- unsigned char keysc() //按键模块
- {if(!k1) //判断按键按下
- {mDelay(5); //延时消抖
- if(!k1)
- while(!k1) display(); //判断按键是否按下返回,因为按下时k1=0即!k1=1,display是消闪烁,一起来控制按下按键为一次的变量
- return 1;
- }
- if(!k2) //判断按键按下
- {mDelay(5); //延时消抖
- if(!k2)
- while(!k2) display(); //判断按键是否按下返回,因为按下时k1=0即!k1=1,display是消闪烁,一起来控制按下按键为一次的变量
- return 2;
- }
- if(!k3)
- {mDelay(5);
- if(!k3)
- while(!k3) display(); //同上所示
- return 3;
- }
- if(!k4)
- {mDelay(5);
- if(!k4)
- while(!k4) display(); //如上所示
- return 4;
- }
- return 0;
- }
- void display() //显示模块
- {unsigned char k; //定义k
- unsigned char y; //定义y
- unsigned char data1; //定义data1
- if(display_state==0)
- {
- for(k=0;k<6;k++) //使得k等于0到6
- {y=0x00;
- if(k==2||k==4) y=0x80;
- P2=0X00;
- data1=shijian[k];
- P0=table[data1]-y;
- P2=teble[k];
- mDelay(2);
- }
- }
- if(display_state==1)
- {
- for(k=0;k<6;k++)
- {y=0x00;
- if(k==2||k==4) y=0x80;
- P2=0X00;
- data1=nyr[k];
- P0=table[data1]-y;
- P2=teble[k];
- mDelay(2);
- }
- }
- if(display_state==2)
- {for(k=0;k<6;k++)
- {y=0x00;
- if(k==2||k==4) y=0x80;
- P2=0X00;
- data1=nyr1[k];
- P0=table[data1]-y;
- P2=teble[k];
- mDelay(2);
- }
- }
- if(display_state==3)
- {for(k=0;k<6;k++)
- {y=0x00;
- if(k==2||k==4) y=0x80;
- P2=0X00;
- data1=shijian2[k];
- P0=table[data1]-y;
- P2=teble[k];
- mDelay(2);
- }
- }
- }
- void mDelay(unsigned int Delay)
- {unsigned int k;
- for(;Delay>0;Delay--)
- {for(k=0;k<54;k++);
- }
- }
- void t0() interrupt 1 //外部中断0
- {
- TH0=55536/256;
- TL0=55536%256;
- i++;
- }
- void t1() interrupt 3 //定时器中断1
- {TH0=55536/256;
- TL0=55536%256;
- j++;
- }
- void zh() //转换模块
- {
- if(i==100)
- { i=0;sec++;
- if(sec==60) {sec=0;minute++;}
- if(minute==60) {minute=0;hour++;}
- if(hour==24) {hour=0;day++;}
- if(day==31) {day=0;month++;}
- if(month==12) {month=0;year++;}
- if(year==99) {year=0;}
-
-
- if(month==1||month==5||month==7||month==8||month==10||month==12)
- if(day==31){day=0;month++;}
- if(month==3||month==4||month==6||month==9||month==11)
- if(day==30){day=0;month++;}
- if(month==12){month=0;year++;}
- if(year/4==0)if(month==2)if(day==29)
- {day=0;month++;}
- else if(month==2)if(day==28)
- {day=0;month++;}
-
-
-
- }
- if(display_state==0)
-
- shijian[0]=sec%10;
- shijian[1]=sec/10;
- shijian[2]=minute%10;
- shijian[3]=minute/10;
- shijian[4]=hour%10;
- shijian[5]=hour/10;
-
- if(display_state==1)
- nyr[0]=day%10;
- nyr[1]=day/10;
- nyr[2]=month%10;
- nyr[3]=month/10;
- nyr[4]=year%10;
- nyr[5]=year/10;
- if(display_state==3)
- shijian2[0]=j%10;
- shijian2[1]=j/10;
- shijian2[2]=se%10;
- shijian2[3]=se/10;
- shijian2[4]=fe%10;
- shijian2[5]=fe/10;
-
- }
- void week() //星期显示模块
- {if(month==1||month==2)
- {month+=12;
- year--;
- }
- year1=year+2000;
- if(month==1||month==2) {month+=12; year1--;}
- w=(day+2*month+3*(month+1)/5+year1+year1/4-year1/100+year1/400)%7;
- nyr1[0]=w+2;
- }
- void miaobiao()
- { if(j==100)
- {se++;j=0; j=0; }
- if(se==60){ fe++;se=0;
- }
- shijian2[0]=j%10;
- shijian2[1]=j/10;
- shijian2[2]=se%10;
- shijian2[3]=se/10;
- shijian2[4]=fe%10;
- shijian2[5]=fe/10;
- }
-
- void main()
- {
- unsigned char key; //定义key
- unsigned char mun=0;//定义mun
- TMOD=0X01;
- TH0=55536/256;
- TL0=55536%256;
- TR0=1;
- EA=1;
- ET0=1; //采用定时器0的方式0;
- TMOD=0X11;
- TH0=55536/256;
- TL0=55536%256;
- TR1=0;
- EA=1;
- ET1=1; //采用定时器1的方式工作方式1
- while(1) //循环
- {
- week(); //星期显示
- zh(); //状态库
- display(); //显示模块
- miaobiao(); //秒表显示
- key=keysc(); //使得key等于按键的值
- zh(); //状态显示
- display(); //显示
- switch(key)
- {
- case 1:if(display_state==0)//如果display_state==0等于0
- {TR0=0;mun++;if(mun>3) {mun=0;TR0=1;}}//让定时器0停止工作如果mun大于3那么定时器从新开始;
- if(display_state==1) //如果display_state==0等于1那么执行以下代码
- {TR0=0;mun++;if(mun>3) {mun=0;TR0=1;}} break;
- case 2:if(display_state==0) //如果display_state==0等于0
- {
- if(mun==1) {sec++; if(sec==60) sec=00;} //如果mun等于1,秒加加,当秒加到60的时候秒等于0
- if(mun==2) {minute++; if(minute==60) minute=00;} //如果mun等于1,。分加加,当分加到60的时候分等于0
- if(mun==3) {hour++; if(hour==24) hour=00;} //如果hour等于1,shi加加,当shi加到60的时候秒等于0
- }
- if(display_state==1)
- {
- if(mun==1) {day++; if(day==32)day=00;}
- if(mun==2) {month++; if(month==12) month=00;}
- if(mun==3) {year++; if(year==99) year=00;}
- }
- if(display_state==2) //display_state等于2的时候
- {
- if(mun==1) {w++; if(w==7)day=01;} //并且mun等于1的时候w++,当w等于
- }
- if(display_state==3)
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
新建文件夹 (2).rar
(57.66 KB, 下载次数: 29)
|