“ESP8266 NodeMCU入门:使用中断”的版本间的差异
来自丢石头百科
小 (Admin移动页面使用中断至ESP8266 NodeMCU入门:使用中断,不留重定向) |
|||
第13行: | 第13行: | ||
ICACHE_RAM_ATTR void trigger(){ | ICACHE_RAM_ATTR void trigger(){ | ||
+ | //每次触发中断时count加1,到达1000后复位成0,并在串口输出"Trigger" | ||
count ++; | count ++; | ||
if(count >= 1000){ | if(count >= 1000){ | ||
第24行: | 第25行: | ||
setup_pwm(); | setup_pwm(); | ||
attachInterrupt(int_pin,trigger,RISING); | attachInterrupt(int_pin,trigger,RISING); | ||
− | analogWrite( | + | analogWrite(pwm_pin, 50); |
} | } | ||
+ | |||
+ | 串口将在每1000次触发中断时输出"Trigger"。 | ||
* 可使用以下方法关闭中断: | * 可使用以下方法关闭中断: | ||
− | detachInterrupt(interrupt) | + | detachInterrupt(interrupt); |
2020年8月17日 (一) 13:55的最新版本
- 开启中断的方法如下:
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加1,到达1000后复位成0,并在串口输出"Trigger" count ++; if(count >= 1000){ count = 0; Serial.println("Trigger"); } } void setup() { Serial.begin(115200); setup_pwm(); attachInterrupt(int_pin,trigger,RISING); analogWrite(pwm_pin, 50); }
串口将在每1000次触发中断时输出"Trigger"。
- 可使用以下方法关闭中断:
detachInterrupt(interrupt);