#include <avr/io.h>
#include <avr/interrupt.h>

#define ENC_PORT PORTD
#define ENC_DDR  DDRD
#define ENC_A PIND&(1<<2)
#define ENC_B PIND&(1<<4)
#define ENC_CCW 0
#define ENC_CW 1

void encoder_action(uint8_t dir);

volatile uint8_t encoder_direction;

ISR(INT0_vect)
{
    if (ENC_B)
    {
        if (ENC_A)
        {
            encoder_direction = ENC_CCW;
            encoder_action(0);
        }
        else
        {
            encoder_direction = ENC_CW;
        }
    }
    else
    {
        if (ENC_A)
        {
            encoder_direction = ENC_CW;
        }
        else
        {
            encoder_direction = ENC_CCW;
            encoder_action(0);
        }
    }
}	

ISR (PCINT3_vect){
    if (ENC_B)
    {
        if (ENC_A)
        {
            encoder_direction = ENC_CW;
            encoder_action(1);
        }
        else
        {
            encoder_direction = ENC_CCW;
        }
    }
    else
    {
        if (ENC_A)
        {
            encoder_direction = ENC_CCW;
        }
        else
        {
            encoder_direction = ENC_CW;
            encoder_action(1);
        }
    }
}	
       
