我有一个使用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在中断之外(在循环内)是否工作。欢迎提出任何建议。
#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;
}
}发布于 2021-11-25 00:12:17
我太离谱了。
以下是如何使用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() {
}发布于 2021-11-16 04:21:44
我发现您的Boolean数据类型可能有一个错误。布尔数据类型要么为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
我知道这并不能完全解决你的问题,但希望这能对你有所帮助。
https://stackoverflow.com/questions/69983525
复制相似问题