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 模式。

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.