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_INTERRUPTS

void 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
}

 串口数据接收

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

armcsdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值