极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 27412|回复: 1

OCROBOT ALPHA 8F328P-U 使用编码器不能正常工作

[复制链接]
发表于 2021-1-17 14:16:44 | 显示全部楼层 |阅读模式
近日使用了一个常见的EC11五脚带开关旋转编码器,并从网上找到一个测试用例,经测试:能在Arduino Nano上正常工作,而在OCROBOT ALPHA 8F328P-U上只有开关正常工作,左右旋转旋钮无反应。代码如下,期待提供指导:

  1. #include "Rotary.h"

  2. // Enconder PINs
  3. #define ENCODER_PIN_A 10
  4. #define ENCODER_PIN_B 11
  5. #define ENCODER_PUSH_BUTTON 12

  6. volatile int encoderCount = 0;

  7. // Devices class declarations
  8. Rotary encoder = Rotary(ENCODER_PIN_A, ENCODER_PIN_B);

  9. void setup()
  10. {
  11.   // Encoder pins
  12.   pinMode(ENCODER_PUSH_BUTTON, INPUT_PULLUP);
  13.   
  14.   // Encoder interrupt
  15.   attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A),  rotaryEncoder, CHANGE);
  16.   attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_B),  rotaryEncoder, CHANGE);

  17. Serial.begin(9600);
  18. }

  19. /**
  20. * Reads encoder via interrupt
  21. * Use Rotary.h and  Rotary.cpp implementation to process encoder via interrupt
  22. */
  23. void rotaryEncoder()
  24. { // rotary encoder events
  25.   uint8_t encoderStatus = encoder.process();
  26.   if (encoderStatus)
  27.     encoderCount = (encoderStatus == DIR_CW) ? 1 : -1;
  28. }


  29. void loop()
  30. {
  31. // Check if the encoder has moved.

  32. if (encoderCount == 1)
  33. {
  34.             Serial.print("->\n") ;
  35.             encoderCount = 0;
  36.   }         
  37. if (encoderCount == -1)
  38. {
  39.             Serial.print("<-\n") ;
  40.     encoderCount = 0;
  41. }
  42. if (digitalRead(ENCODER_PUSH_BUTTON) ==LOW)
  43. {
  44.             Serial.print("O\n") ;
  45.          digitalWrite(ENCODER_PUSH_BUTTON,HIGH);
  46.   }

  47.    }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2021-1-17 19:43:10 | 显示全部楼层
在这里找到了答案:
http://www.taichi-maker.com/homepage/reference-index/arduino-code-reference/attachinterrupt/
Arduino Uno, Nano, Mini支持中断的引脚2, 3,我自己修改到其他PIN了。

进一步作自己,把这个所谓“改进版”库示例照着改成中断方式,同时把LGT8F328P主频置为2MHZ,发现反应比较迟钝,要快速旋动编码器才有输出:
https://github.com/CarlosSiles67/Rotary
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 12:40 , Processed in 0.041415 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表