標題: 51單片機倒計時程序 [打印本頁]

作者: yyl    時間: 2014-12-12 16:27
標題: 51單片機倒計時程序
時鐘倒計時

xx.zip

29.2 KB, 下載次數(shù): 102, 下載積分: 黑幣 -5

倒計時


作者: admin    時間: 2014-12-12 17:36
好資料 下面是樓主的源碼
#include<reg52.h>
#define uchar unsigned char
#define uint  unsigned int
sbit KEY1 = P3^2;
sbit KEY2 = P3^3;
sbit KEY3 = P3^4;       
uint hour=12,min=23,sec=33;
uint flag=1;
uint Count = 0;
uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//數(shù)碼管段值
void Delay(uint del)
{        uchar i,j;
        for(i=0; i<del; i++)
        for(j=0; j<140; j++)   
        ;}
void Time0_Init()
{   Count++;
    TMOD=0x01;
   TH0=(65536-10000)/256;
   TL0=(65536-10000)%256;
   EA=1;
   ET0=1;}
void timer0_int(void)interrupt 1
  {
   Count++;
   TH0=(65536-10000)/256;
   TL0=(65536-10000)%256; }
void time()
{     P2=7;  
      P0=table[sec%10];
          Delay(1);
          P2=6;
          P0=table[sec/10];
          Delay(1);
          P2=5;
          P0=0x40;
          Delay(1);
          P2=4;
          P0=table[min%10];
          Delay(1);
          P2=3;
          P0=table[min/10];
          Delay(1);
          P2=2;
          P0=0x40;
          Delay(1);
          P2=1;
          P0=table[hour%10];
          Delay(1);
          P2=0;
          P0=table[hour/10];
          Delay(1);        }
void Key(void)
{          if(KEY1==0 )                         //計時開始
      {Delay(1);                     //20毫秒軟件防抖
                if(KEY1==0)
                TR0=1; }
          if(KEY2==0 )                         
            {  Delay(1);                  
                if(KEY2 == 0)
                TR0=0;
                Delay(1);}         
         if(KEY3==0 )                        //改變加減法
             {Delay(1);                   //20毫秒軟件防抖
                if(KEY3 == 0)
                flag=!flag;       
                Delay(1);
                }

}       
void Main(void)                 
{ Time0_Init();
while(1)
{ time();
   Key();
   if(Count == 100)
   { Count=0;
    if(flag==1)        //type=1時為加法               
            { sec++;
                if(sec>59)
                {  sec=0;
                   min++;
                   if(min>59)
                   {min=0;
                    hour++;
                        if(hour>23)  
                        hour=0; }} }
                else if(flag==0)
            {sec--;
                 if(sec<1)
                {sec=59;          
                 min--;
                 if(min<1)
                 {min=59;
                hour--;
                 if(hour<1)  
                hour=23;} } }
}
}}


作者: oldoldpotato    時間: 2018-1-8 15:20
牛批牛批
作者: 七猴    時間: 2019-7-28 15:28
這個數(shù)碼管的選通端是直接連的計數(shù)器嗎?P2是對應(yīng)的什么?直接就是位碼嗎?
作者: 6313    時間: 2021-7-14 10:15
有電路圖嗎




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1