⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time99m.c

📁 此程序是倒数器
💻 C
字号:
/***************************************************************************
  Time99m.c acts as a timer with max 99 minute timing.
  Author:Anon
  Modified on:2007.3.12
  Created on: 2003.5.27
***************************************************************************/
/*******************************head files*********************************/
#include<F:\Time99m\reg51.h>
#include<INTRINS.H>

/********************definition for symbolic constants or pins*************/
#define LED1               P10
#define CTRL_OUT           LED1

#define SEG_CODE           P0
#define DISP1              P20
#define DISP2              P21

#define KEY1               P32
#define KEY2               P33
#define KEY3               P34
#define KEY4               P35

#define KEY1_PRESSED       1
#define KEY2_PRESSED       2
#define KEY3_PRESSED       3
#define KEY4_PRESSED       4
#define NOKEY_PRESSED      0xFF 
#define KEY_RELEASED       NOKEY_PRESSED  


#define KEY_MIN_INC        KEY1_PRESSED
#define KEY_MIN_DEC        KEY2_PRESSED
#define KEY_CLOSE_CTRLOUT  KEY3_PRESSED
#define KEY_RESET          KEY4_PRESSED

/************************declaration for function prototype****************/
void SystemInit();
unsigned char ScanKey();
void MinDisp();
void ClrDisp();
void Delay1ms();
void Delay(int ms);

/************************declaration for constant array********************/
unsigned char code NumberTable[]=
{
   0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F
  // 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,
};

unsigned char FiftymSCount=0,HalfSecCount=0,MinCount=99;

bit TimeOutFlag=0;

void (* code RESET_ENTRY)()=0x0000;

/*********************interrutp service routine***************************/
void T1ISR(void) interrupt 3  
{
   TH1=(65536-46080)>>8;            //50ms @11.0592Mhz
   TL1=(65536-46080)&0xFF; 
   
   if(++FiftymSCount<10) return;
   FiftymSCount=0;                 //500ms out
   if(++HalfSecCount<120) return;
   HalfSecCount=0;                 //one minute out
   if(--MinCount!=0) return;
   TimeOutFlag=2;                  //time out
}   
   
/******************************main function*******************************/
void main()
{
   unsigned char KeyPress;
   
   SystemInit();
   
   while(1)
    {
       Delay(10);           //MinDisp is called in Delay function
       if(TimeOutFlag==1)   //time out
         {
           TimeOutFlag=0; 
           TR1=0;           //stop Timer1
           ClrDisp();
           CTRL_OUT=0;      //output a control signal indicating time out
           while(1)
             {
               while((KeyPress=ScanKey())==NOKEY_PRESSED);  //wait key pressed
               while(ScanKey()!=KEY_RELEASED);              //wait key released
               if(KeyPress==KEY_CLOSE_CTRLOUT)
                  CTRL_OUT=1;                    //close control output
               else if(KeyPress==KEY_RESET)   
                  (*RESET_ENTRY)();    //goto CPU reset entry to restart system
             }   
         }
         
       if((KeyPress=ScanKey())==NOKEY_PRESSED) continue;   //no key pressed
       while(ScanKey()!=KEY_RELEASED);                     //wait key released
       if(KeyPress==KEY_MIN_INC)   
         {                       
        if(MinCount<99) 
          {         
            FiftymSCount=0; 
            HalfSecCount=0;
            MinCount++;  
          } 
     } 
       else if(KeyPress==KEY_MIN_DEC)   
         {                       
        if(MinCount>1)  
          {         
            FiftymSCount=0;
            HalfSecCount=0;
            MinCount--; 
          }
     } 
    }
}

/****************************sub functions*********************************/
void SystemInit()
{
   TMOD=0x12;              //Timer1:mode 1(50ms timing)
   
   TH1=(65536-46080)>>8;   //50ms @11.0592Mhz(precision adjustment may be required)
   TL1=(65536-46080)&0xFF; 
   
   TR1=1;
   ET1=1;
   
   EA=1;
}
unsigned char ScanKey()       
{
   if(KEY1==1&&KEY2==2&&KEY3==3&&KEY4==4) 
      return NOKEY_PRESSED;
      
   Delay(20); 
     
   if(KEY1==1&&KEY2==2&&KEY3==3&&KEY4==4) 
      return NOKEY_PRESSED;
   
   
   if(KEY1==0)  return KEY1_PRESSED;
   if(KEY2==0)  return KEY2_PRESSED;
   if(KEY3==0)  return KEY3_PRESSED;
   if(KEY4==0)  return KEY4_PRESSED;
   
   return NOKEY_PRESSED;
}

void MinDisp()
{
   SEG_CODE=~NumberTable[MinCount/10];
   DISP1=1;
   DISP2=0;
   Delay1ms();
   SEG_CODE=(~NumberTable[MinCount%10])&(HalfSecCount%2==0?0x7f:0xff); //dot flashes
   DISP1=0;
   DISP2=1;
   Delay1ms();
   ClrDisp();       
}

void ClrDisp()
{
   SEG_CODE=0xff;   //turn off minute display
   DISP1=1; 
   DISP2=1; 
}
        
void Delay1ms()
{
   unsigned char i;
  
   for(i=152;i;i--)
        ;
}

void Delay(int ms)
{
  ms>>=1;  //ms/=2,MinDisp() needs about 10ms
  do{
      MinDisp();  
   } while(--ms);
}
/********************************end of program****************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -