Arduino 中的 ATTiny85 中断 IDE

ATTiny85 Interrupts in Arduino IDE

我有一个 ATTiny85,我使用 sparkfun 编程器编程 (https://www.sparkfun.com/products/11801) and the ATTiny Board Manager I am using is: 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;
    }
}

我发现您的布尔数据类型可能存在一个错误。布尔数据类型为 true 或 false。我看到您将它用于变量 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() {

}