ESP8266 NodeMCU入门:使用中断
来自丢石头百科
- 开启中断的方法如下:
attachInterrupt(interrupt,function,mode)
其中mode可以为 HIGH、RISING、CHANGE、FALLING、LOW,分别指高电平、上升沿、电平变化、下降沿、低电平时触发。
- 本实例,我们将D5口设置为输出PWM波形,使用杜邦线连接D2和D5,并通过中断定时向串口输出内容。
int pwm_pin = D5; int int_pin = D2; long count = 0; void setup_pwm(){ analogWriteFreq(500); analogWriteRange(100); } ICACHE_RAM_ATTR void trigger(){ count ++; if(count >= 1000){ count = 0; Serial.println("Trigger"); } } void setup() { Serial.begin(115200); setup_pwm(); attachInterrupt(int_pin,trigger,RISING); analogWrite(pwmPin, 50); }
- 可使用以下方法关闭中断:
detachInterrupt(interrupt)