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

標(biāo)題: 51單片機(jī)的外部中斷函數(shù)里可以有if語句嗎 [打印本頁]

作者: 佳佳0227洋洋    時(shí)間: 2015-4-8 20:20
標(biāo)題: 51單片機(jī)的外部中斷函數(shù)里可以有if語句嗎
  1. /* 通過中斷來控制流水燈 */

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

  4. #define  LED  P0
  5. #define  uchar  unsigned char

  6. //uchar count;

  7. void delay_100ms()                //@11.0592MHz
  8. {
  9.         unsigned char i, j, k;

  10.         _nop_();
  11.         _nop_();
  12.         i = 5;
  13.         j = 52;
  14.         k = 195;
  15.         do
  16.         {
  17.                 do
  18.                 {
  19.                         while (--k);
  20.                 } while (--j);
  21.         } while (--i);
  22. }

  23. void liushuideng1()
  24. {
  25.        
  26.         uchar i,led;
  27.         led = 0xfe;
  28. //        while(1)
  29. //        {
  30.                 for(i=0;i<8;i++)
  31.                 {
  32.                         LED = _crol_(led,i);
  33.                         delay_100ms();
  34.                         if(i==8)
  35.                                 i = 0;
  36.                         EX0 = 1;       
  37.                 }
  38. //        }
  39. }

  40. void liushuideng2()
  41. {
  42.         uchar led,i;
  43.         led = 0xff;
  44. //        while(1)
  45. //        {
  46.                 for(i=0;i<8;i++)
  47.                 {
  48.                         led >>= 1;
  49.                         LED = led;
  50.                         delay_100ms();
  51.                         if(i==8)
  52.                         {        i = 0;
  53.                                 led = 0x7f;}
  54.                 }
  55. //        }
  56. }

  57. void liushuideng3()
  58. {
  59.         uchar led;
  60.         led = 0xaa;
  61. //        while(1)
  62. //        {
  63.                 LED = led;
  64.                 delay_100ms();
  65.                 LED = ~led;
  66.                 delay_100ms();
  67. //        }

  68. }

  69. void ZD_init()
  70. {
  71.         EA = 1;
  72.         EX0 = 1;
  73.         IT0 = 1;
  74. }

  75. void main()
  76. {
  77. //        uchar i;
  78.         ZD_init();
  79.         while(1)
  80.         {
  81.                 LED = 0xff;
  82.                 delay_100ms();
  83.                 LED = 0x00;
  84.                 delay_100ms();
  85.         }
  86. }


  87. void waibuZD0()        interrupt 0
  88. {
  89.         static count = 0;
  90.         count++;
  91.         if(count == 1)
  92.                 {       
  93.                         liushuideng1();        
  94. //                        delay_100ms();
  95.                 }
  96.         else if(count == 2)
  97.                 {       
  98.                         liushuideng2();
  99. //                        delay_100ms();
  100.                 }
  101.         else if(count == 3)
  102.                 {       
  103.                         liushuideng3();
  104. //                        delay_100ms();
  105.                 }
  106.         else
  107.                 { count = 0;
  108.                 }
  109.        
  110. }
復(fù)制代碼
上面是代碼,現(xiàn)象是,中斷一次會(huì)產(chǎn)生兩個(gè)流水燈現(xiàn)象。而我要的是中斷一次只產(chǎn)生一個(gè)流水燈現(xiàn)象。并且只用一個(gè)中斷。

作者: dingmingzhou    時(shí)間: 2015-4-9 09:00
好像沒人說不可以
作者: dingmingzhou    時(shí)間: 2015-4-9 09:01
好像沒人說不可以在中斷服務(wù)程序里用IF
作者: maxplus    時(shí)間: 2015-4-9 10:56
估計(jì)是中斷源的問題,如果中斷源是按鍵,就可能是按鍵抖動(dòng)的問題,實(shí)際上產(chǎn)生了多次中斷?梢约觽(gè)消抖電路,不如加個(gè)電容之類
作者: 克林    時(shí)間: 2019-11-12 16:23
如果中斷源是按鈕的話,可以通過代碼延時(shí)來進(jìn)行按鈕的消抖操作
作者: 別問我呀    時(shí)間: 2019-11-12 17:21
當(dāng)然可以呀
作者: 噗噗噗p    時(shí)間: 2019-11-12 17:53
按鍵按下存在抖動(dòng),必須要采取硬件或者軟件延時(shí)來消除抖動(dòng)
作者: liuyongjun000a    時(shí)間: 2019-11-12 17:54
你這代碼沒問題,主要是外引腳觸發(fā)方式?jīng)]有定義吧,上升沿和下降沿都解發(fā)了中斷,所以就出現(xiàn)兩次了。
作者: faiwong    時(shí)間: 2019-11-12 21:17
IT0默認(rèn)為0,上升沿中斷,下降沿也中斷,你所說的中斷一次實(shí)際上是觸發(fā)了兩次中斷,產(chǎn)生兩個(gè)流水燈現(xiàn)象沒毛病。
作者: Dfadti    時(shí)間: 2019-11-12 21:54
可以的,實(shí)在不知道,就試試嘛
作者: qwertyuio543    時(shí)間: 2019-11-12 22:46
可以的
作者: bh2030693    時(shí)間: 2019-11-12 22:46
根據(jù)你的代碼原文稍作精簡和修飾,你可以試一下(while里面注釋掉的代碼也可以取消注釋):
/* 通過中斷來控制流水燈 */

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

#define LED P0
#define uchar unsigned char

void Delay_100ms(void) {
  unsigned char i, j, k;

  _nop_();
  _nop_();
  i = 5;
  j = 52;
  k = 195;
  do {
    do {
      while (--k)
        ;
    } while (--j);
  } while (--i);
}

void LiuShuiDeng1(void) {
  uchar i;

  for (i = 0; i < 8; i++) {
    LED = ~(0x01 << i);
    Delay_100ms();
  }
}

void LiuShuiDeng2(void) {
  uchar i;
  uchar temp = ~0x80;

  for (i = 0; i < 8; i++) {
    LED = temp >> i;
    Delay_100ms();
  }
}

void LiuShuiDeng3(void) {
  LED = 0xaa;
  Delay_100ms();
  LED = ~0xaa;
  Delay_100ms();
}

void ExInt0_init(void) {
  IT0 = 1;
  EX0 = 1;
  EA = 1;
}

void main(void) {
  ExInt0_init();
  while (1) {
    // LED = 0xff;
    // Delay_100ms();
    // LED = 0x00;
    // Delay_100ms();
  }
}

void ExInt0(void) interrupt 0 {
  static uchar count;

  count++;
  switch (count) {
    case 1:
      LiuShuiDeng1();
      break;
    case 2:
      LiuShuiDeng2();
      break;
    case 3:
      LiuShuiDeng3();
      count = 0;
      break;
    default:
      count = 0;
      break;
  }
}




作者: bh2030693    時(shí)間: 2019-11-12 23:14
當(dāng)?shù)谝淮瓮獠恐袛?執(zhí)行的時(shí)候,外部中斷請(qǐng)求位IE0會(huì)自動(dòng)被清零,中斷之所以執(zhí)行兩遍,很顯然是當(dāng)執(zhí)行第一次中斷的過程中再次有信號(hào)觸發(fā)了中斷,所以第一遍執(zhí)行完依然存在中斷標(biāo)志,繼而執(zhí)行第二次中斷并清除已存在的請(qǐng)求標(biāo)志。為什么沒有第三次?因?yàn)檫@個(gè)觸發(fā)信號(hào)顯然在第一次觸發(fā)的時(shí)候發(fā)生的,正如樓上幾位朋友分析的,是因?yàn)槎秳?dòng)重復(fù)觸發(fā)。
在中斷函數(shù)最后一個(gè)大括號(hào)前加一句:“IE0 = 0;”人為清除中斷請(qǐng)求標(biāo)志。

void ExInt0(void) interrupt 0 {
  ...
  ...
  ...
  IE0 = 0;
}






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