基于单片机的电子时钟设计(keil+protues仿真,含代码及原理图)

   本学期单片机课程要求做课程设计,我选取的课题如下:
基于单片机的电子时钟设计,要求:
(1)实时显示当前时间;
(2)能够对时间进行设置;
(3)包括年月日,小时,分钟,秒.
(4)整点提醒功能.
经过一周的时间已实现上述功能,故在此分享一下;

所选用器材
单片机最小系统(这就不用细说了吧,这里我选用AT89C51),排阻,四个按钮开关,8位共阴数码管,蜂鸣器;

protues仿真电路原理图
在这里插入图片描述
注:P1^1引脚接的是蜂鸣器,但是因为单片机的引脚输出太低,不能驱动蜂鸣器,所以需通过三极管放大电流从而驱动蜂鸣器正常工作,当引脚输出为低电平时分蜂鸣器响,同时对元件参数做些设置——蜂鸣器工作电压设置为2V即可,三极管前电阻阻值通常设置为1K即可

接下来直接放代码,注释写的也很清楚了,就不描述了

#include<reg51.h>
#define uChar unsigned char
#define uInt unsigned int
uChar a[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; 
uChar b[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; 
uChar second=50,minute=59,hour=12,year=18,month=07,day=21,count;
sbit Key1 = P3^0; //计时停止
sbit Key2 = P3^1; //调位
sbit Key3 = P3^2; //加一 
sbit Key4 = P3^3; //切换  
sbit Buzzer=P1^1;
/*********************延迟函数********************/
void Delay(uInt t)
{
while(t)
{
t--;
}
}
/*********************时分秒显示函数*******************/ 
void Dispaly1(uChar second,uChar minute,uChar hour)
{
/*********************第一位数码管*********************/
P2=b[0];
P0=a[hour/10];
Delay(10);
/*********************第二位数码管*********************/
P2=b[1];
P0=a[hour%10];
Delay(10);
/*********************第三位数码管*********************/
P2=b[2]; 
P0=0x40; 
Delay(10);
/*********************第四位数码管*********************/
P2=b[3]; 
P0=a[minute/10]; 
Delay(10);
/*********************第五位数码管*********************/
P2=b[4];
P0=a[minute%10]; 
Delay(10);
/*********************第六位数码管*********************/
P2=b[5]; 
P0=0x40; 
Delay(10);
/*********************第七位数码管*********************/
P2=b[6]; 
P0=a[second/10]; 
Delay(10);
/*********************第八位数码管*********************/
P2=b[7];; 
P0=a[second%10]; 
Delay(10);
}
/*********************年月日显示函数********************/
void Dispaly2(uChar day,uChar month,uChar year)
{
P2=b[0];
P0=a[day/10];
Delay(10);

P2=b[1];
P0=a[day%10];
Delay(10);

P2=b[2]; 
P0=0x40; 
Delay(10);

P2=b[3]; 
P0=a[month/10]; 
Delay(10);

P2=b[4]; 
P0=a[month%10]; 
Delay(10);

P2=b[5]; 
P0=0x40; 
Delay(10);

P2=b[6]; 
P0=a[year/10]; 
Delay(10);

P2=b[7]; 
P0=a[year%10]; 
Delay(10);
}
/*********************时钟按键扫描函数*********************/
void Keyscan1()
{
static uChar i=0,j=0; 
if(Key1==0) 
{
Delay(10); //消抖 
if(Key1==0) 
while(!Key1); //等待按键弹
i++;
}
/*时钟暂停功能*/
if(i%2==1) 
{
TR0=0;/*如果是奇数次按键自然关闭定时器0*/
}
if(i%2==0)
{
TR0=1;/*如果是偶数次按键则打开定时器0*/ 
}
/*时钟调位和数值加一功能*/
if(Key2==0) 
{
Delay(10); 
if(Key2==0)
while(!Key2);
j++; 
}
if(j%4==1)
{
if(Key3==0)
{
Delay(10); 
if(Key3==0)
while(!Key3);
second++;
if(second==60)
second=0;
}
}
if(j%4==2)
{
if(Key3==0)
{
Delay(10); 
if(Key3==0)
while(!Key3);
minute++;
if(minute==60)
minute=0; 
}
}
if(j%4==3)
{
if(Key3==0)
{ 
Delay(10); 
if(Key3==0)
while(!Key3);
hour++;
if(hour==24)
hour=0; 
}
}
}
/*日期按键扫描函数*/
void Keyscan2()
{
static uChar m=0,n=0; 
if(Key1==0) 
{
Delay(10); 
if(Key1==0)
while(!Key3);
m++;
}
if(m%2==1) 
{
TR0=0;/*奇数次按键则关闭定时器0*/
}

if(m%2==0)
{
TR0=1;/*偶数次按键则打开定时器0*/ 
}
if(Key2==0) 
{
Delay(10); 
if(Key2==0)
while(!Key2);
n++; 
}
/*日期调位和日期加一功能*/
if(n%4==1)
{
if(Key3==0)
{
Delay(10); 
if(Key3==0)
while(!Key3);
day++;
if(day==30)
day=0;
}
}
if(n%4==2)
{
if(Key3==0)
{ 
Delay(10); 
if(Key3==0)
while(!Key3);
month++;
if(month==12)
month=0; 
}
}
if(n%4==3)
{
if(Key3==0)
{ 
Delay(10); 
if(Key3==0)
while(!Key3);
year++;
if(year==99)
year=0; 
}
}
}

/************************************************/
/***************主函数***************************/
/************************************************/
void main()
{						
TMOD=0x01; 	 /*定时器以方式一工作*/
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;/*10ms计时*/
EA=1;
ET0=1;/*允许定时器0中断*/
TR0=1;/*打开定时器0*/
while(1)
{
static uChar h=0;
/*时钟和日期切换功能*/ 
if(Key4==0) 
{
Delay(10); 
if(Key4==0)
while(!Key4);
h++;
}
if(h%2==0)/*如果按键偶数次则显示时钟*/
{
Dispaly1(second,minute,hour);
Keyscan1(); 
}

if(h%2==1)/*如果按键奇数次则显示日期*/ 
{
Dispaly2(year,month,day);
Keyscan2();
}
}
}
/**********************中断函数**************************/
void time0_int(void) interrupt 1
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
count++;
if(count==100)/*10ms??ê±£???100′??ò??o?1s*/
{
count=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
day++;
if(day==30)
{
day=0;
month++;
if(month==12)
{
month=0;
year++;
if(year==99)
{
year=0;
}
}
}
}
}
}
}
/*判断整点提醒*/
if(second==00&&minute==00)								   
Buzzer=0;
else
Buzzer=1;
}

最后实现的效果
时间显示:
在这里插入图片描述
日期显示:
在这里插入图片描述
具体功能通过4个按钮实现

注意!

实际仿真中数码管显示可能会不稳定,具体原因已排除代码问题,通过实验把蜂鸣器换成发光二极管后显示正常,但是换为蜂鸣器后显示就会不稳定;所以猜测可能是仿真电路的问题或者软件bug,上实物后应该不会出现这种情况

发布了18 篇原创文章 · 获赞 43 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42194695/article/details/89765975