begin()

描述

初始化 86Duino 编码器介面,并指定工作模式。

语法


Enc0.begin(mode)
Enc0.begin(mode, bits)
Enc0.begin(mode, bits, clock)
Enc0.begin(mode, bits, clock, wtime)
Enc0.begin(mode, bits, clock, wtime, gray2bin)

Enc1.begin(mode)
Enc1.begin(mode, bits)
Enc1.begin(mode, bits, clock)
Enc1.begin(mode, bits, clock, wtime)
Enc1.begin(mode, bits, clock, wtime, gray2bin)

Enc2.begin(mode)
Enc2.begin(mode, bits)
Enc2.begin(mode, bits, clock)
Enc2.begin(mode, bits, clock, wtime)
Enc2.begin(mode, bits, clock, wtime, gray2bin)

Enc3.begin(mode)
Enc3.begin(mode, bits)
Enc3.begin(mode, bits, clock)
Enc3.begin(mode, bits, clock, wtime)
Enc3.begin(mode, bits, clock, wtime, gray2bin)

参数

Enc0、Enc1、Enc2、Enc3:分别对应 ENC0、ENC1、ENC2、ENC3 编码器介面。

mode:有五种模式可选择

  1. MODE_STEP_DIR:Pulse/DIR 增量编码器工作模式(接线范例
  2. MODE_CWCCW:CW/CCW 增量编码器工作模式(接线范例
  3. MODE_AB_PHASE:A/B Pulse 增量编码器工作模式(接线范例
  4. MODE_SSI:SSI 绝对编码器工作模式(接线范例
  5. MODE_CAPTURE:PWM 脉波捕捉模式

  6. 请注意:86Duino PLC 内部硬体电路为了特定用途而设计,此函式库在选用 86Duino PLC 开发板的情况下,不支援 SSI 模式。

(请注意:86Duino PLC 内部硬体电路为了特定用途而设计,此函式库在 86Duino IDE 中选择 86Duino PLC 开发板的情况下,不支援 SSI 模式。)

bitsMODE_SSI 模式的附加设定,用于设定 SSI 绝对编码器的位元宽度,预设值是 12(位元),最高可设定到 32(位元)。

clockMODE_SSI 模式的附加设定,用于设定 SSI 的通信速率,预设是 1000000(Hz)。

wtimeMODE_SSI 模式的附加设定,用于设定 SSI Wait Time (两次 SSI 传输之间的等待时间),预设是 20(us)。

gray2binMODE_SSI 模式的附加设定,输入值为 true 或 false,用于指定是否开启 Gray Code 转换功能,开启后,硬体会自动对 SSI 读到的编码器回传值进行 Gray Code to Binary 转换。预设值是 false。

回传

无回传值

范例

设定 ENC0 为 Pulse/DIR 模式,并读取编码器 count 数:

#include <Encoder.h>

void setup() {
   Serial.begin(9600);
   Enc0.begin(MODE_STEP_DIR);
}

void loop() {
   Serial.println(Enc0.read()); // 读取编码器计数器数值
   delay(100);
}

设定 ENC1 为 PWM 脉波捕捉模式,并读取脉波的 HIGH、LOW 宽度:

#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) // 读取脉波 LOW 宽度
      num1 = Enc1.read();
    else if (flag == INTR_A_PULSE_HIGH) // 读取脉波 HIGH 宽度
      num2 = Enc1.read();
}

void setup() {
   Serial.begin(9600);
   Enc1.begin(MODE_CAPTURE);
   Enc1.attachInterrupt(encoder_isr); // 挂载 ISR 函式并启动中断功能
}

void loop() {
   Serial.print("LOW:");
   Serial.print(num1); // 印出脉波 LOW 的宽度,单位:us
   Serial.print("    ");
   Serial.print("HIGH:");
   Serial.println(num2); // 印出脉波 HIGH 的宽度,单位:us
   delay(100);
}

See also

attachInterrupt()
read()
setIndexReset()
setComparator()


函式库参考主页面

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.