ESP32 中断
中断
含义:程序被中断程序打断转去执行中断程序
将中断附加到GPIO
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode);
ISR 函数原型
void ISR()
{
//中断发生时执行的代码
}
外部中断类型
LOW 低电平触发中断
HIGH 高电平触发中断
RISING 上升沿触发中断
FALLING 下降沿触发中断
CHANGE 变化时触发中断
分离中断
detachInterrupt(uint8_t interruptNum);
最简单的中断程序代码
#include <Arduino.h>
const byte LED = 13;
const byte BUTTON = 2;
void ButtonISR()
{
if(digitalRead(BUTTON) == HIGH)
{
digitalWrite(LED, HIGH);
}else digitalWrite(LED, LOW);
}
void setup()
{
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON),ButtonISR,CHANGE);
}
void loop() {
}
PS:对于非原子操作,需要考虑同步问题
就需要用 noInterrupt() 和 interrupt() 函数来关闭和开启中断
对于在临界区中的代码,需要考虑同步问题
portENTRY_CRITICAL(&mux); //进入临界区
portEXIT_CRITICAL(&mux); //退出临界区
本文来自博客园,作者:Alaso_shuang,转载请注明原文链接:https://www.cnblogs.com/Alaso687/p/19191216

浙公网安备 33010602011771号