|
近日使用了一个常见的EC11五脚带开关旋转编码器,并从网上找到一个测试用例,经测试:能在Arduino Nano上正常工作,而在OCROBOT ALPHA 8F328P-U上只有开关正常工作,左右旋转旋钮无反应。代码如下,期待提供指导: - #include "Rotary.h"
- // Enconder PINs
- #define ENCODER_PIN_A 10
- #define ENCODER_PIN_B 11
- #define ENCODER_PUSH_BUTTON 12
- volatile int encoderCount = 0;
- // Devices class declarations
- Rotary encoder = Rotary(ENCODER_PIN_A, ENCODER_PIN_B);
- void setup()
- {
- // Encoder pins
- pinMode(ENCODER_PUSH_BUTTON, INPUT_PULLUP);
-
- // Encoder interrupt
- attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), rotaryEncoder, CHANGE);
- attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_B), rotaryEncoder, CHANGE);
- Serial.begin(9600);
- }
- /**
- * Reads encoder via interrupt
- * Use Rotary.h and Rotary.cpp implementation to process encoder via interrupt
- */
- void rotaryEncoder()
- { // rotary encoder events
- uint8_t encoderStatus = encoder.process();
- if (encoderStatus)
- encoderCount = (encoderStatus == DIR_CW) ? 1 : -1;
- }
- void loop()
- {
- // Check if the encoder has moved.
- if (encoderCount == 1)
- {
- Serial.print("->\n") ;
- encoderCount = 0;
- }
- if (encoderCount == -1)
- {
- Serial.print("<-\n") ;
- encoderCount = 0;
- }
- if (digitalRead(ENCODER_PUSH_BUTTON) ==LOW)
- {
- Serial.print("O\n") ;
- digitalWrite(ENCODER_PUSH_BUTTON,HIGH);
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|