首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino IDE中的ATTiny85中断

Arduino IDE中的ATTiny85中断
EN

Stack Overflow用户
提问于 2021-11-16 03:45:00
回答 2查看 171关注 0票数 0

我有一个使用sparkfun程序员(https://www.sparkfun.com/products/11801)编程的ATTiny85,我使用的ATTiny板级管理器是:https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

以下是我的代码,当我将引脚2接地时,我遇到了中断工作的问题。我已经测试了LED在中断之外(在循环内)是否工作。欢迎提出任何建议。

代码语言:javascript
复制
#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;
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-25 00:12:17

我太离谱了。

以下是如何使用Arduino IDE在ATTiny85上设置中断(此示例使用数字引脚4(芯片上的引脚3):

代码语言:javascript
复制
#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() {

}
票数 0
EN

Stack Overflow用户

发布于 2021-11-16 04:21:44

我发现您的Boolean数据类型可能有一个错误。布尔数据类型要么为true,要么为false。我看到您将它用于变量lastState。初始化为等于0是我认为编译器不允许的。也许您应该尝试将bool变量设置为以下值...

代码语言:javascript
复制
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
}

代码语言:javascript
复制
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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69983525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档