【问题标题】:ATTiny85 Interrupts in Arduino IDEArduino IDE 中的 ATtiny85 中断
【发布时间】:2021-12-27 04:46:25
【问题描述】:

我有一个 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;
    }
}

【问题讨论】:

    标签: c++ c arduino attiny


    【解决方案1】:

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

    我知道这并不能完全解决您的问题,但希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我走得太远了。

      以下是如何使用 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() {
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-20
        • 2020-10-25
        • 1970-01-01
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多