-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.c
More file actions
137 lines (122 loc) · 3.79 KB
/
encoder.c
File metadata and controls
137 lines (122 loc) · 3.79 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "utils/uartstdio.h"
#include "encoder.h"
volatile static encoder_count_t enc0 = 0, enc1 = 0;
static signed dir0, dir1; // direction to count (up/down) for each encoder
void InitializeEncoders(tBoolean invert0, tBoolean invert1)
{
// enable and configure the GPIO pins
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // enable the peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); // enable the peripheral
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_6); // configure pins as inputs
GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_5 | GPIO_PIN_6); // configure pins as inputs
// enable and configure the interrupts
IntEnable(INT_GPIOB); // enable interrupts for the periph
IntEnable(INT_GPIOC); // enable interrupts for the periph
GPIOIntTypeSet(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_6, GPIO_BOTH_EDGES); // configure the interrupts
GPIOIntTypeSet(GPIO_PORTC_BASE, GPIO_PIN_5 | GPIO_PIN_6, GPIO_BOTH_EDGES); // configure the interrupts
GPIOPinIntEnable(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_6); // enable the interrupt for the pins
GPIOPinIntEnable(GPIO_PORTC_BASE, GPIO_PIN_5 | GPIO_PIN_6); // enable the interrupt for the pins
dir0 = invert0 ? -1 : 1;
dir1 = invert1 ? -1 : 1;
}
void GetEncoderCounts(encoder_count_t *pEncCount0, encoder_count_t *pEncCount1)
{
*pEncCount0 = enc0;
*pEncCount1 = enc1;
}
encoder_count_t GetEncoderCount(encoder_t encoder)
{
switch(encoder)
{
case ENCODER_0:
return enc0;
case ENCODER_1:
return enc1;
default:
return 0;
}
}
void PresetEncoderCounts(encoder_count_t new_count0, encoder_count_t new_count1)
{
enc0 = new_count0;
enc1 = new_count1;
}
void PresetEncoderCount(encoder_t encoder, encoder_count_t new_count)
{
switch(encoder)
{
case ENCODER_0:
enc0 = new_count;
break;
case ENCODER_1:
enc1 = new_count;
break;
}
}
void EncoderInterruptHandler(void)
{
unsigned static char lastState0 = 0;
unsigned static char lastState1 = 0;
unsigned char pin;
unsigned port;
// ENC1A PB4
// ENC1B PC5
// ENC2A PB6
// ENC2B PC6
/*
0 => 1
1 => 3
2 => 0
3 => 2
0 => 2
1 => 0
2 => 3
3 => 1
*/
if((GPIOPinIntStatus(port=GPIO_PORTC_BASE, false) & (pin=GPIO_PIN_5))
|| GPIOPinIntStatus(port=GPIO_PORTB_BASE, false) & (pin=GPIO_PIN_4))
{
// encoder 0 fired!
unsigned char state = (GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_4) ? 2 : 0);
state += (GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_5) ? 1 : 0);
if((lastState0 == 0 && state == 1)
|| (lastState0 == 1 && state == 3)
|| (lastState0 == 2 && state == 0)
|| (lastState0 == 3 && state == 2)) {
enc0 += dir0;
}
else if((lastState0 == 0 && state == 2)
|| (lastState0 == 1 && state == 0)
|| (lastState0 == 2 && state == 3)
|| (lastState0 == 3 && state == 1)) {
enc0 -= dir0;
}
lastState0 = state;
}
else if(GPIOPinIntStatus(port=GPIO_PORTC_BASE, false) & (pin=GPIO_PIN_6)
|| GPIOPinIntStatus(port=GPIO_PORTB_BASE, false) & (pin=GPIO_PIN_6))
{
unsigned char state = (GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_6) ? 2 : 0);
state += (GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_6) ? 1 : 0);
if((lastState1 == 0 && state == 1)
|| (lastState1 == 1 && state == 3)
|| (lastState1 == 2 && state == 0)
|| (lastState1 == 3 && state == 2)) {
enc1 += dir1;
}
else if((lastState1 == 0 && state == 2)
|| (lastState1 == 1 && state == 0)
|| (lastState1 == 2 && state == 3)
|| (lastState1 == 3 && state == 1)) {
enc1 -= dir1;
}
lastState1 = state;
} else return;
GPIOPinIntClear(port, pin);
}