利用A下降沿计算圈数
This commit is contained in:
53
Homework/Homework5/Encoder/Encoder.ino
Normal file
53
Homework/Homework5/Encoder/Encoder.ino
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#include <TimerOne.h>
|
||||||
|
|
||||||
|
const byte IN1 = 6;
|
||||||
|
const byte IN2 = 7;
|
||||||
|
const byte ENABLE = 5;
|
||||||
|
const byte ENCO_B = 4;
|
||||||
|
const byte ENCO_A = 2;
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
const unsigned long RESET_COUNT_PERIOD_IN_US = 50000;
|
||||||
|
const int EDGES_PER_CYCLE = 13;
|
||||||
|
const int REDUCTION_RATIO = 20;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(IN1, OUTPUT);
|
||||||
|
pinMode(IN2, OUTPUT);
|
||||||
|
pinMode(ENABLE, OUTPUT);
|
||||||
|
pinMode(A0, INPUT);
|
||||||
|
pinMode(A1, INPUT);
|
||||||
|
digitalWrite(IN1, HIGH);
|
||||||
|
digitalWrite(IN2, LOW);
|
||||||
|
pinMode(ENCO_B, INPUT);
|
||||||
|
pinMode(ENCO_A, INPUT);
|
||||||
|
Serial.begin(115200);
|
||||||
|
Timer1.initialize();
|
||||||
|
Timer1.setPeriod(RESET_COUNT_PERIOD_IN_US);
|
||||||
|
Timer1.attachInterrupt(resetCountAndPrint);
|
||||||
|
attachInterrupt(digitalPinToInterrupt(ENCO_A), onEncoAFalling, FALLING);
|
||||||
|
analogWrite(ENABLE, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Serial.print(digitalRead(2));
|
||||||
|
// Serial.print(" ");
|
||||||
|
// Serial.println(digitalRead(4));
|
||||||
|
// delay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resetCountAndPrint() {
|
||||||
|
Serial.print("Rpm: ");
|
||||||
|
Serial.println((double)count * 60 * 1000 * 1000 / EDGES_PER_CYCLE / REDUCTION_RATIO / RESET_COUNT_PERIOD_IN_US);
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onEncoAFalling() {
|
||||||
|
// Serial.println("In!");
|
||||||
|
if (digitalRead(ENCO_B) == LOW) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user