-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinChangeInterrupt.ino
56 lines (46 loc) · 1.18 KB
/
pinChangeInterrupt.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
/*
* Example for using the rotary encoder with pin change interrupts
*/
#include <KY040.h>
#define CLK_PIN 5 // aka. A
#define DT_PIN 4 // aka. B
KY040 g_rotaryEncoder(CLK_PIN,DT_PIN);
// Rotary encoder value (will be set in ISR)
volatile int v_value=0;
// Enable pin change interrupt
void pciSetup(byte pin) {
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}
// ISR to handle pin change interrupt for D0 to D7 here
ISR (PCINT2_vect) {
// Process pin state
switch (g_rotaryEncoder.getRotation()) {
case KY040::CLOCKWISE:
v_value++;
break;
case KY040::COUNTERCLOCKWISE:
v_value--;
break;
}
}
void setup() {
Serial.begin(9600);
// Set pin change interrupt for CLK and DT
pciSetup(CLK_PIN);
pciSetup(DT_PIN);
}
void loop() {
static int lastValue = 0;
int value;
// Get rotary encoder value set in ISR
cli();
value = v_value;
sei();
// Show, if value has changed
if (lastValue != value) {
Serial.println(value);
lastValue = value;
}
}