本文主要是介绍arduino 读取EC11编码器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用github上的库
实物图
引脚接线
EC11模块引脚 | arduino UNO引脚 |
+ | VCC |
GND | GND |
CLK | 2 (3) |
DT | 3 (2) |
github演示代码
文件存放
//
// Example for EC11 class (Rotary Encoder Helper).
// Copyright (C) 2016, Aleh Dzenisiuk.
// http://github.com/aleh/ec11
//#include "EC11.hpp"//
// Here we assume that the pins A and B of a EC-11 rotary encoder are connected to pins 2 and 3 of the Arduino Uno
// board and the pin C is connected to the ground.
//EC11 encoder;// Defined as 0 to see how polling-style pin checks behave.
#define DEMO_INTERRUPTS 0 // 0:查询方式读取引脚 1:中断方式读取#if DEMO_INTERRUPTS//
// Interrupt-based example. This is recommended, but it means only pins 2 and 3 can be used with Uno.
//const int encoderPinA = 2;
const int encoderPinB = 3;void pinDidChange() {encoder.checkPins(digitalRead(encoderPinA), digitalRead(encoderPinB));
}void prepare() {attachInterrupt(0, pinDidChange, CHANGE);attachInterrupt(1, pinDidChange, CHANGE);
}#else//
// Polling allows to use the encoder with any digital input pin.
//const int encoderPinA = 2;
const int encoderPinB = 3;void prepare() {
}#endif // #if DEMO_INTERRUPTSvoid setup() {Serial.begin(9600);Serial.println("EC11 Test");// We can use internal pull-up with the encoder pins, assuming pin C is simply grounded.pinMode(encoderPinA, INPUT_PULLUP);pinMode(encoderPinB, INPUT_PULLUP);prepare();
}static int value = 0;void loop() {EC11Event e;if (encoder.read(&e)) {// OK, got an event waiting to be handled.if (e.type == EC11Event::StepCW) {value += e.count;} else {value -= e.count;}Serial.println(value);}#if DEMO_INTERRUPTS// Wait quite some time to demonstrate that we can check for events fairly infrequently and still not miss them.delay(200);#else// With polling-style pin checking we can still read infrequently, but we need to poll the pins often enough.for (int i = 0; i < 200; i++) {encoder.checkPins(digitalRead(encoderPinA), digitalRead(encoderPinB));delay(1);}#endif
}
串口数据接收
这篇关于arduino 读取EC11编码器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!