概述

8421 Encoder-Horizontal是一款具有16档编码(0-F)卧式直插的8421编码器,常应用于各种设备的状态控制。

特性

应用场景

技术规格

8421 Encoder-Horizontal 尺寸图
















接口说明

8421 Encoder-Vertical 顶部&底部
标号 名称 功能描述
1 8 8421BCD编码8端口
2 4 8421BCD编码4端口
3 2 8421BCD编码2端口
4 1 8421BCD编码1端口
5 C 公共端-接入电源正极VCC
6 X 公共端-接入电源负极GND

真值表

8421 Encoder-Vertical 真值表

Arduino使用教程

准备

连线图

8421 Encoder-Horizontal 引用连线图

样例代码

/**************************************************************************/
/*
    @file       DFR0722_V10_Demo.ino
    @author     PowerLiao (DFRobot)
    @version    V1.0
    @date       2020-07-09
    @copyright  Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
    @licence    The MIT License (MIT)
    @breif      This example read and print the 8421 encoder code.

    This demo and related libraries are for [DFR0722]8421 Encoder-Horizontal(V1.0)
    Check out the links below for tutorials and connection diagrams
    Product(CH): https://www.dfrobot.com.cn/
    Product(EN): https://www.dfrobot.com/

*/
/**************************************************************************/



#define setbit(data,b) (data|=(1<<b)) //set the b bit of data to 1
#define clrbit(data,b) (data&=~(1<<b)) //set the b bit of data to 0

uint8_t code8421 = 0;
const uint8_t code8Pin = 5;
const uint8_t code4Pin = 4;
const uint8_t code2Pin = 3;
const uint8_t code1Pin = 2;

void setup() {
  Serial.begin(115200);
  Serial.println("8421 Encoder Starts up!");
  pinMode(code8Pin, INPUT);
  pinMode(code4Pin, INPUT);
  pinMode(code2Pin, INPUT);
  pinMode(code1Pin, INPUT);
}

void loop() {
  //According to the pin level and set the corresponding bit to 0 or 1
  if (digitalRead(code8Pin) == HIGH){
    setbit(code8421, 3);
  }else{
    clrbit(code8421, 3);
  }

  if (digitalRead(code4Pin) == HIGH){
    setbit(code8421, 2);
  }else{
    clrbit(code8421, 2);
  }

  if (digitalRead(code2Pin) == HIGH){
    setbit(code8421, 1);
  }else{
    clrbit(code8421, 1);
  }

  if (digitalRead(code1Pin) == HIGH){
    setbit(code8421, 0);
  }else{
    clrbit(code8421, 0);
  }

  //Output the code in hexadecimal 
  Serial.print("Now code8421 is:  ");
  Serial.println(code8421, HEX);
  delay(1000);
}

结果

常见问题

更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖。

更多