// Il suffit de définir ses PINs et d'appeler dans le loop ReadRotary() #define MLCLK VALBIT1 // B1=CLK (tout à droite, opposé au -) #define MLDATA VALBIT0 // B0=DATA (à gauche de CLK) // Retourne la variation 1, 0, ou -1 int ReadRotary() { // Robust Rotary encoder reading - Copyright John Main - best-microcontroller-projects.com // A vald CW or CCW move returns 1, invalid returns 0. static uint8_t prevNextCode = 0; static uint16_t store = 0; static int8_t rot_enc_table[] = {0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0}; TU_ENTIER16 v16 = GPIOB->regs->IDR; prevNextCode <<= 2; if (v16 & MLDATA) prevNextCode |= 0x02; if (v16 & MLCLK) prevNextCode |= 0x01; prevNextCode &= 0x0f; // If valid then store as 16 bit data. if (rot_enc_table[prevNextCode]) { store <<= 4; store |= prevNextCode; //if (store==0xd42b) return 1; //if (store==0xe817) return -1; if ((store&0xff)==0x2b) return -1; if ((store&0xff)==0x17) return 1; } return 0; }