52單片機之定時器0

#include <reg52.h>
#include <intrins.h>

#define uint unsigned int
#define uchar unsigned char

void Delay_ms(int);
void Display_show();
void Int_Init();
void KEY3_add();
void KEY4_min();


sbit LED1 = P0^7;
sbit KEY1 = P3^3;         //按鍵1
sbit KEY2 = P3^4;         //按鍵2
sbit KEY3 = P3^6;         //按鍵3
sbit KEY4 = P3^7;         //按鍵4
uchar table[] = {0xbF, 0x86, 0xdb, 0xcf, 0xe6, 0xed, 0xfd, 0x87, 0xff, 0xef};
               // 0     1     2      3    4      5    6      7     8     9
uint count = 999;  //計數
int flag = 1;


/*************主函數*******************/
void main()
{
    Int_Init();
    while(1)
    {
        Display_show();
        if(flag)
            count--;
        KEY3_add();
        KEY4_min();
    }
}


/***********延遲函數***********/
void Delay_ms(int time)
{
    int i;
    while(time--)
        for(i=0; i < 120; i++); 

}


/*********數碼管顯示函數************/
void Display_show()
{
    int i,k;
    for(i=0; i<5; i++)
    {
        P0 = table[count%10];   //個位
        P1 = 0x01;
        Delay_ms(5);
        P0 = table[(count/10)%10]; //十位
        P1 = 0x02;
        Delay_ms(5);
        P0 = table[count/100]; //百位
        P1 = 0x04;
        Delay_ms(5);
    }

    if(count==0)
    {
        P1=0x07;
        P0=0xbf;
        for(k=0;k<6;k++)
        {
            P0 = P0^0x80;;
            Delay_ms(500);
        }
            count=999;
    }
}


/*********中斷初始化********/
void Int_Init()
{
    IT1 = 1;
    EX1 = 1;
    EA = 1; 
}


/**************中斷函數*******************/
void KEY1_interrupt() interrupt 2 using 0  
{
    flag = !flag;   
}


/**********按鍵3加函數********/
void KEY3_add()
{
    if(KEY3 == 0)
    {
        Delay_ms(30);   //20~30ms
        if(KEY3 == 0)
        {
            if(count<990)
                count = count + 10;
        }
        while(!KEY3);
    }
}


/**************按鍵4減函數*********************/
void KEY4_min()
{
    if(KEY4 == 0)
    {
        Delay_ms(30);   //20~30ms
        if(KEY4 == 0)
        {
            if(count>10)
                count = count - 10;
        }
        while(!KEY4);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章