setUnderflow()

描述

開啟計數器數值回捲時發出中斷的功能(86Duino 編碼器的內部計數器值,總是會在遞減到 0 後,回到上限值)。從 86Duino Coding 105 版本以後才能使用這個函式,並且只可在 Pulse/DIR、CW/CCW、A/B Pulse 工作模式下使用。

語法


Enc0.setUnderflow()
Enc0.setUnderflow(condition)

Enc1.setUnderflow()
Enc1.setUnderflow(condition)

Enc2.setUnderflow()
Enc2.setUnderflow(condition)

Enc3.setUnderflow()
Enc3.setUnderflow(condition)

參數

Enc0、Enc1、Enc2、Enc3:分別對應 ENC0、ENC1、ENC2、ENC3 編碼器介面。

condition:true 或 false。如果要啟動計數值回捲的中斷事件,則設定 true,反之則設定 false。(預設值是 true)

回傳

無回傳值

範例

#include "Encoder.h"

volatile int ovnumber = 0;
volatile int uvnumber = 0;
void encoder_callback(int flag) {
  if(flag == INTR_OVERFLOW) // 假如是計數溢出的中斷事件
    ovnumber++;
  else if(flag == INTR_UNDERFLOW) // 假如是計數回捲的中斷事件
    uvnumber++;
}

void setup() {
  Serial.begin(9600);
  Enc0.begin(MODE_AB_PHASE);
  Enc0.setOverflow();
  Enc0.setUnderflow();
  Enc0.setRange(200); // 設定上限值為 200
  Enc0.attachInterrupt(encoder_callback); // 掛載 callback 函式並啟動中斷功能
}

void loop() {
  Serial.print("count = ");
  Serial.print(Enc0.read()); // 讀取編碼器的計數值
  Serial.print(" ");
  Serial.print("overflow = ");
  Serial.print(ovnumber); // 印出計數溢出的次數
  Serial.print(" ");
  Serial.print("underflow = ");
  Serial.println(uvnumber); // 印出計數回捲的次數
  
  delay(100);
}

See also

setOverflow()
setRange()
setComparator()
setIndexReset()


函式庫參考主頁面

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.