基于AT89C51单片机的电子闹钟设计

2023-12-13 16:20:05

1.设计任务

利用AT89C51单片机为核心控制元件,设计一个电子闹钟,设计的系统实用性强、操作简单,实现了智能化、数字化。

(1)按开始键自动进入时间显示,开始为0,按K1键进入更改时间,闪烁位为可更改位,按K1键调整更改位,K2键为自增键,K3键为关闭响铃键,修改成功之后按K1键确定修改成功;

(2)按K1键进入秒表功能,再次按K2键秒表开始计时,K3键为秒表暂停键,继续则按K2键,K3键为清零键;

(3)按K1键进入闹钟功能,进去之后闪烁位为可更改位,按K1键调整更改位,K2键为自增键,K3键为关闭键。

2. 设计要求

2.1系统方案论证

根据设计任务,分析设计系统的组成,给出实现设计任务的几种方案,分析比较几种设计方案的优略,本着尽量以软件代替硬件,同时力求电路简单,工作可靠的原则,确定总体设计方案。

2.2系统硬件电路设计

根据系统设计方案进行软、硬件的分配,软、硬件设计分别进行。硬件设计包括单片机最小系统和扩展接口及配置,硬件结构在设计时要选择合适的元器件,硬件电路要简洁、工作可靠,需用Proteus绘制整个系统的电路仿真原理图。

2.3软件设计

根据该系统要求的功能进行软件设计,简述软件的功能,并根据每个模块的功能绘制软件流程图,根据流程图编写程序并汇编调试通过;列出软件清单,软件清单要求加以注释。

2.4 软硬件系统仿真

将编译后的程序软件加载到Proteus软件仿真的单片机ROM中,运行系统,实现软件程序对单片机系统的硬件电路的控制,并调试仿真结果,直至与设计任务相符。

#include<reg51.h>
#include<stdlib.h>
#include<stdio.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char

uint num,A_num,x=0;
uint h,m,s;
uint h1=0,m1=0,s1=0,sign=1; 

uchar code Zifu[]="0123456789";
 
sbit beep = P2^3;

sbit LCD_EN = P3^4;
sbit LCD_RS = P3^5;

sbit duan = P2^6;
sbit wei = P2^7;

sbit key_A = P3^6;
sbit key_B = P3^7;

void delay_1ms(uchar x){
 uchar i,j;
 for(j=0;j<x;j++)
  for(i=0;i<110;i++);
}

void write_command(uchar command){
 LCD_RS = 0;
 LCD_EN = 0;
 P0 = command;
 delay_1ms(2);
 LCD_EN = 1;    //EN 由1 -- 0 完成 有效数据 的 锁存
 delay_1ms(2);
 LCD_EN = 0;
}
 
void write_data(uchar yes){
 LCD_RS = 1;
 LCD_EN = 0;
 P0 = yes;
 delay_1ms(2);
 LCD_EN = 1;
 delay_1ms(2);
 LCD_EN = 0;
}
 
void lcd_post(int X,int Y){   //0,1表征 第一行 和 第二行
 write_command(0x80+X*(0x40)+Y);
}
 
void init(){
 h=m=s=0;
 num=A_num=0;
 
 LCD_EN=0;
 write_command(0x38);
 write_command(0x0c);
 //write_command(0x06); //写字符后地址指针加一
 write_command(0x01);
 
 TMOD = 0x02;
 TH0 = 6;
 TL0 = 6;
 EA = 1;
 ET0 = 1;
 TR0 = 1;
 
/*时分秒*/ 
 lcd_post(0,0); write_data(Zifu[h/10]);
 lcd_post(0,1); write_data(Zifu[h%10]);
 
 lcd_post(0,2); write_data(':');
 
 lcd_post(0,3); write_data(Zifu[m/10]);
 lcd_post(0,4); write_data(Zifu[m%10]);
 
 lcd_post(0,5); write_data(':');
 
 lcd_post(0,6); write_data(Zifu[s/10]);
 lcd_post(0,7); write_data(Zifu[s%10]);
 

 lcd_post(1,5); write_data('a');
 lcd_post(1,6); write_data('l');
 lcd_post(1,7); write_data('a');
 lcd_post(1,8); write_data('r');
 lcd_post(1,9); write_data('m');
 
 lcd_post(1,10); write_data(' ');
 lcd_post(1,11); write_data('c');
 
 lcd_post(1,12); write_data('l');
 
 lcd_post(1,13); write_data('o');
 lcd_post(1,14); write_data('c');
 lcd_post(1,15); write_data('k');
}
 


void ling()
{
 unsigned char a;
 for(a=0;a<500;a++)
 {
  
 if((P1&0x80)==0)
 {
  break;
 }
 
 lcd_post(1,5); write_data('l');
 lcd_post(1,6); write_data('i');
 lcd_post(1,7); write_data('n');
 lcd_post(1,8); write_data('g');
 lcd_post(1,9); write_data('!');
 lcd_post(1,10); write_data(' ');
 lcd_post(1,11); write_data('l');
 lcd_post(1,12); write_data('i');
 lcd_post(1,13); write_data('n');
 lcd_post(1,14); write_data('g');
 lcd_post(1,15); write_data('!');
 beep=0;  
 
 delay_1ms(500);
 lcd_post(1,5); write_data(' ');
 lcd_post(1,6); write_data(' ');
 lcd_post(1,7); write_data(' ');
 lcd_post(1,8); write_data(' ');
 lcd_post(1,9); write_data(' ');
 lcd_post(1,10); write_data(' ');
 lcd_post(1,11); write_data(' ');
 lcd_post(1,12); write_data(' ');
 lcd_post(1,13); write_data(' ');
 lcd_post(1,14); write_data(' ');
 lcd_post(1,15); write_data(' ');
 beep=1;
 
 delay_1ms(500);
 }
 
 
 lcd_post(1,5); write_data('a');
 lcd_post(1,6); write_data('l');
 lcd_post(1,7); write_data('a');
 lcd_post(1,8); write_data('r');
 lcd_post(1,9); write_data('m');
 lcd_post(1,10); write_data(' ');
 lcd_post(1,11); write_data('c');
 lcd_post(1,12); write_data('l');
 lcd_post(1,13); write_data('o');
 lcd_post(1,14); write_data('c');
 lcd_post(1,15); write_data('k');

}

void keyscan(){
 if(key_A==0){
  delay_1ms(3);
  if(key_A==0){
   A_num++; 
   switch(A_num){
    case 1:         //时
     TR0=0;        
     lcd_post(0,1);
     write_command(0x0f);
     break;
    case 2:lcd_post(0,4);break;    //分
    case 3:lcd_post(0,7);break;    //秒
    
    case 4:
     A_num=0;
     write_command(0x0c);
     TR0=1;
      sign = 0;
     break;
   }  
  }
  while(!key_A);
 }
 if(A_num!=0){
  if(key_B==0){
   delay_1ms(3);
   if(key_B==0){ 
    
    switch(A_num){
    case 1:
     h1=(++h1)%24;
     lcd_post(0,0); write_data(Zifu[h1/10]);
     lcd_post(0,1); write_data(Zifu[h1%10]);
     break;    //时
    case 2:
     m1=(++m1)%60;
     lcd_post(0,3); write_data(Zifu[m1/10]);
     lcd_post(0,4); write_data(Zifu[m1%10]);
     break;    //分
    case 3:
     s1=(++s1)%60;
     lcd_post(0,6); write_data(Zifu[s1/10]);
     lcd_post(0,7); write_data(Zifu[s1%10]);
     break;    //秒
    case 4:sign = 0;break;
    }write_command(0x10);  
   }
   while(!key_B);
  }
 }
}
 
void main()
{ 
 
  int clk1 =0;
 int clk2 =0;
  there:
 beep=0;
 init();
 //there:
 
 
  h1=m1=s1=0;
 while(sign)
 {
  keyscan();
 }
 
 clk1 = 0;
 clk2 = h1*3600+m1*60+s1;
 init();
 while(1){
    
  if(num==3686){
   num=0;
     s++;
      clk1++;
    if(clk1 > clk2)
    {
      ling();
     sign = 1;
     goto there;
     
    }
   if(s==60){
    s=0;
    m++;
    
    if(m==60){
     m=0;
     h++;
     if(h==24)h=0;
     lcd_post(0,0); write_data(Zifu[h/10]);
     lcd_post(0,1); write_data(Zifu[h%10]);
    }
      lcd_post(0,3); write_data(Zifu[m/10]);
       lcd_post(0,4); write_data(Zifu[m%10]);
     }
       lcd_post(0,6); write_data(Zifu[s/10]);
       lcd_post(0,7); write_data(Zifu[s%10]);
      }
  
       }
}
 

完整代码点开链接私信? 免费? 获取。

【iBot机器人工作室的个人空间-哔哩哔哩】 https://b23.tv/ryUWVKa

文章来源:https://blog.csdn.net/K11mvp/article/details/134712175
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。