Created
June 4, 2016 11:14
-
-
Save weldtype/4822ee900d9ea47955e04055a42898a2 to your computer and use it in GitHub Desktop.
Arduino_quadrature_encoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ロータリーエンコーダを試す | |
// 2016/06/04 edy | |
// | |
//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 = 0; | |
volatile int intcount = 0; //割り込み回数 | |
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(4, INPUT); | |
pinMode(5, INPUT); | |
attachInterrupt(1, QE_IRQ, CHANGE); | |
new_SW = digitalRead (4) * 2 + digitalRead (5); | |
} | |
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) | |
{ | |
int out ; | |
old_SW = new_SW; | |
new_SW = digitalRead (4) * 2 + digitalRead (5); | |
out = QEM[old_SW * 4 + new_SW];; | |
count += out; | |
intcount++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment