伊莉討論區

標題: 我需要kiel C AT89S51的人才....救救我-.- [打印本頁]

作者: jack8372    時間: 2012-11-25 09:46 PM     標題: 我需要kiel C AT89S51的人才....救救我-.-

我有個問題
需要大大幫我解答.....如何讓我設的P2.3 按鈕開關 按一下 開始 再一下 暫停 (有錯幫我改一改)
以下是程式碼
#include <reg51.h>         // key_4seg.C, 按 KB1 數字加 1, 4個 7 段顯示器顯示 0000 ~ 9999
sbit P2_0=P2^0;        //設定 P2_0為P2.0
sbit P2_1=P2^1;        //設定 P2_1為P2.1
sbit P2_2=P2^2;        //設定 P2_2為P2.2
sbit P2_3=P2^3; //設定 P2_3為P2.3
char s1=0x00,s2=0x00,s3=0x00,s4=0x00;   //初始值 s1_個位數, s2_十位數, s3_百位數, s4_千位數
void scanseg();             //副程式宣告
void delay500us();         //副程式宣告
void keyplus();                 //副程式宣告
void debounce();          //副程式宣告
void minus();        //副程式宣告
void clear();        //副程式宣告
void cool();         //副程式宣告
void stop();
//
main()                              //程式開始
{
        while(1)
        {       
           scanseg();        //掃描 4個 7 段顯示器
           if (P2_0==0)        //P2.0=0 ?
           {
              debounce();        //反彈跳延遲副程式
              while (P2_0==0)        //放開按鍵 ? 若無,則等待 ( 持續 SCAN )
                  scanseg();
              keyplus();        //數字加 1               
            }
                scanseg();        //掃描 4個 7 段顯示器
           if (P2_1==0)        //P2.1=0 ?
           {
              debounce();        //反彈跳延遲副程式
              while (P2_1==0)        //放開按鍵 ? 若無,則等待 ( 持續 SCAN )
                  scanseg();
              minus();        //數字減 1               
            }
                scanseg();        //掃描 4個 7 段顯示器
           if (P2_2==0)        //P2.2=0 ?
           {
                  debounce();        //反彈跳延遲副程式
              while (P2_2==0)        //放開按鍵 ? 若無,則等待 ( 持續 SCAN )
                  scanseg();
              clear();        //數字清除       
                  }
                  scanseg();        //掃描 4個 7 段顯示器
           if (P2_3==0)        //P2.3=0 ?
           {
                  debounce();        //反彈跳延遲副程式
              while (P2_3==0)        //放開按鍵 ? 若無,則等待 ( 持續 SCAN )
                  scanseg();
              cool();        //數字快速無限加 1       
                  }
                  scanseg();        //掃描 4個 7 段顯示器
           if (P2_3==1)
           {
           stop();
           while(P2_3==1)
              scanseg();
           cool();        //數字快速無限加 1       
           }
        }
}
void keyplus()
{
        s1++;
        if(s1==10)
        {
            s1=0;
            s2++;
                if(s2==10)       
                {
                s2=0;
                s3++;
                 if(s3==10)
                 {
                        s3=0;
                        s4++;
                         if(s4==10)
                            s4=0;
                        }
                }
        }
}
void minus()
{
      s1--;
                if(s1<0)
                {
                 s1=9;
                 s2--;
                 if(s2<0)       
                  {
                        s2=9;
                        s3--;
                        if(s3<0)
                         {
                                s3=9;
                                s4--;
                                if(s4<0)
                                 s4=9;
                                }
                        }
                }
}
void clear()
{
s1=0;
s2=0;
s3=0;
s4=0;
}
void cool()
{
     scanseg();
        s1++;
        if(s1==10)
        {
         s1=0;
         s2++;
          if(s2==10)       
            {
                s2=0;
                s3++;
                 if(s3==10)
                    {
                        s3=0;
                        s4++;
                         if(s4==10)
                            s4=0;
                        }
                }
        }
}
void debounce()        //key debouncing 10*4*0.5ms=20ms
{
        unsigned int i;
        for(i=0;i<10;i++)
           scanseg();
}
void stop()
{
        unsigned int i;
        for(i=0;i<10;i++)
          scanseg();
}
void scanseg()
{
     unsigned int i;       
        for(i=0;i<10;i++)                //10*4*0.5ms=20ms=0.02秒
    {
        P1=s1|0x30;                // 00110000 OR 0000xxxx = 0011xxxx
        delay500us();
        P1=s2|0x20;
        delay500us();
        P1=s3|0x10;
        delay500us();
        P1=s4|0x00;
        delay500us();
    }
}
//
void delay500us()         //delay 0.5ms=500us
{
        unsigned int i;
        for(i=0;i<60;i++);
}


***有錯幫一下   感恩= ="





歡迎光臨 伊莉討論區 (http://blog.eyny.com/) Powered by Discuz!