|
试过用自带的attachInterrupt函数,也编译,但是有点奇怪,用信号去接触它不会触发,我手还没有碰到,它却能触发,像抗干扰不好,能隔空感应。
下面是百度的代码,感觉中断代码像是原生的C语言那种,看不懂,好在能用,但是只有在低电平才触发,相当于FALLING,我想要不管是从高到低,还是从低到高,只要发生了变化都触发,也就是CHANGE模式,请问该如何写。感谢
- #include "Arduino.h"
- const byte interruptPin = 0;
- const byte ledpin = 1;
- bool lastState = HIGH;
- unsigned long lastMillis = 0;
- unsigned long currentMillis = 0;
- ISR (PCINT0_vect) // this is the Interrupt Service Routine
- {
- currentMillis = millis();
- if (currentMillis - lastMillis > 500){
- lastMillis = currentMillis;
- lastState = !lastState;
- digitalWrite(ledpin,lastState);
- }
- }
- void setup() {
- pinMode(interruptPin, INPUT_PULLUP);
- pinMode(ledpin, OUTPUT);
- digitalWrite(ledpin,lastState);
- // interrupts
- PCMSK |= bit (interruptPin); // want pin
- GIFR |= bit (PCIF); // clear any outstanding interrupts
- GIMSK |= bit (PCIE); // enable pin change interrupts
- }
- void loop() {
- }
复制代码
|
|