|
|
|
@ -0,0 +1,82 @@ |
|
|
|
#include <cstdint> |
|
|
|
#include <binary.h> |
|
|
|
#include <esp32-hal-gpio.h> |
|
|
|
#include <Arduino.h> |
|
|
|
#include "encoder1.h" |
|
|
|
|
|
|
|
#define PinA 27 // контат AB
|
|
|
|
#define PinC 14 // контат BC
|
|
|
|
//#define PinS 5 // кнопка
|
|
|
|
|
|
|
|
#define encoder1 |
|
|
|
|
|
|
|
uint8_t pinA = 0; // состояние контата AB
|
|
|
|
uint8_t pinC = 0; // состояние контата BC
|
|
|
|
|
|
|
|
uint8_t blockA = false; // блокировка прерывания на контакте AB
|
|
|
|
uint8_t blockC = false; // блокировка прерывания на контакте BC
|
|
|
|
|
|
|
|
uint8_t encoderResult = 0; // Результирующая переменная
|
|
|
|
int16_t encoderCount = 0; // Счетчик щелчков энкодера
|
|
|
|
int16_t oldencoderCount = 0; // Счетчик старое значение
|
|
|
|
|
|
|
|
int16_t getEncoderCount() { |
|
|
|
return encoderCount; |
|
|
|
} |
|
|
|
|
|
|
|
int16_t getOldencoderCount() { |
|
|
|
return oldencoderCount; |
|
|
|
} |
|
|
|
|
|
|
|
void setOldencoderCount(int16_t val) { |
|
|
|
oldencoderCount = val; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void encoderFilter () { |
|
|
|
encoderResult = encoderResult & B00001111; |
|
|
|
if (encoderResult == B1011) encoderCount ++; // по часовой стрелке
|
|
|
|
if (encoderResult == B0111) encoderCount --; // против часосовой стрелки
|
|
|
|
} |
|
|
|
|
|
|
|
void pinACHANGE () { // Изменилось состояние контакта АB
|
|
|
|
if (blockA == true) return; |
|
|
|
pinA = !digitalRead(PinA); |
|
|
|
pinC = !digitalRead(PinC); |
|
|
|
encoderResult <<= 1; |
|
|
|
bitWrite(encoderResult, 0, pinA); |
|
|
|
encoderResult <<= 1; |
|
|
|
bitWrite(encoderResult, 0, pinC); |
|
|
|
encoderFilter(); |
|
|
|
if (!pinA && !pinC) blockA = false; else blockA = true; |
|
|
|
blockC = false; |
|
|
|
} |
|
|
|
|
|
|
|
void pinCCHANGE () { // Изменилось состояние контакта BC
|
|
|
|
if (blockC == true) return; |
|
|
|
pinA = !digitalRead(PinA); |
|
|
|
pinC = !digitalRead(PinC); |
|
|
|
encoderResult <<= 1; |
|
|
|
bitWrite(encoderResult, 0, pinA); |
|
|
|
encoderResult <<= 1; |
|
|
|
bitWrite(encoderResult, 0, pinC); |
|
|
|
encoderFilter(); |
|
|
|
if (!pinA && !pinC) blockC = false; else blockC = true; |
|
|
|
blockA = false; |
|
|
|
} |
|
|
|
|
|
|
|
void setupEncoder1() { |
|
|
|
pinMode (PinA, INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
|
|
|
pinMode (PinC, INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
|
|
|
attachInterrupt(digitalPinToInterrupt(PinA), pinACHANGE, CHANGE); |
|
|
|
attachInterrupt(digitalPinToInterrupt(PinC), pinCCHANGE, CHANGE); |
|
|
|
} |
|
|
|
|
|
|
|
/*void loop() {
|
|
|
|
|
|
|
|
if (oldencoderCount != encoderCount) { |
|
|
|
Serial.println (encoderCount); |
|
|
|
oldencoderCount = encoderCount; |
|
|
|
} |
|
|
|
|
|
|
|
}*/ |