-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder_Module.c
More file actions
52 lines (41 loc) · 930 Bytes
/
Copy pathEncoder_Module.c
File metadata and controls
52 lines (41 loc) · 930 Bytes
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
/*
* Encoder_Module.c
*
* Created: 3/16/2022 8:21:08 PM
* Author: andre
*/
#include "Encoder_Module.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
static volatile int32_t rotary_encoder_position = 0;
int32_t Encoder_Get_Position()
{
return rotary_encoder_position;
}
void Encoder_Init()
{
rotary_encoder_position = 0;
//PORTD0: no pull-up, input (A Phase)
PORTD &= ~(1 << PORTD0);
DDRD &= ~(1 << DDD0);
//PORTD1: no pull-up, input (B Phase)
PORTD &= ~(1 << PORTD1);
DDRD &= ~(1 << DDD1);
//EICRA.ISC0 = 3 (Positive edge triggered)
EICRA |= (1 << ISC00);
//INT0 = 1 (Enable external interrupt 0)
EIMSK |= (1 << INT0);
}
ISR(INT0_vect)
{
//PORTB |= (1 << PORTB5);
if ( (!(PIND & (1 << PIND0)) && (PIND & (1 << PIND1))) || ((PIND & (1 << PIND0)) && !(PIND & (1 << PIND1)))) //Forward
{
rotary_encoder_position++;
}
else //Backward
{
rotary_encoder_position--;
}
}