乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Arduino在中斷函數(shù)里面怎么延時(shí)

       新用戶5228KeDY 2021-10-10

      Arduino在中斷函數(shù)里面怎么延時(shí)

      Arduino里面有兩種中斷,一種是內(nèi)部中斷,就象系統(tǒng)時(shí)鐘那樣,一直在那兒持續(xù),輪詢到中斷信號(hào),以最高的優(yōu)先級(jí)去執(zhí)行。另一種是外部中斷,輸入中斷信號(hào),以低于內(nèi)部中斷的優(yōu)先級(jí)去執(zhí)行。

      一般用不上中斷,但中斷異常重要。因?yàn)閘oop里面的耗時(shí)的動(dòng)作,例如持續(xù)不停的for循環(huán),for之外的動(dòng)作,只能等到for之后再去執(zhí)行。如果正在for的時(shí)候需要另一個(gè)緊急任務(wù),或者無條件去執(zhí)行的任務(wù),就只能使用中斷了。中斷有些象應(yīng)急搶險(xiǎn)系統(tǒng),無論什么情況,只要中斷信號(hào)來了,無條件去執(zhí)行。

      據(jù)資料介紹,中斷會(huì)打斷代碼的正常執(zhí)行次序,所以中斷函數(shù)里不能使用諸如millis()、 delay()等由中斷實(shí)現(xiàn)的延時(shí)函數(shù)。

      又有資料說可以使用 delayMicroseconds(),因?yàn)檫@個(gè)函數(shù)與中斷無關(guān)。但是試用之下,感覺似乎并沒有起到延時(shí)的作用,很可能它也不能使用。搜索一下有沒有人解決這個(gè)問題呢,確實(shí)有人遇到類似問題,已經(jīng)解決。

      LED發(fā)光二極管3只,220R電阻3只,1K電阻1只。

      接線如圖:

      設(shè)置997ms輪詢1次內(nèi)部中斷,觸發(fā)則使D1亮2000ms熄滅。電鍵由打開到閉合,觸發(fā)則使D2亮2000ms熄滅。接線后,執(zhí)行代碼,D1亮暗幾次后,節(jié)奏紊亂,說明997ms輪詢和2000ms延時(shí)有效。電鍵閉合,若閉合時(shí)D3為熄滅狀態(tài),D2亮起后,約2000ms后D3重復(fù)亮起,說明中斷函數(shù)中的延時(shí)有效。

      代碼:

      #include <MsTimer2.h>//需MsTimer2庫(kù)
      const int led_pin7 = 7;//內(nèi)部中斷LED腳
      const int led_pin6 = 6; //外部中斷LED腳
      const int led_pin5 = 5; //自己閃的LED
      const int pinInterrupt = 2;//外部中斷信號(hào)輸入
      ////////////////////軟件延時(shí)///////////////////////
      #define NOP do { __asm__ __volatile__ ("nop"); } while (0)
      #define ulong unsigned long
      void delay_(int ms)
      {
        for (int i = 0; i < ms; i++)
        {
          for (ulong j = 0; j < 1985; j++) NOP;
        }
      }
      /////////////////////////////////////////////////
      void flash7()
      {
        //使用自己的delay_才能正確延時(shí)
        static boolean output = HIGH;
        digitalWrite(led_pin7, output);
        delay_(2000);
        output = !output;
      }

      void flash6()
      {
        if ( digitalRead(pinInterrupt) == LOW ) {
          digitalWrite(led_pin6, HIGH);
          delay_(2000);
        }
        else
        {
          digitalWrite(led_pin6, LOW);
        }
      }

      void setup()
      {
        pinMode(led_pin7, OUTPUT);
        pinMode(led_pin6, OUTPUT);
        pinMode(led_pin5, OUTPUT);
        pinMode(pinInterrupt, INPUT);
        //內(nèi)部中斷鉤子
        MsTimer2::set(977, flash7); //997毫秒輪詢一次
        MsTimer2::start();
        //外部中斷鉤子
        attachInterrupt( digitalPinToInterrupt(pinInterrupt), flash6, CHANGE);
      }

      void loop()
      {
        digitalWrite(led_pin5, HIGH);
        delay(157);
        digitalWrite(led_pin5, LOW);
        delay(137);
      }

        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多