【问题标题】:Adapting this arduino instructable to ESP32将此 arduino 教程改编为 ESP32
【发布时间】:2020-05-23 01:52:53
【问题描述】:

我正在尝试创建一个与此处找到的项目类似的项目:

Easy Arduino Menus for Rotary Encoders

但是,我使用的是 ESP32,而不是 Arduino 板。

要做到这一点,我需要让我的 Rotary Encoder 使用他的代码: Improved Arduino Rotary Encoder Reading

但是,我无法编译代码并在“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;
  }
}

【问题讨论】:

    标签: arduino interactive arduino-ide esp32


    【解决方案1】:

    您建议的库(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;
      }
    }
    

    【讨论】:

    • 感谢您转换此代码。我可以让它在引脚 34 和 35 上工作。但是,我不确定如何解释不同引脚所需的二进制值,例如引脚 25 和 26?
    【解决方案2】:

    PIND 是仅兼容 Arduino 板的寄存器之一,可用于所谓的直接端口操作。

    具体来说,PIND 是端口 D 的输入寄存器(UNO 上的引脚 0 到 7)

    例如,读取这个寄存器会给你从 PIN0 到 PIN7 的每个 gpio 的输入状态。在旋转编码器中,这用于一次性读取 PORTD 的所有值,然后屏蔽除“pinA”和“pinB”之外的其他引脚,它们分别是引脚 2 和引脚 3。

    这在 ESP32 上不起作用,因为该平台没有这样的寄存器(请记住,您在此处进行直接硬件访问,而不是通过标准 Arduino API) 您可以查看 ESP32 中的 GPIO_IN_REG,您可以使用它以类似的方式读取 GPIO 引脚状态。 GPIO_IN_REG 将返回 GPIO 0 - 31 的输入值。

    您也可以尝试使用这个库: https://github.com/igorantolic/ai-esp32-rotary-encoder 如果您需要已经制作好的东西,而不是重新发明轮子,除非是为了您的学习目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2022-12-18
      • 2019-03-18
      • 2021-12-01
      • 2019-09-14
      相关资源
      最近更新 更多