Absolute Rotary Encoder
EAW0J-B24-AE0128L
128 positions (clicks) but hard to turn vs KY-040......
Arduino code (no i2c ... no library )
The ino file
----------------------------
//https://www.bourns.com/docs/product-datasheets/ace.pdf?sfvrsn=2abe8ff1_3
/*
Bit/Pin correlation: b7 b6 b5 b4 b3 b2 b1 b0 = p8 p7 p6 p5 p4 p3 p2 p1
A binary “1” denotes an “open” switch and a binary “0” denotes a “closed” switch.
Positions 0-127 are seen by a clockwise rotation of the shaft.
*/
//On Arduino ground the (left) C pin of the Rotary Encoder
(between 2 and 3 pin of encoder)
//your pin arrangement here
const uint8_t rpin1=4;
const uint8_t rpin2=5;
const uint8_t rpin3=6;
const uint8_t rpin4=7;
const uint8_t rpin5=8;
const uint8_t rpin6=9;
const uint8_t rpin7=10;
const uint8_t rpin8=11;
/*use uint8_t and int for return as from
//hardware/arduino/avr/cores/arduino/wiring_digital.c
//int digitalRead(uint8_t pin)
*/
int bit0;
int bit1;
int bit2;
int bit3;
int bit4;
int bit5;
int bit6;
int bit7;
//bit7,bit6,bit5,bit4,bit3,bit2,bit1,bit0
int allBitsAsInt;
int rotaryPreviusPosition=-1;
int rotaryCurrentPosition;
//PROGMEM??
const uint8_t encoderMap_12345678[256] = {
0xFF,0x38,0x28,0x37,0x18,0xFF,0x27,0x34,0x08,0x39,0xFF,0xFF,0x17,0xFF,0x24,0x0D,
0x78,0xFF,0x29,0x36,0xFF,0xFF,0xFF,0x35,0x07,0xFF,0xFF,0xFF,0x14,0x13,0x7D,0x12,
0x68,0x69,0xFF,0xFF,0x19,0x6A,0x26,0xFF,0xFF,0x3A,0xFF,0xFF,0xFF,0xFF,0x25,0x0E,
0x77,0x76,0xFF,0xFF,0xFF,0x6B,0xFF,0xFF,0x04,0xFF,0x03,0xFF,0x6D,0x6C,0x02,0x01,
0x58,0xFF,0x59,0xFF,0xFF,0xFF,0xFF,0x33,0x09,0x0A,0x5A,0xFF,0x16,0x0B,0xFF,0x0C,
0xFF,0xFF,0x2A,0x2B,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x15,0xFF,0x7E,0x7F,
0x67,0xFF,0x66,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x5B,0xFF,0xFF,0xFF,0xFF,0xFF,
0x74,0x75,0xFF,0xFF,0x73,0xFF,0xFF,0xFF,0x5D,0x5E,0x5C,0xFF,0x72,0x5F,0x71,0x00,
0x48,0x47,0xFF,0x44,0x49,0xFF,0xFF,0x1D,0xFF,0x46,0xFF,0x45,0xFF,0xFF,0x23,0x22,
0x79,0xFF,0x7A,0xFF,0x4A,0xFF,0xFF,0x1E,0x06,0xFF,0x7B,0xFF,0xFF,0xFF,0x7C,0x11,
0xFF,0xFF,0xFF,0x43,0x1A,0xFF,0x1B,0x1C,0xFF,0x3B,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x05,0xFF,0xFF,0xFF,0x6E,0xFF,0x6F,0x10,
0x57,0x54,0xFF,0x2D,0x56,0x55,0xFF,0x32,0xFF,0xFF,0xFF,0x2E,0xFF,0xFF,0xFF,0x21,
0xFF,0x53,0xFF,0x2C,0x4B,0xFF,0xFF,0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x20,
0x64,0x3D,0x65,0x42,0xFF,0x3E,0xFF,0x31,0x63,0x3C,0xFF,0x2F,0xFF,0xFF,0xFF,0x30,
0x4D,0x52,0x4E,0x41,0x4C,0x3F,0xFF,0x40,0x62,0x51,0x4F,0x50,0x61,0x60,0x70,0xFF };
void readRotaryCurrentPosition(){
bit0=digitalRead(rpin1);
bit1=digitalRead(rpin2);
bit2=digitalRead(rpin3);
bit3=digitalRead(rpin4);
bit4=digitalRead(rpin5);
bit5=digitalRead(rpin6);
bit6=digitalRead(rpin7);
bit7=digitalRead(rpin8);
allBitsAsInt = bit7;
allBitsAsInt <<= 1 ; allBitsAsInt += bit6;
allBitsAsInt <<= 1 ; allBitsAsInt += bit5;
allBitsAsInt <<= 1 ; allBitsAsInt += bit4;
allBitsAsInt <<= 1 ; allBitsAsInt += bit3;
allBitsAsInt <<= 1 ; allBitsAsInt += bit2;
allBitsAsInt <<= 1 ; allBitsAsInt += bit1;
allBitsAsInt <<= 1 ; allBitsAsInt += bit0;
rotaryCurrentPosition=encoderMap_12345678[allBitsAsInt];
}
void printForDebug(){// "Endianness" .... little?
Serial.print("( ");
Serial.print(bit7);Serial.print(" , ");
Serial.print(bit6);Serial.print(" , ");
Serial.print(bit5);Serial.print(" , ");
Serial.print(bit4);Serial.print(" , ");
Serial.print(bit3);Serial.print(" , ");
Serial.print(bit2);Serial.print(" , ");
Serial.print(bit1);Serial.print(" , ");
Serial.print(bit0);Serial.print(" ) ");
Serial.print("allBitsAsInt= ");
Serial.print(allBitsAsInt);
Serial.print(" -> ");
Serial.print(rotaryCurrentPosition);
Serial.println();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(rpin1,INPUT_PULLUP);
pinMode(rpin2,INPUT_PULLUP);
pinMode(rpin3,INPUT_PULLUP);
pinMode(rpin4,INPUT_PULLUP);
pinMode(rpin5,INPUT_PULLUP);
pinMode(rpin6,INPUT_PULLUP);
pinMode(rpin7,INPUT_PULLUP);
pinMode(rpin8,INPUT_PULLUP);
// really need this ?
rotaryPreviusPosition=-1;
readRotaryCurrentPosition();
}
void loop() {
readRotaryCurrentPosition();
if(rotaryPreviusPosition!=rotaryCurrentPosition){
rotaryPreviusPosition=rotaryCurrentPosition;
//printForDebug();
Serial.println(rotaryCurrentPosition);
}
else{
//Serial.println("user didnt turn the encoder");
}
delay(10);
}//end loop
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου