attachInterrupt()
Description
Attach interrupt service to encoder interface.
Under different operating mode, the condition to trigger interrupt is different:
- Pulse/DIR, CW/CCW and A/B Pulse mode: index interrupt event (trigger by setIndexReset()), comparator interrupt event (trigger by setComparator()), counter overflow interrupt event (trigger by setRange()), counter underflow interrupt event (trigger by setRange()).
- PWM pulse capture mode: Pulse changing from LOW to HIGH edge trigger and pulse going from HIGH to LOW edge trigger.
- SSI mode: Does not have a mechanism to trigger interrupt.
Syntax
Enc0.attachInterrupt(isr)
Enc1.attachInterrupt(isr)
Enc2.attachInterrupt(isr)
Enc3.attachInterrupt(isr)
Parameters
Enc0、Enc1、Enc2、Enc3:Correspond to ENC0, ENC1, ENC2 & ENC3 encoder interfaces.
isr:Interrupt service routine. This function must have an integer (int) type parameter to associate the interrupt event to one of the following 10 conditions:
INTR_COMPARE:Interrupt event triggered by comparator.INTR_INDEX:Interrupt event triggered by index.INTR_OVERFLOW:Interrupt event triggered by counter overflow. (The function is available after Coding 105)INTR_UNDERFLOW:Interrupt event trggered by counter underflow. (The function is available after Coding 105)INTR_A_PULSE_LOW:Interrupt event triggered by pulse on pin A, changing from LOW to HIGH.INTR_A_PULSE_HIGH:Interrupt event triggered by a pulse on Pin A, changing from HIGH to LOW.INTR_B_PULSE_LOW:Interrupt event triggered by a pulse on Pin B, changing from LOW to HIGH.INTR_B_PULSE_HIGH:Interrupt event triggered by a pulse on Pin B, changing from HIGH to LOW.INTR_Z_PULSE_LOW:Interrupt event triggered by a pulse on Pin Z, changing from LOW to HIGH.INTR_Z_PULSE_HIGH:Interrupt event triggered by a pulse on Pin Z, changing from HIGH to LOW.
In the above, condition 1 to 4 only occur under Pulse/DIR, CW/CCW and A/B Pulse operating mode. Condition 5 to 10 only occur under PWM operating mode.
Returns
None
Example
#include <Encoder.h>
volatile unsigned long num1 = 0L;
volatile unsigned long num2 = 0L;
void encoder_isr(int flag) { // ISR function
if(flag == INTR_COMPARE)
num1++; // If the counter value meet the specified value, increase the value by 1.
else if(flag == INTR_INDEX)
num2 ++; // When the motor complete one full revolution, increase the value by 1.
}
void setup() {
Serial.begin(9600);
Enc0.begin(MODE_AB_PHASE);
Enc0.setIndexReset(); // When an index signal occurs (Pin Z goes HIGH), raise an interrupt event to call ISR function.
Enc0.setComparator(20L); // When the counter reach 20, raise an interrupt to call ISR function.
Enc0.attachInterrupt(encoder_isr); // Attach ISR and initiate interrupt function.
}
void loop() {
Serial.print("Pulse number: ");
Serial.print(Enc0.read()); // Read pulse counter.
Serial.print(" ");
Serial.print("index: ");
Serial.print(num1); // Output rotary encoder total revolution.
Serial.print(" ");
Serial.print("trigger: "); // Output number of time the counter value reach 20.
Serial.println(num2);
delay(100);
}
See also
- detachInterrupt()
- setRange()
- 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.
