极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 24079|回复: 4

也谈Arduino与旋转编码器的第三种连接方式(中断方式和普通D口合拼)

[复制链接]
发表于 2014-3-14 21:35:31 | 显示全部楼层 |阅读模式
本帖最后由 pww999 于 2014-3-14 21:53 编辑

跟据 supercctv  也谈Arduino与旋转编码器的两种连接方式(中断方式,普通D口)
http://www.geek-workshop.com/thread-595-1-1.html

这种方式为 使用1个中断口和1个普通D口 实现 中断 计数 ,(中断A相,用D端口 读取B相 +或—,同时计数),测试通过,
在某种场合时或许能用上

#include "Wire.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); //set the LCD address to 0x27 for a 16 chars and 2 line display
   // -----------
#define PinA 2  //中断0

const int encoderPinB = 5;   //用D5 读取B相 +或—,同时计数

unsigned long time = 0;
long count = 0; //计数值
long num = 0;

//初始化

void setup()
{

    lcd.init();

    lcd.backlight();
  
    delay(20);
         lcd.setCursor(0, 0);
    lcd.print("pww999");
   
   // -----------
  pinMode(PinA,INPUT); //D2脚为输入


  attachInterrupt(0, blinkA, LOW);  //注册中断0调用函数blinkA
  
  pinMode(encoderPinB, INPUT);
  digitalWrite(encoderPinB, HIGH);
  
  time = millis(); //时间初值
}

void loop()
{
  while (num != count)
  {
    num = count;
   // Serial.println(num);
     lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("pww999");
    lcd.setCursor(0, 1);
    lcd.print(num);
  }
}

//中断0调用函数
void blinkA()
{
  if ((millis() - time) > 3) //防抖动处理
  {
    if (digitalRead(encoderPinB) == HIGH)  //读取D5
    {
        count ++;
    }
        else
    {
       count --;
    }
  }
  time = millis();
}
回复

使用道具 举报

发表于 2014-3-15 16:03:09 | 显示全部楼层
attachInterrupt(0, blinkA, LOW);  //注册中断0调用函数blinkA
中断模式设置成“FALLING”会不会好些,至少可以避免重复中断
回复 支持 反对

使用道具 举报

发表于 2014-3-17 16:40:15 | 显示全部楼层
有个疑问,电机转速较低时可在串口中显示,当转速提高时在串口中显示为0?
回复 支持 反对

使用道具 举报

发表于 2016-7-16 19:06:28 | 显示全部楼层
平衡小车AB相又是怎么接线?
回复 支持 反对

使用道具 举报

发表于 2017-3-18 04:37:29 来自手机 | 显示全部楼层
lcd怎么连的啊???
回复 支持 反对

使用道具 举报

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

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

Archiver|联系我们|极客工坊

GMT+8, 2024-5-5 00:17 , Processed in 0.062779 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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