/*
#define ENCODER_CLK 4
#define ENCODER_DT 5
#define ENCODER_SW 2
void encoderPulse() {
}
void encoderPrint() {
}
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT_PULLUP);
pinMode(ENCODER_DT, INPUT_PULLUP);
pinMode(ENCODER_SW, INPUT_PULLUP);
encoderPulse();
PCICR |= (1 << PCIE2); // Activer les interruptions pour le port D
PCMSK2 |= (1 << PCINT20) | (1 << PCINT21); // Activer D4 (PCINT20) et D5 (PCINT21)
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), encoderPulse, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCODER_DT ), encoderPulse, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCODER_SW ), encoderPrint, FALLING); // Does not work on Nano!!!
}
void loop() {
}*/
int counter = 0;
void setup() {
// Configurer les broches D3, D4 et D5 en entrée avec pull-up
DDRD &= ~((1 << PD3) | (1 << PD4) | (1 << PD5)); // Configurer D3, D4 et D5 en entrée
PORTD |= (1 << PD3) | (1 << PD4) | (1 << PD5); // Activer les résistances pull-up
// Configurer les broches D4 et D5 pour interruption sur changement d'état
PCICR |= (1 << PCIE2); // Activer les interruptions pour le port D
PCMSK2 |= (1 << PCINT20) | (1 << PCINT21); // Activer D4 et D5 (PCINT20 et PCINT21)
// Configurer la broche D3 pour interruption sur front descendant
EICRA |= (1 << ISC11); // Configurer INT1 pour front descendant
EICRA &= ~(1 << ISC10); // ISC10 = 0
EIMSK |= (1 << INT1); // Activer INT1
// Initialisation série pour debug
Serial.begin(115200);
// Appeler la routine ISR(PCINT2_vect) via pointeur
void (*isrPtr)() = (void (*)())0x000A; // Pointeur vers l'adresse 0x000A
isrPtr(); // Appeler la routine à l'adresse 0x000A
}
void loop() {
// Boucle principale
}
// Gestionnaire pour D4 et D5 (port D)
ISR(PCINT2_vect) {
static int8_t pins = (PIND & 0b00110000) >> 4;
static int8_t offset[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
static char symbolArray[] = {'x', '<', '>', 'x', '>', 'x', 'x', '<', '<', 'x', 'x', '>', 'x', '>', '<', 'x'};
pins |= (PIND & 0b00110000) >> 2;
counter += offset[pins];
char symbol = symbolArray[pins];
// int8_t symbol = pins;
pins >>= 2;
// interrupts();
// Serial.write(symbol);
}
// Gestionnaire pour D3 (INT1)
ISR(INT1_vect) {
Serial.write(' ');
Serial.print(counter);
Serial.write(' ');
}