Skip to content

Instantly share code, notes, and snippets.

@weldtype
Created June 4, 2016 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weldtype/5ade0b3a82b2286a62c5601708f0ddcc to your computer and use it in GitHub Desktop.
Save weldtype/5ade0b3a82b2286a62c5601708f0ddcc to your computer and use it in GitHub Desktop.
quadrature_encoder_noEXOR
//ロータリーエンコーダを試す(2)
// 2016/06/04 edy
// 2016/06/05 EX-ORを使わない
//
//How to use a quadrature encoder
//http://www.robotshop.com/media/files/PDF/tutorial-how-to-use-a-quadrature-encoder-rs011a.pdf
#include <LiquidCrystal.h>
// 接続ピンをして指定してライブラリを初期化
//LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(7, 6, 8, 9, 10, 11);
int new_SW, old_SW;
volatile int count;
volatile int intcount; //割り込み回数
//const int QEM[] = {0, -1, 1, 2, 1, 0, 2, -1, -1, 2, 0, 1, 2, 1, -1, 0};
const int QEM[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
void setup()
{
lcd.begin(16, 2);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
attachInterrupt(0, QE_IRQ, CHANGE);
attachInterrupt(1, QE_IRQ, CHANGE);
new_SW = digitalRead (2) * 2 + digitalRead (3);
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print(count, DEC);
lcd.print(" ");
lcd.setCursor(8, 0);
lcd.print(count / 4, DEC);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(intcount, DEC);
lcd.print(" ");
delay(100);
}
void QE_IRQ(void)
{
old_SW = new_SW;
new_SW = digitalRead (2) * 2 + digitalRead (3);
count += QEM[old_SW * 4 + new_SW];
intcount++; //割り込み回数カウント、通常は不要
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment