“ESP8266 NodeMCU入门:使用中断”的版本间的差异
来自丢石头百科
(未显示同一用户的2个中间版本) | |||
第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); | ||
第11行: | 第13行: | ||
ICACHE_RAM_ATTR void trigger(){ | ICACHE_RAM_ATTR void trigger(){ | ||
+ | //每次触发中断时count加1,到达1000后复位成0,并在串口输出"Trigger" | ||
count ++; | count ++; | ||
if(count >= 1000){ | if(count >= 1000){ | ||
第21行: | 第24行: | ||
Serial.begin(115200); | Serial.begin(115200); | ||
setup_pwm(); | setup_pwm(); | ||
− | attachInterrupt(int_pin, | + | attachInterrupt(int_pin,trigger,RISING); |
− | analogWrite( | + | analogWrite(pwm_pin, 50); |
} | } | ||
+ | |||
+ | 串口将在每1000次触发中断时输出"Trigger"。 | ||
+ | |||
+ | * 可使用以下方法关闭中断: | ||
+ | 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);