148 lines
1.8 KiB
C
148 lines
1.8 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{
|
||
|
S2, S3, S4, S5, S6, S7,
|
||
|
INIT, WAIT
|
||
|
} RotaryState = INIT; /* Emus for the stoplight task (task 3) */
|
||
|
|
||
|
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 Timer */
|
||
|
// TCCR2A |= (1<<WGM21);
|
||
|
// TCCR2B |= (3<<CS20);
|
||
|
// TIMSK2 |= (1<<OCIE2A);
|
||
|
// OCR2A = 250 - 1;
|
||
|
|
||
|
/* With external interrupt */
|
||
|
PCICR |= (1<<PCIE2);
|
||
|
PCMSK2 |= (1<<PCINT23)|(1<<PCINT22);
|
||
|
}
|
||
|
|
||
|
// ISR(TIMER2_COMPA_vect)
|
||
|
// {
|
||
|
// drehgeber_process();
|
||
|
// }
|
||
|
|
||
|
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 = S2;
|
||
|
}
|
||
|
if(enc == 2)
|
||
|
{
|
||
|
RotaryState = S5;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case S2:
|
||
|
if(enc == 3)
|
||
|
{
|
||
|
RotaryState = S3;
|
||
|
}
|
||
|
if(enc == 0)
|
||
|
{
|
||
|
RotaryState = WAIT;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case S3:
|
||
|
if(enc == 2)
|
||
|
{
|
||
|
RotaryState = S4;
|
||
|
}
|
||
|
if(enc == 1)
|
||
|
{
|
||
|
RotaryState = S2;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case S4:
|
||
|
if(enc == 0)
|
||
|
{
|
||
|
count++;
|
||
|
RotaryState = WAIT;
|
||
|
}
|
||
|
if(enc == 3)
|
||
|
{
|
||
|
RotaryState = S3;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case S5:
|
||
|
if(enc == 3)
|
||
|
{
|
||
|
RotaryState = S6;
|
||
|
}
|
||
|
if(enc == 0)
|
||
|
{
|
||
|
RotaryState = WAIT;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case S6:
|
||
|
if(enc == 1)
|
||
|
{
|
||
|
RotaryState = S7;
|
||
|
}
|
||
|
if(enc == 2)
|
||
|
{
|
||
|
RotaryState = S5;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
|
||
|
case S7:
|
||
|
if(enc == 0)
|
||
|
{
|
||
|
count--;
|
||
|
RotaryState = WAIT;
|
||
|
}
|
||
|
if(enc == 3)
|
||
|
{
|
||
|
RotaryState = S6;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int16_t drehgeber_get(void)
|
||
|
{
|
||
|
return count;
|
||
|
}
|