LoginSignup
0
0

More than 1 year has passed since last update.

ATtiny1616搭載オリジナルボードでエンコーダーボードを動かす

Last updated at Posted at 2021-07-23

オリジナルボードでATtiny1616でエンコーダーモーターを動かすの続きです。
https://qiita.com/usashirou/items/d7790e04f842ef43b5ee
エンコーダーモーターを普通のモータードライバーで動かす際のエクステンションボードを製作しました。

ボード全容

DSC_0484 (2).JPG

回路図

image.png

テストプログラム

全GPIOの動作確認を行うプログラムです

#define PA1 14
#define PA2 15
#define PA3 16
#define PA4 0
#define PA5 1
#define PA6 2
#define PA7 3
#define PB4 5
#define PB5 4
#define PC0 10
#define PC1 11
#define PC2 12
#define PC3 13

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(PA1, INPUT_PULLUP);
  pinMode(PA2, INPUT_PULLUP);
  pinMode(PA3, INPUT_PULLUP);
  pinMode(PA4, INPUT_PULLUP);
  pinMode(PA5, INPUT_PULLUP);
  pinMode(PA6, INPUT_PULLUP);
  pinMode(PA7, INPUT_PULLUP);
  pinMode(PB4, INPUT_PULLUP);
  pinMode(PB5, INPUT_PULLUP);
  pinMode(PC0, INPUT_PULLUP);
  pinMode(PC1, INPUT_PULLUP);
  pinMode(PC2, INPUT_PULLUP);
  pinMode(PC3, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(PA1) == LOW)
  {
    Serial.println("PA1");
  }
  if (digitalRead(PA2) == LOW)
  {
    Serial.println("PA2");
  }
  if (digitalRead(PA3) == LOW)
  {
    Serial.println("PA3");
  }
  if (digitalRead(PA4) == LOW)
  {
    Serial.println("PA4");
  }
  if (digitalRead(PA5) == LOW)
  {
    Serial.println("PA6");
  }
  if (digitalRead(PA6) == LOW)
  {
    Serial.println("PA6");
  }
  if (digitalRead(PA7) == LOW)
  {
    Serial.println("PA7");
  }
  if (digitalRead(PB4) == LOW)
  {
    Serial.println("PB4");
  }
  if (digitalRead(PB5) == LOW)
  {
    Serial.println("PB5");
  }
  if (digitalRead(PA6) == LOW)
  {
    Serial.println("PA6");
  }
  if (digitalRead(PC0) == LOW)
  {
    Serial.println("PC0");
  }
  if (digitalRead(PC1) == LOW)
  {
    Serial.println("PC1");
  }
  if (digitalRead(PC2) == LOW)
  {
    Serial.println("PC2");
  }
  if (digitalRead(PC3) == LOW)
  {
    Serial.println("PC3");
  }
}

本プログラム

各エンコーダーを読み込むだけのプログラムとなっています。

#define ENCA 10 // EncoderA
#define ENCB 11 // EncoderA
#define ENCC 12 // EncoderB
#define ENCD 13 // EncoderB
#define ENCE 14 // EncoderC
#define ENCF 15 // EncoderC
#define ENCG 3 // EncoderD
#define ENCH 2 // EncoderD

int posA = 0;
int posB = 0;
int posC = 0;
int posD = 0;

void setup() {
  Serial.begin(9600);

  pinMode(ENCA, INPUT);
  pinMode(ENCB, INPUT);
  pinMode(ENCC, INPUT);
  pinMode(ENCD, INPUT);
  pinMode(ENCE, INPUT);
  pinMode(ENCF, INPUT);
  pinMode(ENCG, INPUT);
  pinMode(ENCH, INPUT);

  attachInterrupt(digitalPinToInterrupt(ENCA), readEncoderA, RISING);
  attachInterrupt(digitalPinToInterrupt(ENCC), readEncoderB, RISING);
  attachInterrupt(digitalPinToInterrupt(ENCE), readEncoderC, RISING);
  attachInterrupt(digitalPinToInterrupt(ENCG), readEncoderD, RISING);
}

void loop() {
  Serial.print("EncoderA = ");
  Serial.println(posA);
  Serial.print("\r\n");
  Serial.print("EncoderB = ");
  Serial.println(posB);
  Serial.print("\r\n");
  Serial.print("EncoderC = ");
  Serial.println(posC);
  Serial.print("\r\n");
  Serial.print("EncoderD = ");
  Serial.println(posD);
  Serial.print("\r\n");
}

void readEncoderA() {
  int a = digitalRead(ENCB);
  if (a > 0) {
    posA++;
  }
  else {
    posA--;
  }
}

void readEncoderB() {
  int b = digitalRead(ENCC);
  if (b > 0) {
    posB++;
  }
  else {
    posB--;
  }
}

void readEncoderC() {
  int c = digitalRead(ENCE);
  if (c > 0) {
    posC++;
  }
  else {
    posC--;
  }
}

void readEncoderD() {
  int d = digitalRead(ENCG);
  if (d > 0) {
    posD++;
  }
  else {
    posD--;
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0