-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEncoder.ino
98 lines (74 loc) · 2.85 KB
/
Encoder.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Application note: incremental encoder and RS422/RS485 Shield
* Version 1.0
* Copyright (C) 2020 Hartmut Wendt www.zihatec.de
*
* used encoder: SICK DFS60 www.sick.com
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Pin definitions
const byte ZeroPin = 2; // RS422 input for Z channel (zero detection)
const byte COSPin = 3; // RS422 input for A channel (cos signal)
const byte SINPin = 4; // RS422 input for B channel (sin signal)
// variables
enum {ENC_STOP, ENC_CLOCKWISE_ROTATION, ENC_COUNTERCLOCKWISE_ROTATION}; // encoder operation modes
volatile byte encoder_state = ENC_STOP;
volatile int encoder_position = 0;
volatile int encoder_oldpos = 0;
void setup() {
Serial.begin(115200); //Use serial monitor for debugging
// Define pins for input and output
pinMode(SINPin, INPUT);
// set internal pullup resistor for interrupt pin
pinMode(COSPin, INPUT_PULLUP);
pinMode(ZeroPin, INPUT_PULLUP);
// set 1st interrupt service routine to COSPin and 'RISING' edge
attachInterrupt(digitalPinToInterrupt(COSPin), encoder_isr, RISING);
// set 2nd interrupt service routine to ZeroPin and 'HIGH' level
attachInterrupt(digitalPinToInterrupt(ZeroPin), zero_detection_isr, HIGH);
}
void loop() {
// Detect Encoder Stop
if (encoder_oldpos == encoder_position) encoder_state= ENC_STOP;
// output encoder incremental and status
Serial.print("Encoder position: ");
Serial.print(encoder_position);
Serial.print(", Encoder state: ");
if (encoder_state==ENC_CLOCKWISE_ROTATION) {
Serial.println("Clockwise Rotation");
} else if (encoder_state==ENC_COUNTERCLOCKWISE_ROTATION) {
Serial.println("Counter-Clockwise Rotation");
} else {
Serial.println("Stop");
}
encoder_oldpos = encoder_position;
delay(500);
}
void encoder_isr() {
if (digitalRead(SINPin) == LOW) {
// clockwise rotation
encoder_state=ENC_CLOCKWISE_ROTATION;
encoder_position++;
} else {
//counter-clockwise rotation
encoder_state=ENC_COUNTERCLOCKWISE_ROTATION;
encoder_position--;
}
}
void zero_detection_isr() {
// detect pulse on zero channel
encoder_position = 0;
}