51 single chip microcomputer alarm clock

Publisher:清新心情Latest update time:2015-10-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When I used 51 before, I simulated a lot of things in Proteus. I have experimented with some basic functions of 51. Although I have done more complex things, they were all done casually. I used whatever I needed. The program was bloated and difficult to read, inefficient, and I did not record the analysis in time.

 

I will make an alarm clock again when I have time. It feels very different from the ones I made before. In the past, I just copied it and made few changes myself. I always felt that the program was too long. When I designed the steps myself, the problems were not in this area. Although 51 is very familiar to many people, I think the algorithms are the same. It is very efficient to verify the algorithms on the familiar 51.

 

Function:

    1: Clock display

    2: Clock adjustable

    3: Can set alarm clock

    4: The alarm can be turned off at any time

 

Ideas:

     1: The clock is controlled by timer 1. Every second, the second variable is +1.

       Digital tube scanning display clock

 

    2: Set 2 adjustable time and minute buttons. When the button is pressed, the corresponding time variable +1

 

    3: Set the mode key. Press it an odd number of times to change the alarm time.

                    Press even times to change the clock hour and minute

     

     Constantly check and detect, is the clock time == the alarm time? If it is equal, the corresponding alarm will sound

 

    4: Set the alarm off button to turn off the alarm when pressed

       There will be a small problem in this place. The alarm needs to be scanned all the time to see if it is turned on, and the alarm button needs to be scanned all the time.

       When the button is pressed, the alarm is turned off at this moment. However, within this minute, the alarm has a reason to be turned on, so after turning it off, 

      When the alarm is detected again, it will turn on again.

        Improvement method: Set a flag value. After the alarm is turned off, the alarm will no longer be detected within a specified time. The specified time is related to the control accuracy of the alarm.

 

51 small alarm clock
 

The procedure is as follows:

 
#include

#define uchar unsigned char
#define uint unsigned int

sbit beep=P3^1;
sbit led=P3^0;

sbit hour_key=P3^4;    //Adjust the hour key
sbit minu_key=P3^5;    //Adjust the minute key

sbit stopsounder_key=P3^3;   //Turn off the alarm button

 bit sounder_flag=0;

sbit mode_key=P3^7;      //Adjust the clock or alarm control button


uchar mode_number;    //mode count

uchar  sec_count; 

uint a,b;  //mode count

 uchar minu0=1;
uchar hour0,sec0,   //alarm clock

     hour1,minu1,sec1,//clock

  h1,h2,m1,m2,s1,s2,//display position

  k; //State conversion flag
  
uchar code select[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};

uchar code table[]=  {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

 

 



void keyscan();
void init();
void delay(uchar z);
void display(uchar,uchar,uchar);
void  mode_check();

 


void init()
{
 a=0;
 b=0;
 k=0;


 hour1=0;
 minu1=0;
 sec1=0;

 hour0=0;
 minu0=1;
 sec0=0;

 

 TMOD=0x11;      //Timer 0, 1 works in mode 1; assign initial value

 TH0=(65536-5000)/256;
 TL0=(65536-5000)%6;

 

 TH1=(65536-50000)/256;
 TL1=(65536-50000)%6;


 
 EA=1;

 EX0=1;

//  ET0=0;
 ET1=1;

 

 IT0=1;       //P3.2 pin falling edge generates external interrupt 0

//  PX0=1;
 


//  TR0=1;     // Initially, the stopwatch does not work

 TR1=1;    //The clock starts working 


}


void delay(uchar z)
{
 int x,y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}

 

 

 



void ex0_int() interrupt 0

  //Function to be added
}

[page]

void keyscan()
{

 

 if(hour_key==0)         //press the key
 {
  
  delay(10);
  if(hour_key==0)
  
  while(!hour_key)display(hour1,minu1,sec1);   //eliminate the jitter of the digital tube

   if(mode_number%2==0)       //If in clock mode, the clock time is +1
   hour1++;
   else hour0++;         //If in alarm mode, the alarm time is +1

    if(hour1==12)
   {
    hour1=0;
   }

   if(hour0==12)
   {
    hour0=0;
   }

 

  }
 }

 

 if(minu_key==0)          //Minute key
 {
 
  delay(10);
  if(minu_key==0)
  {
   while(!minu_key)display(hour1,minu1,sec1);    //Eliminate the jitter of the digital tubeif
   (mode_number%2==0)
   minu1++;
   else
   minu0++;

   if(minu1==60)
   {
    minu1=0;
   }

   if(minu0==60)
   {
    minu0=0;
   }

 

  }

 }

 

 
   if(mode_key==0)         //Mode key

   {
   
  delay(10);
  if(mode_key==0)
   
    
   while(!mode_key)display(hour1,minu1,sec1);  //Digital tube jitter

   mode_number++;
  
   
  }

 }


    if(stopsounder_key==0)          //turn off the alarm button
 {
      sounder_flag=1;
  delay(10);
  if(stopsounder_key==0)
  {
   
     while(!stopsounder_key)display(hour1,minu1,sec1);

  beep=1;
  }
  

 }

 

 


 
}


 

void display(uchar hour,uchar minu,uchar sec)
{
 h1=hour/10;
 h2=hour;

 m1=minu/10;
 m2=minu;

 s1=sec/10;
 s2=sec;


 P2=0xff;
 P1=table[h1];
 P2=select[0];
 delay(5);

 P2=0xff;
 P1=table[h2];
 P2=select[1];
 delay(5);

 P2=0xff;
 P1=0x40;;
 P2=select[2];
 delay(5);

 P2=0xff;
 P1=table[m1];
 P2=select[3];
 delay(5);

 P2=0xff;
 P1=table[m2];
 P2=select[4];
 delay(5);

 P2=0xff;
 P1=0x40;
 P2=select[5];
 delay(5);

 P2=0xff;
 P1=table[s1];
 P2=select[6];
 delay(5);

 P2=0xff;
 P1=table[s2];
 P2=select[7];
 delay(5);

}



void timer1_int() interrupt 3   //control the clock to work  for 50ms
{
 TH1=(65536-50000)/256;
 TL1=(65536-50000)%6;
 b++;
 if(b==10)led=~led;
 if(b==20)
 {
   led=~led;
  b=0;
  sec1++;
        if(sec1==60)
          

    sounder_flag=0;     //Release the detection alarm every minute

             sec1=0;
               minu1++;
             if(minu1==60)
              {
                  minu1=0;
                  hour1++;
                  if(hour1==24)
                        hour1=0;
                       
              }
          }
      
 }
   
}

 


 
void main()
{
 init();
       
 
 for(;;)
     
    
    keyscan();       //Key scanif
 
 
    (mode_number%2==0)     //Normal clock display
    
       
     display(hour1,minu1,sec1);       
    }
      
  else
   {
 
     
      display(hour0,minu0,sec0);       //Set time displaydelay
 
    (100);
 
   }
 
    
  
 
  if(hour0==hour1&&minu0==minu1)      //If the set time is up
     
     if(!sounder_flag)       //And the button is pressed for one minute
     
        beep=0;       //Alarm on
     
    
     }
 
       }
 
 
 
 }


}

Reference address:51 single chip microcomputer alarm clock

Previous article:Pin description and off-chip bus structure of MCS-51 single-chip microcomputer
Next article:51 assembly instruction set

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号