137 lines
1.7 KiB
C
137 lines
1.7 KiB
C
/*
|
|
* rotaryEncoder.c
|
|
*
|
|
* Created: 16/12/2021 16:34:05
|
|
* Author: n0x
|
|
*/
|
|
|
|
#include "rotaryEncoder.h"
|
|
|
|
#define ROTA PORTC7
|
|
#define ROTB PORTC6
|
|
#define ROTBUTTON PORTC5
|
|
|
|
/* enums */
|
|
enum{
|
|
LEFT1, LEFT2, LEFT3, RIGHT1, RIGHT2, RIGHT3,
|
|
INIT, WAIT
|
|
} RotaryState = INIT; /* Enum for the rotary enc */
|
|
|
|
int16_t count;
|
|
|
|
void
|
|
drehgeber_init(void)
|
|
{
|
|
DDRC &= ~(7<<DDC5); /* Set bit 5-7 of Data Direction Register C as input */
|
|
PORTC |= (7<<PORTC5); /* Initialize bit 5-7 of PORTC as for pull up resistor */
|
|
|
|
/* With external interrupt */
|
|
PCICR |= (1<<PCIE2);
|
|
PCMSK2 |= (1<<PCINT23)|(1<<PCINT22);
|
|
}
|
|
|
|
ISR(PCINT2_vect)
|
|
{
|
|
drehgeber_process();
|
|
}
|
|
|
|
void
|
|
drehgeber_process(void)
|
|
{
|
|
uint8_t a,b, enc;
|
|
a = ((PINC & (1<<ROTA)) == 0);
|
|
b = ((PINC & (1<<ROTB)) == 0)<<1;
|
|
enc=a+b;
|
|
|
|
switch(RotaryState)
|
|
{
|
|
case INIT:
|
|
RotaryState = WAIT;
|
|
break;
|
|
|
|
case WAIT:
|
|
if(enc == 1)
|
|
{
|
|
RotaryState = LEFT1;
|
|
}
|
|
if(enc == 2)
|
|
{
|
|
RotaryState = RIGHT1;
|
|
}
|
|
break;
|
|
|
|
case LEFT1:
|
|
if(enc == 3)
|
|
{
|
|
RotaryState = LEFT2;
|
|
}
|
|
if(enc == 0)
|
|
{
|
|
RotaryState = WAIT;
|
|
}
|
|
break;
|
|
|
|
case LEFT2:
|
|
if(enc == 2)
|
|
{
|
|
RotaryState = LEFT3;
|
|
}
|
|
if(enc == 1)
|
|
{
|
|
RotaryState = LEFT1;
|
|
}
|
|
break;
|
|
|
|
case LEFT3:
|
|
if(enc == 0)
|
|
{
|
|
count++;
|
|
RotaryState = WAIT;
|
|
}
|
|
if(enc == 3)
|
|
{
|
|
RotaryState = LEFT2;
|
|
}
|
|
break;
|
|
|
|
case RIGHT1:
|
|
if(enc == 3)
|
|
{
|
|
RotaryState = RIGHT2;
|
|
}
|
|
if(enc == 0)
|
|
{
|
|
RotaryState = WAIT;
|
|
}
|
|
break;
|
|
|
|
case RIGHT2:
|
|
if(enc == 1)
|
|
{
|
|
RotaryState = RIGHT3;
|
|
}
|
|
if(enc == 2)
|
|
{
|
|
RotaryState = RIGHT1;
|
|
}
|
|
break;
|
|
|
|
|
|
case RIGHT3:
|
|
if(enc == 0)
|
|
{
|
|
count--;
|
|
RotaryState = WAIT;
|
|
}
|
|
if(enc == 3)
|
|
{
|
|
RotaryState = RIGHT2;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
int16_t drehgeber_get(void)
|
|
{
|
|
return count;
|
|
} |