“ESP8266 NodeMCU入门:使用中断”的版本间的差异
来自丢石头百科
第1行: | 第1行: | ||
− | * | + | * 开启中断的方法如下: |
+ | attachInterrupt(interrupt,function,mode) | ||
+ | 其中mode可以为 HIGH、RISING、CHANGE、FALLING、LOW,分别指高电平、上升沿、电平变化、下降沿、低电平时触发。 | ||
+ | * 本实例,我们将D5口设置为输出PWM波形,使用杜邦线连接D2和D5,并通过中断定时向串口输出内容。 | ||
int pwm_pin = D5; | int pwm_pin = D5; | ||
int int_pin = D2; | int int_pin = D2; | ||
long count = 0; | long count = 0; | ||
− | |||
void setup_pwm(){ | void setup_pwm(){ | ||
analogWriteFreq(500); | analogWriteFreq(500); | ||
第21行: | 第23行: | ||
Serial.begin(115200); | Serial.begin(115200); | ||
setup_pwm(); | setup_pwm(); | ||
− | attachInterrupt(int_pin, | + | attachInterrupt(int_pin,trigger,RISING); |
analogWrite(pwmPin, 50); | analogWrite(pwmPin, 50); | ||
} | } | ||
+ | |||
+ | * 可使用以下方法关闭中断: | ||
+ | detachInterrupt(interrupt) |
2020年8月14日 (五) 20:48的版本
- 开启中断的方法如下:
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)