Noduino Interrupts
来自Jack's Lab
(版本间的差异)
(→Examples) |
|||
第11行: | 第11行: | ||
[[文件:RotaryEncoderWaveform.gif]] | [[文件:RotaryEncoderWaveform.gif]] | ||
− | Use an encoder example | + | Use an encoder example: |
+ | |||
+ | * D8 connect to the channel A to receive the interrupt (rising) | ||
+ | * D9 connect to the channel B | ||
<source lang=cpp> | <source lang=cpp> | ||
+ | #include "noduino.h" | ||
+ | |||
+ | static int32_t pos = 0; | ||
+ | static bool past_b = 0; | ||
+ | |||
+ | void do_encoder_a() | ||
+ | { | ||
+ | past_b = (bool)digitalRead(D9); | ||
+ | past_b ? pos++ : pos--; | ||
+ | serial_printf("pos = %d\r\n", pos); | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | serial_begin(115200); | ||
+ | |||
+ | pinMode(D9, INPUT); | ||
+ | |||
+ | attachInterrupt(D8, do_encoder_a, RISING); | ||
+ | } | ||
+ | void loop() | ||
+ | { | ||
+ | } | ||
</source> | </source> | ||
2016年8月28日 (日) 08:39的版本
1 Overview
- attachInterrupt()
- detachInterrupt()
- noInterrupts()
2 Examples
Use an encoder example:
- D8 connect to the channel A to receive the interrupt (rising)
- D9 connect to the channel B
#include "noduino.h" static int32_t pos = 0; static bool past_b = 0; void do_encoder_a() { past_b = (bool)digitalRead(D9); past_b ? pos++ : pos--; serial_printf("pos = %d\r\n", pos); } void setup() { serial_begin(115200); pinMode(D9, INPUT); attachInterrupt(D8, do_encoder_a, RISING); } void loop() { }