簡體   English   中英

Arduino IDE 中的 ATTiny85 中斷

[英]ATTiny85 Interrupts in Arduino IDE

我有一個 ATTiny85,我使用 sparkfun 程序員 ( https://www.sparkfun.com/products/11801 ) 進行編程,我使用的 ATTiny Board Manager 是: https ://raw.githubusercontent.com/damellis/attiny/ ide-1.6.x-boards-manager/package_damellis_attiny_index.json

下面是我的代碼,當我將引腳 2 接地時,我無法使中斷工作。我已經測試過 LED 在中斷之外(在循環內)是否正常工作。 歡迎任何建議。

#include "Arduino.h"

#define interruptPin 2

const int led = 3;
bool lastState = 0;

void setup() {
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(interruptPin, pulseIsr, CHANGE);
  pinMode(led, OUTPUT);
}

void loop() {

}


void pulseIsr() {
    if (!lastState) {
      digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
      lastState = 1;
    }
    else {
      digitalWrite(led, LOW);  // turn the LED off by making the voltage LOW
      lastState = 0;
    }
}

我發現您的布爾數據類型可能存在一個錯誤。 布爾數據類型為真或假。 我看到您將它用於變量 lastState。 初始化為 0 是我認為編譯器不允許的。 也許您應該嘗試將 bool 變量設置為以下內容...

bool lastState = true;

if (!lastState) {
// what you want your code to perform if lastState is FALSE
}
else {
//what else you want your code to perform if lastState is TRUE
}

或者

bool lastState = true;

if (lastState) {
// what you want your code to perform if lastState is TRUE
}
else {
//what else you want your code to perform if lastState is FALSE
}

這是關於布爾數據類型的有用 pdf,可通過我的 Nextcloud 實例下載。

https://nextcloud.point2this.com/index.php/s/so3C7CzzoX3nkzQ

我知道這並不能完全解決您的問題,但希望這會有所幫助。

我走了很遠。

以下是如何使用 Arduino IDE 在 ATTiny85 上設置中斷(此示例使用數字引腳 4(芯片上的引腳 3):

#include "Arduino.h"

const byte interruptPin = 4;
const byte led = 3;
bool lastState = false;

ISR (PCINT0_vect) // this is the Interrupt Service Routine
{
  if (!lastState) {
    digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
    lastState = true;
  }
  else {
    digitalWrite(led, LOW);  // turn the LED off by making the voltage LOW
    lastState = false;
  }
}

void setup() {
  pinMode(interruptPin, INPUT_PULLUP); // set to input with an internal pullup resistor (HIGH when switch is open)
  pinMode(led, OUTPUT);

  // interrupts
  PCMSK  |= bit (PCINT4);  // want pin D4 / pin 3
  GIFR   |= bit (PCIF);    // clear any outstanding interrupts
  GIMSK  |= bit (PCIE);    // enable pin change interrupts 
}

void loop() {

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM