51 single chip microcomputer timer alarm program (with buttons)

Publisher:创意梦者Latest update time:2015-03-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The circuit diagram is very simple. The digital tube is connected to the p2 port with two 573s, and is controlled by latches of p0.0 and p0.1. For details, please refer to the circuit on the microcontroller development board: http://www.51hei.com/f/51hei-5.pdf. You can connect according to the diagram inside. You only need to change the port to the one in this program. You can set the timing time, hours, minutes, seconds, etc. by pressing the button.

//////////////////Include header files and macro definitions//////////////////////////////////////
#include
#include #define uchar
unsigned char #define uint unsigned
int
/// ... int0_ok=P3^2; sbit int1_ok=P3^3; ///////////////////Digital tube segment latch//////////////////////////////////////// sbit duan=P0^0; sbit wei=P0^1; uchar bit1,bit2,bit3,bit4,bit5,bit6;///////////////Hour, minute, and second display codeuchar fen=0;// Minute cacheuchar miao=0;//Second cacheuchar shi=0;//Hour cacheuchar key_temp;/////Key temporary storage //uchar key_old; uchar key_count;//Key countuchar key_back;/////Key backupuchar key_data;//////Key valueuchar key_con;//// uchar key_ok; uchar int0_function=0;///Function 0, for adjustment uchar int1_function=0;///Function 1, for operation and stop uchar time;////Timing constant uchar t2time; #define KEYDELAY 25 /*First key delay times*/ #define KEYLOOP 10 /*Continuous key interval times, must be less than KEYDELAY*/ void shanshuo(uchar aa,uchar ii); void init(); void sign(uchar i); /////////////////Millisecond delay/////////////////////////////////////////////////////////// void delayms(uint i) //Error 0us {      uint c;     unsigned char a,b;     for(c=i;c>0;c--)         for(b=142;b>0;b--)    for (          a             =2;a>0;          a--    )    ; } /// ... tablewei[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};//bit code uchar code welcom[]={0xbf,0xbf,0xc0,0x8c,0x86,0xc8,0xbf,0xbf};//--OPEN-- 1 uchar code on[]={0x bf,0xbf,0xbf,0xc0,0xc8,0xbf,0xbf,0xbf};//---on--- 2 uchar code off[]={0xbf,0xbf,0xc0,0x8e,0x8e,0xbf,0xbf,0xbf};//--off--- 3 uchar code closed[]={0xbf,0xc6,0xc7,0xc0,0x92,0x86,0xf8,0xbf};//-closed- 4 /////////////////////////////////////////Digital tube display program//////////////////////////////////////////// void display(uchar duan1,uchar wei1) {   P2=0xff;   duan=0;   wei=0;   P2=tableduan[duan1];   duan=1;   duan=0;   P2=tablewei[wei1];   wei=1;   wei=0;   delayms(1); }
























































 



 



////////////////////////////////////Digital tube display program/////////////////////////////////////////////////////Digital tube display
bit calculation/////////////////////////////////////////
void jisuan()
{
     bit1=shi/10;
  bit2=shi%10;
     bit3=fen/10;
  bit4=fen%10;
     bit5=miao/10;
  bit6=miao%10;
  /*
    display(bit1,0);
    display(bit2,1);
    display(10,2);
    display(bit3,3);
    display(bit4,4);  display
    (10,5);
    display(bit5,6);  display     (
    bit6,7)    ;    *  /
}
///
... 0xfb:key=1;break;    case 0xf7:key=2;break;    case 0xef:key=3;break;    case 0xdf:key=4;break;    case 0xbf:key=5;break;    case 0x7f:key=6;break;    default:key=0;break;  }  return key;  }  ////////////////////////////// /////Long press key value table///////////////////////////////////////////////////////  /*uchar key_data_con_jisuan(uchar dat)  {    uchar key;     switch(dat)  {    case 0xef:key=5;break;    case 0xdf:key=6;break;    default:key=0;break;  }  return key;  }  */  ////////////////////////////////////////////////Keyboard scan///////////////////////////////////////////////////////////////////////  void key_scan()  {   P3=0xff;    if((P3&0xff)!=0xff)    {        delayms(10);       key_temp=P3;    if(key_temp==key_back)    {        delayms(10);     key_count++;     if (key_count==KEYDELAY) //Continuous keys, if not, shield this if                {                 key_ok=0;                 key_data=key_data_jisuan(key_temp); //Key*                 key_count=KEYDELAY-KEYLOOP;             }    }    else    {    key_count=0;    key_back=key_temp;    }    //key_ok=1;    }    else    {       if(key_count)    {     key_count=0;     key_data=key_data_jisuan(key_temp);    }    key_temp=0;    key_ok=1;































































      }
   }[page]
   //////////////////Initialization of Timer 0/////////////////////////////
void InitTimer0(void)
{
    TMOD = 0x01;
    TH0 = 0x3C;
    TL0 = 0x0B0;
  
    ET0 = 1;
    //TR0 = 1;
    //EA = 1;
}
void InitTimer1(void)
{
    TMOD = 0x10;
    TH1 = 0x3C;
    TL1 = 0x0B0;
    EA = 1;
   // ET1 = 1;
   // TR1 = 1;
}
////////////////////////////////Main function starts//////////////////////////////
void main()
{
   uchar ss;
   InitTimer0();
  // InitTimer1();
  while(1)
  {
   key_scan();
   if(key_data==1)
   {
    key_data=0;
 
 int0_function++;
 if(int0_function==5)
  {
  int1_function=0;
  ss=0;
  int0_function=1;
  }
   }
 if(key_data==2)
   {
    key_data=0;
  
 int1_function++;
 if(int1_function>=3) int1_function=1;
   }
if(int0_function==0)
{
 sign(1);
}
/////////////////////Hour setting//////////////////////////////////////////
if(int0_function==1)
 {
  TR0 = 0;
    EA = 0;
   // ET1 = 1;
   // TR1 = 1;
    light=1;
 // shanshuo(0,1);
  if(key_data==3)
  {
 
  key_data=0;
  shi++;if(shi==24)shi=0;
  }
 if(key_data==4)
  {
 
  key_data=0;
  shi--;if(shi==0)shi=23;
  }
   jisuan();
  shanshuo(0,1);
 for(ss=10;ss>0;ss--)
 {
    display(bit1,0);
    bit2,1);
    display(10,2);
    display(bit3,3);
    display(bit4,4);
    display(10,5);
    display(bit5,6);
    display(bit6,7);
 }
for(ss=10;ss>0;ss--)
 {
   // display(bit1,0);
  // display(bit2,1);
    display(10,2);
    display(bit3,3);
    display(bit4,4);
    display(10,5) ;
    display(bit5,6);
    display(bit6,7);
 }
   
  
 }
//////////////////Minute setting//////////////////////////
if(int0_function==2)
 {
    TR0 = 0;
    EA = 0;
   // ET1 = 1;
   // TR1 = 1;
    light=1;
  if(key_data==3)
  {
 
  key_data=0;
  fen++;if(fen==60)fen=0;
  }
 if(key_data==4)
  {
 
  key_data=0;
  fen--;if(fen==0)fen=59;
  }
  jisuan();
 // shanshuo(0,2);
for(ss=10;ss>0;ss--)
 {
    display(bit1,0);
    display(bit2,1);
    display(10,2);
    display(bit3,3 );
    display(bit4,4);
    display(10,5);
    display(bit5,6);
    display(bit6,7);
 }
for(ss=10;ss>0;ss--)
 {
    display(bit1, 0);
   display(bit2,1);
    display(10,2);
   // display(bit3,3);
   // display(bit4,4);
    display(10,5);
    display(bit5,6);
    display (bit6,7);
 }
  
 }
///////////////////////Second setting////////////////////////// ////
if(int0_function==3)
 {
    TR0 = 0;
    EA = 0;
   // ET1 = 1;
   // TR1 = 1;
    light=1;
  if(key_data==3)
  {
 
  key_data=0;
  miao++; if(miao==60)miao=0;
  }
 if(key_data==4)
  {
 
  key_data=0;
  miao--;if(miao==0)miao=59;
  }
  jisuan();
 // shanshuo(0, 3);
for(ss=10;ss>0;ss--)
 {
    display(bit1,0);
    display(bit2,1);
    display(10,2);
    display(bit3,3);
    display(bit4, 4);
    display(10,5);
    display(bit5,6);
    display(bit6,7);
 }
for(ss=10;ss>0;ss--)
 {
    display(bit1,0);
   display(bit2,1);
    display(10,2) ;
    display(bit3,3);
    display(bit4,4);
    display(10,5);
   // display(bit5,6);
   // display(bit6,7);
 }
  }
if(int0_function==4)
{
    display(bit1,0);
    display(bit2,1);
    display(10,2);
    display(bit3,3);
    display(bit4,4);
    display(10,5);
    display(bit5,6);
    display (bit6,7);
}
//if(int0_function==6)
//{
// sign(6);
//}
  ////////////////////// //////Timing starts///////////////////////
   if(int1_function==1)
   {
    TR0 = 1;
    EA = 1;
 int0_function=4;
   // ET1 = 0;
    // TR1 = 0;
 if((shi==0)&&(fen==0)&&(miao==0))
 {
  light= 0;
 }
 
   jisuan();
   shanshuo(0,0);
  
   }
/////////////////Timing is over, turn off the timer and relay/////////////////
 if(int1_function==2)
   {
    TR0 = 0;
    EA = 0;
   // ET1 = 1;
   // TR1 = 1;
    light=1;
   int0_function=0;
   int1_function=0;
  
   jisuan();
   sign(4);

 
   }
 
 
  // jisuan();
 //shanshuo(0,0);
 
  }
}
//////////////////Timer 0 interrupt service///////// ///////////////////
void Timer0Interrupt(void) interrupt 1
{
    TH0 = 0x3C;
    TL0 = 0x0B0;
    //add your code here!
    time++;
   if(time==20)
     {
     time=0;
     if(miao>0)miao--;
     if((miao==0)&&(fen>0)){miao=59;fen--;}
     if((fen==0)&&( shi>0)){fen=59;shi--;}
     }
}[page]
void Timer1Interrupt(void) interrupt 3
{
    TH1 = 0x3C;
    TL1 = 0x0B0;
    //add your code here!
 t2time++;
 if(t2time==20)t2time=0;
}
void shanshuo(uchar aa,uchar i)///If i=1, it flashes in hours; if i=2, it flashes in minutes; if i=3, it flashes in seconds
{
    //uchar i; //flash frequencyif
 (aa==4|aa==0)
 {
                for(i;i>0;i--)
       {
       display(bit1,0);
       display(bit2,1);
       display(10,2);
       display(bit3,3);
       display(bit4,4);
       display(10,5);
       display(bit5,6);
       display(bit6,7);
       }
 }
    if(aa==1 )
 {
        for(i;i>0;i--)
       {
      // display(bit1,0);
      // display(bit2,1);
       display(10,2);
       display(bit3,3);
       display(bit4 ,4);
       display(10,5);
       display(bit5,6);
       display(bit6,7);
       }
    
 }
 if(aa==2)
 {
                   for(i;i>0;i--)
       {
         display(bit1,0);
         display(bit2,1);
         display(10,2);
        // display(bit3, 3);
        // display(bit4,4);
         display(10,5);
         display(bit5,6);
         display(bit6,7);
       }
 }
 if(aa==3)
 {
                       for(i;i>0 ;i--)
        {
          display(bit1,0);
          display(bit2,1);
          display(10,2);
          display(bit3,3);
          display(bit4,4);
          display(10,5);
         // display(bit5,6);
         // display(bit6,7);
        }
 }
for(i;i>0;i--)
  {
    display(bit1,0);
    display(bit2,1);
    display(10,2 );
    display(bit3,3);
    display(bit4,4);
    display(10,5);
    display(bit5,6);
    display(bit6,7);
  }
}
////////////////////Blinking Program///////////////////////////////////////////////////////////////////////////////////////////
void sign(uchar i )// 1, display open, display on 3, display off 4, display closed
{
  uchar bb;
  if(i==0)
  {
   P2=0x00;
   P0=0x03;
  }
  if(i==1)
  {
    for(bb=0;bb<8;bb++)
    {
     
  P2=0xff;
  duan=0;
  wei=0;
  P2=welcom[bb];
  duan=1;
  //delayms(1);
  duan=0;
  P2=tablewei[bb];
  wei=1;
  //delayms(1);
  wei=0;
  delayms(1);
    }
   }
  if(i==2)
  {
    for(bb=0;bb<8;bb++)
    {
     
  P2=0xff;
  duan=0;
  wei=0;
  P2=on[bb];
  duan=1;
  //delayms(1);
  duan=0;
  P2=tablewei[bb];
  wei=1;
  //delayms(1);
  wei=0;
  1);
    }
  
}
if(i==3)
  {
    for(bb=0;bb<8;bb++)
    {
     
  P2=0xff;
  duan=0;
  wei=0;
  P2=off[bb];
  duan=1;
  //delayms(1);
  duan=0;
  P2=tablewei[bb];
  wei=1;
  //delayms(1);
  wei=0;
  delayms(1);
    }
   }
   if(i==4|i==0)
  {
    for(bb=0;bb<8;bb++)
    {
     
  P2=0xff;
  duan=0;
  wei=0;
  P2=closed[bb];
  duan=1;
  //delayms(1);
  duan=0;
  P2=tablewei[bb];
  wei=1;
  //delayms(1);
  wei=0;
  delayms(1);
    }
   }
  
  }

Reference address:51 single chip microcomputer timer alarm program (with buttons)

Previous article:From lighting the first lamp to the running lamp
Next article:51 MCU-Interrupt Function Basics

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号