Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #define EncPin1 19
- #define EncPin2 23
- volatile int8_t interruptCounter;
- long int EncoderCounter;
- hw_timer_t *timer = NULL;
- portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
- const int8_t table[16] PROGMEM = {0,0,-1,0,0,0,0,1,1,0,0,0,0,-1,0,0};
- void IRAM_ATTR Encoder();
- int8_t encode_read();
- void setup()
- {
- Serial.begin(115200);
- pinMode(EncPin1, INPUT_PULLUP);
- pinMode(EncPin2, INPUT_PULLUP);
- timer = timerBegin(0, 80, true);
- timerAttachInterrupt(timer, &Encoder, true);
- timerAlarmWrite(timer, 3000000, true);
- timerAlarmEnable(timer);
- }
- void loop()
- {
- EncoderCounter += encode_read();
- }
- void IRAM_ATTR Encoder()
- {
- static int8_t last = 0;
- int8_t EncVal1 = digitalRead(EncPin1);
- int8_t EncVal2 = digitalRead(EncPin2);
- last = (last << 2) & 0x0F;
- if (EncVal1 == HIGH)
- last |= 1;
- if (EncVal2 == HIGH)
- last |= 2;
- interruptCounter += pgm_read_byte(&table[last]);
- Serial.println(last, BIN);
- Serial.println(EncoderCounter);
- }
- int8_t encode_read(void)
- { // Encoder auslesen
- int8_t val;
- // atomarer Variablenzugriff
- cli();
- val = interruptCounter;
- interruptCounter = 0;
- sei();
- return val;
- }
Advertisement
Add Comment
Please, Sign In to add comment