欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 13507|回復(fù): 6
收起左側(cè)

用兩個按鍵控制單片機(jī)流水燈的暫停和啟動問題

[復(fù)制鏈接]
ID:272210 發(fā)表于 2018-9-13 14:35 來自觸屏版 | 顯示全部樓層 |閱讀模式
問題詳見程序的注釋語句
單片機(jī)源程序如下:
  1. #include "reg51.h"                      /* 此文件中定義了單片機(jī)的一些特殊功能寄存器 */
  2. #include <intrins.h>
  3. typedef unsigned int        int16_t;        /* 對數(shù)據(jù)類型進(jìn)行聲明定義 */
  4. typedef unsigned char        int8_t;

  5. #define LED P2
  6. /**************端口定義************************************************/
  7. sbit Key_A=P3^1;           //開始
  8. sbit Key_B=P3^2;           //停止
  9. /*********變量定義******************************************/
  10. int8_t Start=0;
  11. int8_t Stop=0;

  12. void delay( int8_t );
  13. void LSD  ( )       ;
  14. void delay( int16_t i )
  15. {

  16.         while ( i-- );
  17.                
  18. }

  19. void Key_Scan(  )
  20. {

  21.            if(Key_A==0)                  //檢測開始按鍵
  22.            delay(500);
  23.            if(Key_A==0)
  24.            {
  25.                Start++;
  26.                    while(!Key_A);
  27.            }

  28.                               if(Key_B==0)                  //檢測停止按鍵
  29.            delay(500);
  30.            if(Key_B==0)
  31.            {
  32.                Stop++;
  33.                    while(!Key_B);
  34.            }

  35.            if(Start>1)
  36.            Start=0;
  37.            if(Stop>1)
  38.            Stop=0;               
  39. }

  40. void LSD ( )
  41. {
  42.            int8_t i=0;
  43.             int8_t LED_Last=0;

  44.             LED=0xfF;
  45.                 for(i=0;i<=7;i++)
  46.                 {
  47.                     LED=LED<<1;
  48.                 //        LED_Last=LED;

  49.                         delay(50000);
  50.                 }
  51. }

  52. void main()
  53. {
  54.         while(1)
  55.         {
  56.            Key_Scan();
  57.            if(Start)
  58.            {
  59.          while(1)
  60.          {
  61.              LSD();

  62.                   Key_Scan();
  63.                   if(Stop==1)//無法通過stop這個變量//跳出內(nèi)層循環(huán),這個是怎么回事?
  64.                   {
  65.                     Start=0;
  66. LED=0xff;
  67.                         break;
  68.                   }
  69. }
  70.       
  71.          

  72.          
  73.          }
  74.             
  75.         }
  76. }
復(fù)制代碼


回復(fù)

使用道具 舉報

ID:164602 發(fā)表于 2018-9-14 08:59 | 顯示全部樓層
我在板子上試驗了你的程序。
第一:首次按下K1,燈只循環(huán)一次就自動停止了。
第二:是可以退出循環(huán)的——機(jī)器運(yùn)行時間極短,你按下鍵時,跳出循環(huán)的判斷語句早執(zhí)行過了,當(dāng)然就出不來了,我說可以出來,是一直按著鍵,當(dāng)燈亮完最后一個后,這些語句就執(zhí)行到了,就退出來了。
建議:這種需要分時控制的程序,最好用外部中斷,隨時都可以打斷燈的開關(guān)。

在你的程序基礎(chǔ)上,我修改了一下:
#include "reg51.h"                      /* 此文件中定義了單片機(jī)的一些特殊功能寄存器 */
#include <intrins.h>
typedef unsigned int        int16_t;        /* 對數(shù)據(jù)類型進(jìn)行聲明定義 */
typedef unsigned char        int8_t;

#define LED P2
/**************端口定義************************************************/
sbit Key_B=P3^3;           //外部中斷1,HC6800-ES-V2.0板子是獨立按鍵K4鍵
/*********變量定義******************************************/
bit Start=0;

void IntConfiguration()//外部中斷設(shè)置子函數(shù)
{
        IT1=1;
        EX1=1;
        EA=1;
}

void delay( int16_t i )
{

        while ( i-- );
               
}

void LSD ( )
{
           int8_t i=0;
            int8_t LED_Last=0;

            LED=0xfF;
                for(i=0;i<=7;i++)
                {
                    LED=LED<<1;
                //        LED_Last=LED;

                        delay(50000);
                }
}

void main()
{
        IntConfiguration();
        while(1)
                {
                        while (Start)
                        {
                                LSD ( );
                        }
                        LED=0xff;
        }
}

void Int1(void) interrupt 2//外部中斷1的中斷服務(wù)函數(shù)
{
        delay(50000);
        Start=~Start;
        IE0=0;
}
這樣,只用一個鍵,就可以啟停燈的亮滅了。k4鍵按奇數(shù)次,燈就亮,按偶數(shù)次燈就滅。
回復(fù)

使用道具 舉報

ID:164602 發(fā)表于 2018-9-14 09:14 | 顯示全部樓層
如果你想在任何時候按下偶數(shù)次鍵,燈就立即滅,程序是這樣的:
#include "reg51.h"                      /* 此文件中定義了單片機(jī)的一些特殊功能寄存器 */
#include <intrins.h>
typedef unsigned int        int16_t;        /* 對數(shù)據(jù)類型進(jìn)行聲明定義 */
typedef unsigned char        int8_t;

#define LED P2
/**************端口定義************************************************/
sbit Key_B=P3^3;           //停止
/*********變量定義******************************************/
bit Start=0;
int8_t i=0;

void IntConfiguration()//外部中斷設(shè)置子函數(shù)
{
        IT1=1;
        EX1=1;
        EA=1;
}

void delay( int16_t i )
{

        while ( i-- );
               
}

void main()
{
        IntConfiguration();
        while(1)
                {
                        while (Start)
                        {
                                i++;
                                if (i==8)
                                {
                                        i=0;
                                        LED=0xff;
                                        delay(50000);
                                }
                                LED=LED<<1;
                                delay(50000);
                        }
        }
}

void Int1(void) interrupt 2//外部中斷1的中斷服務(wù)函數(shù)
{
        delay(50000);
        Start=~Start;
        LED=0xfF;
        i=0;
if (Start==1)
        {LED=LED<<1;
        delay(50000);}
        IE0=0;
}
回復(fù)

使用道具 舉報

無效樓層,該帖已經(jīng)被刪除
ID:776802 發(fā)表于 2020-6-12 11:48 來自觸屏版 | 顯示全部樓層
樓主?
回復(fù)

使用道具 舉報

ID:877984 發(fā)表于 2021-1-15 20:32 | 顯示全部樓層
HC6800-ES-V2.0 發(fā)表于 2018-9-14 09:14
如果你想在任何時候按下偶數(shù)次鍵,燈就立即滅,程序是這樣的:
#include "reg51.h"                       ...

請問最后的IE0=0是什么意思
回復(fù)

使用道具 舉報

ID:213173 發(fā)表于 2021-1-15 22:44 | 顯示全部樓層
用兩個按鍵控制單片機(jī)流水燈的暫停和啟動
  1. #include "reg51.h"                      /* 此文件中定義了單片機(jī)的一些特殊功能寄存器 */
  2. #include <intrins.h>
  3. typedef unsigned int        int16_t;        /* 對數(shù)據(jù)類型進(jìn)行聲明定義 */
  4. typedef unsigned char        int8_t;

  5. #define LED P2
  6. /**************端口定義************************************************/
  7. sbit Key_A=P3^1;           //開始
  8. sbit Key_B=P3^2;           //停止
  9. /*********變量定義******************************************/
  10. bit Start=0;
  11. bit sign=0;
  12. int16_t count,count1;

  13. void Key_Scan()
  14. {
  15.         if(Key_A==0||Key_B==0)//檢測按鍵
  16.         {
  17.                 if(++count1>=250 && sign==0)//檢測開始按鍵
  18.                 {
  19.                         sign=1;
  20.                         if(Key_A==0)Start=1; //開始
  21.                         if(Key_B==0)Start=0; //停止
  22.                 }        
  23.         }
  24.         else
  25.         {
  26.                 sign=0;
  27.                 count1=0;
  28.         }
  29. }

  30. void main()
  31. {
  32.         LED=0xfe;
  33.         while(1)
  34.         {
  35.                 Key_Scan();
  36.                 if(Start)
  37.                 {
  38.                         count++;
  39.                         if(count>=10000)
  40.                         {
  41.                                 count=0;
  42.                                 LED=LED<<1|0x01;
  43.                                 if(LED==0xff)
  44.                                         LED=0xfe;
  45.                         }
  46.                 }
  47.         }
  48. }
復(fù)制代碼
回復(fù)

使用道具 舉報

ID:847443 發(fā)表于 2021-1-25 17:13 | 顯示全部樓層
挑不出循環(huán)可以用中斷,或者skip
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表