readNanoseconds()
描述
此函式只能在 PWM 脉波捕捉模式下使用,功能和 read() 相同,但回传 ns (Nanosecond)单位的脉波宽度。
语法
Enc0.readNanoseconds()
Enc1.readNanoseconds()
Enc2.readNanoseconds()
Enc3.readNanoseconds()
参数
Enc0、Enc1、Enc2、Enc3:分别对应 ENC0、ENC1、ENC2、ENC3 编码器介面。
回传
回传脉波 LOW 或 HIGH 的宽度(单位:ns),最长回传宽度约 4.2 秒。注意:readNanoseconds() 只能在 ISR 函式内被呼叫,如果在其它地方被呼叫,将总是回传 0。
范例
#include <Encoder.h>
volatile unsigned long num1 = 0L;
volatile unsigned long num2 = 0L;
void encoder_isr(int flag) { // ISR 函式
if(flag == INTR_A_PULSE_LOW)
num1 = Enc1.readNanoseconds(); // 读取脉波 LOW 宽度
else if(flag == INTR_A_PULSE_HIGH)
num2 = Enc1.readNanoseconds(); // 读取脉波 HIGH 宽度
}
void setup() {
Serial.begin(9600);
Enc1.begin(MODE_CAPTURE);
Enc1.attachInterrupt(encoder_isr); // 挂载 ISR 函式并启动中断功能
}
void loop() {
Serial.print("LOW:");
Serial.print(num1); // 印出脉波 LOW 的宽度,单位:ns
Serial.print(" ");
Serial.print("HIGH:");
Serial.println(num2); // 印出脉波 HIGH 的宽度,单位:ns
delay(100);
}
See also
- begin()
- attachInterrupt()
The text of the 86Duino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.
