-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.c
45 lines (37 loc) · 1.29 KB
/
Encoder.c
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
#include "msp.h"
#include "encoder.h"
#include "stdio.h"
#include "stdbool.h"
bool ccwFlag = false, cwFlag = false, swFlag = false;
volatile int rotaryPos = 0;
void encoder_Setup(void){
ENCODER->SEL0 &=~ ENC_SETUP;
ENCODER->SEL1 &=~ ENC_SETUP;
ENCODER->DIR &=~ ENC_SETUP; // Inputs
ENCODER->REN |= ENC_SETUP;
ENCODER->OUT |= ENC_SETUP; // Data Output Register -> Pull-up
ENCODER->IE |= ENC_SETUP; // Interrupt Enable Register
ENCODER->IES &=~ ENC_SETUP; // Interrupt Edge Select Register
ENCODER->IFG &=~ ENC_SETUP; // Clear Interrupt Flags
NVIC->ISER[1] = 1 << ((PORT3_IRQn) & 31); // Enabled interrupts for pushbutton detection
__enable_interrupt();
}
void PORT3_IRQHandler(void){
if(ENCODER->IFG & ENC_SW)
swFlag = true;
if(ENCODER->IFG & ENC_CLK){
if(ENCODER->IES & ENC_CLK){ // rising edge
if(ENCODER->IN & ENC_DT)
cwFlag = true;
else if(!cwFlag)
ccwFlag = true;
}else if(!(ENCODER->IES & ENC_CLK)){ // falling edge
if(ENCODER->IN & ENC_DT)
ccwFlag = true;
else if(!ccwFlag)
cwFlag = true;
}
}
ENCODER->IES ^= ENC_CLK; // switch the edge to read (rise or fall)
ENCODER->IFG &=~ ENC_SETUP;
}