-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.h
55 lines (43 loc) · 967 Bytes
/
encoder.h
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
#ifndef _ENCODER_H_
#define _ENCODER_H_
#define MIN 0
#define MAX 999
#define pin_A 6
#define pin_B 5
#define PIN PIND //or PINC or PINB
volatile uint8_t RON_mode = 0;
volatile uint8_t A_state = 0;
volatile uint8_t B_state = 0;
volatile int R_count = 0;
void encoder_rotary(void)
{
if (!(PIN & (1<<pin_B)) && (PIN & (1<<pin_A)) && !B_state) { // detect direction
B_state = 0;
A_state = 1;
}
if (!(PIN & (1<<pin_A)) && (PIN & (1<<pin_B)) && !A_state) { // detect direction
A_state = 0;
B_state = 1;
}
if ((PIN & (1<<pin_B)) && (PIN & (1<<pin_A))) { //both are zero = reset
A_state = 0;
B_state = 0;
}
if (!(PIN & (1<<pin_B)) && !(PIN & (1<<pin_A))) { //check if encoder movment continues
if (A_state) {
if (R_count < MAX)
{
R_count++;
}
A_state = 0; //reset
}
if (B_state) {
if ((R_count > MIN) | (RON_mode && (R_count > MIN-1)))
{
R_count--;
}
B_state = 0; // reset
}
}
}
#endif