我正在尝试创建一个与此处找到的项目相似的项目:
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;
}
}
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
PIND仅是兼容Arduino板的寄存器之一,可用于所谓的直接端口操作。
具体地说,PIND是端口D的输入寄存器(UNO上的引脚0至7)
例如,读取该寄存器将为您提供从PIN0到PIN7的每个gpio的输入状态。在旋转编码器中,它用于一次性读取PORTD的所有值,然后屏蔽其他引脚,但分别为引脚2和引脚3的“ pinA”和“ pinB”除外。
这在ESP32上不起作用,因为该平台没有此类寄存器(请记住,您正在此处进行直接硬件访问,并且未通过标准Arduino API) 您可以查看ESP32中的GPIO_IN_REG,以类似的方式读取GPIO引脚状态。 GPIO_IN_REG将返回GPIO 0-31的输入值。
您也可以尝试使用此库: https://github.com/igorantolic/ai-esp32-rotary-encoder 如果您需要已经完成的工作,则无需重新发明轮子,除非这是出于学习目的。