Адаптация этого ардуино к ESP32

Я пытаюсь создать проект, похожий на найденный здесь:

Простые меню Arduino для поворотных энкодеров

Однако я использую ESP32, а не плату Arduino.

Чтобы добраться до этого, мне нужно, чтобы мой Rotary Encoder работал с его кодом: Улучшено считывание поворотного энкодера Arduino

Однако я не могу скомпилировать код и получаю ошибку «PIND». Эта строка:

reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values.

Итак, мой вопрос: у вас есть идея, как я могу адаптировать код кодировщика для работы с ESP32?

Заранее большое спасибо. :)

Его полный код:

/*******Interrupt-based Rotary Encoder Sketch*******
by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/

static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  Serial.begin(115200); // start the serial monitor link
}

void PinA(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void PinB(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void loop(){
  if(oldEncPos != encoderPos) {
    Serial.println(encoderPos);
    oldEncPos = encoderPos;
  }
}


person davidqvist    schedule 16.12.2018    source источник


Ответы (2)


Библиотека (https://github.com/igorantolic/ai-esp32-rotary-encoder), который вы предложили, не очень надежен. вы получаете много ложных показаний. Когда мне нужно использовать поворотный энкодер, я придерживаюсь приведенного выше примера.

Я адаптировал его к ESP32:

К счастью, для GPIO34 и GPIO35 он почти одинаков.
GPIO34 - двоичный 100
GPIO35 - двоичный 1000
как 1100, так и 0xC

https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf

Страница 58:
GPIO_IN_REG -> GPIO 0 ... 31
GPIO_IN1_REG -> GPIO 32 ... 39

Обратите внимание, что для GPIO 34, 35, 36 и 39 требуется подтягивающий резистор. В противном случае вы можете использовать INPUT_PULLUP

#include <Arduino.h>

static int pinA = 35;
static int pinB = 34;
volatile byte aFlag = 0;
volatile byte bFlag = 0;
volatile byte encoderPos = 0;
volatile byte oldEncPos = 0;
volatile byte reading = 0;

void IRAM_ATTR PinA()
{
  cli();
  reading = GPIO_REG_READ(GPIO_IN1_REG) & 0xC;
  if (reading == B1100 && aFlag)
  {
    encoderPos--;
    bFlag = 0;
    aFlag = 0;
  }
  else if (reading == B1000)
    bFlag = 1;
  sei();
}

void IRAM_ATTR PinB()
{
  cli();
  reading = GPIO_REG_READ(GPIO_IN1_REG) & 0xC;
  if (reading == B1100 && bFlag)
  {
    encoderPos++;
    bFlag = 0;
    aFlag = 0;
  }
  else if (reading == B100)
    aFlag = 1;
  sei();
}

void setup()
{
  Serial.begin(115200);

  pinMode(pinA, INPUT);
  pinMode(pinB, INPUT);
  attachInterrupt(digitalPinToInterrupt(pinA), PinA, RISING);
  attachInterrupt(digitalPinToInterrupt(pinB), PinB, RISING);
}

void loop()
{
  if (oldEncPos != encoderPos)
  {
    Serial.print("encoderPos: ");
    Serial.println(encoderPos);
    oldEncPos = encoderPos;
  }
}
person SeventyNine    schedule 22.05.2020
comment
Спасибо за преобразование этого кода. Я могу заставить его работать на контактах 34 и 35. Однако я не уверен, как интерпретировать двоичные значения, необходимые для разных контактов, например контакты 25 и 26? - person FlipflopPancake; 10.10.2020

PIND - это один из регистров только для совместимых плат Arduino, который можно использовать для так называемого прямого управления портами.

В частности, PIND - это входной регистр порта D (контакты с 0 по 7 на UNO).

Например, чтение этого регистра даст вам состояние ввода каждого gpio от PIN0 до PIN7. В поворотном энкодере это используется для считывания всех значений PORTD за один раз, а затем маскирования других контактов, кроме «pinA» и «pinB», которые являются контактами 2 и 3 соответственно.

Это не будет работать на ESP32, так как эта платформа не имеет такого регистра (помните, что вы делаете здесь прямой доступ к оборудованию, а не используете стандартный API Arduino). Вы можете посмотреть GPIO_IN_REG в ESP32, который вы можете использовать для чтения вывода GPIO. заявляет аналогичным образом. GPIO_IN_REG вернет входные значения GPIO от 0 до 31.

Вы также можете попробовать и использовать эту библиотеку: https://github.com/igorantolic/ai-esp32-rotary-encoder, если вам нужно что-то уже сделано, вместо того, чтобы изобретать колесо, если это не для ваших учебных целей.

person coolbreeze    schedule 16.12.2018