begin()
Description
Initialize 86Duino encoder interface and set operating mode.
Syntax
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)
Parameters
Enc0、Enc1、Enc2、Enc3
:Corresponding to ENC0, ENC1, ENC2 and ENC3 encoder interfaces.
mode
:The interface can be set in one of the following 5 modes:
MODE_STEP_DIR
:Pulse/DIR incremental mode(Wiring example)MODE_CWCCW
:CW/CCW incremental mode(Wiring example)MODE_AB_PHASE
:A/B Pulse incremental mode(Wiring example)MODE_SSI
:SSI absolute mode(Wiring example)MODE_CAPTURE
:PWM pulse width capture mode.
Note:The internal electronic circuit of 86Duino PLC is designed for PLC application, when you select 86Duino PLC board on 86Duino IDE, it does not support SSI mode in the Encoder library.
bits
:MODE_SSI
Additional setting for the MODE_SSI mode, to set the width, preset to 12-bit, maximum is 32-bit.
clock
:MODE_SSI
Additional setting for the MODE_SSI mode, set the SSI communication rate, preset to 1,000,000 (Hz)
wtime
:MODE_SSI
Additional setting for the MODE_SSI mode, set the SSI wait time between data transmission, preset to 20 us.
gray2bin
:MODE_SSI
Additional setting for the MODE_SSI mode (true/false), to configure Gray Code conversion on or off. When turn on, after reading encoder value, the hardware automatically convert the return value from Gray Code to Binary. The preset value is false.
Returns
None
Example
Set ENCO to Pulse/DIR mode, and read the encoder’s counter value.
#include <Encoder.h> void setup() { Serial.begin(9600); Enc0.begin(MODE_STEP_DIR); } void loop() { Serial.println(Enc0.read()); // Read encoder counter value delay(100); }
Set ENC1 to PWM pulse capture mode, read the pulse width for digital HIGH and LOW condition.
#include <Encoder.h> volatile unsigned long num1 = 0L; volatile unsigned long num2 = 0L; void encoder_isr(int flag) { // ISR function if (flag == INTR_A_PULSE_LOW) // Read pulse width during digital LOW condition num1 = Enc1.read(); else if (flag == INTR_A_PULSE_HIGH) // Read pulse width during digital HIGH condition num2 = Enc1.read(); } void setup() { Serial.begin(9600); Enc1.begin(MODE_CAPTURE); Enc1.attachInterrupt(encoder_isr); // Mount ISR function and enable interrupt } void loop() { Serial.print("LOW:"); Serial.print(num1); // Output pulse width during digital LOW condition in micro-second (us) Serial.print(" "); Serial.print("HIGH:"); Serial.println(num2); // Output pulse width during digital HIGH condition in micro-second (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.