基于51单片机设计的数字钟
一:效果图如下
8位LED数码管显示,P3.7做时钟调节,P3.6做分钟调节,P3.5做秒钟调节;
二:原理图如下
//源程序代码如下
#include<regx51.h> // 包含51单片机寄存器定义的头文
unsigned char code disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40}; unsigned char code bitdisp[8]={0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c}; unsigned char buffer[8]={0,0,16,0,0,16,0,0}; unsigned char dispbitcnt; unsigned int tcnt=0; unsigned char second=0; unsigned char count=0; unsigned char minute=0; unsigned char hour=12;
void delay10ms(void) { unsigned char i,j; for(i=200;i>0;i--) for(j=248;j>0;j--) ;
}
void t0(void) interrupt 1 using 0 //中断服务子程序 {
TH0=0x06; TL0=0x06; TR0=1;
if(count!=8) {count++;} else { count=0; if(dispbitcnt!=8) //端口显示程序 { P2=bitdisp[dispbitcnt]; P1=disp[buffer[dispbitcnt]]; dispbitcnt++; } else{dispbitcnt=0;}
if(tcnt!=400) {tcnt++;} else { tcnt=0; if(second!=59) { second++; } else {second=0; if(minute!=59) { minute++; } else { minute=0; if(hour!=23) {hour++;} else{hour=0;} } } } }
}
void initial(void) //中断初始化程序 { TMOD=0x02; TH0=0x06; TL0=0x06; TR0=1; ET0=1; EA=1; }
void main(void) { initial(); while(1) { if((P3&0xe0)!=0xe0) { delay10ms(); if(P3_7==0) { if(hour!=23) {hour++;} else {hour=0;} } else { if(P3_6==0) { if(minute!=59) { minute++; } else{minute=0;} } else {delay10ms(); if(second!=59) { second++; } else {second=0;} } } }
buffer[0]=hour/10; buffer[1]=hour%10; buffer[3]=minute/10; buffer[4]=minute%10; buffer[6]=second/10; buffer[7]=second%10; } }
|