7 changed files with 254 additions and 113 deletions
@ -0,0 +1,16 @@ |
|||||
|
{ |
||||
|
"configurations": [ |
||||
|
{ |
||||
|
"name": "Mac", |
||||
|
"includePath": [ |
||||
|
"/Users/gsd/Library/Arduino15/packages/**" |
||||
|
], |
||||
|
"defines": [], |
||||
|
"compilerPath": "/usr/bin/clang", |
||||
|
"cStandard": "c17", |
||||
|
"cppStandard": "c++17", |
||||
|
"intelliSenseMode": "macos-clang-arm64" |
||||
|
} |
||||
|
], |
||||
|
"version": 4 |
||||
|
} |
||||
@ -1,5 +1,16 @@ |
|||||
{ |
{ |
||||
"files.associations": { |
"files.associations": { |
||||
"cstdint": "cpp" |
"cstdint": "cpp", |
||||
|
"__bit_reference": "cpp", |
||||
|
"__hash_table": "cpp", |
||||
|
"__split_buffer": "cpp", |
||||
|
"array": "cpp", |
||||
|
"initializer_list": "cpp", |
||||
|
"string": "cpp", |
||||
|
"string_view": "cpp", |
||||
|
"unordered_map": "cpp", |
||||
|
"vector": "cpp", |
||||
|
"ostream": "cpp", |
||||
|
"map": "cpp" |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,146 @@ |
|||||
|
#include <cstdint> |
||||
|
#include <binary.h> |
||||
|
#include <esp32-hal-gpio.h> |
||||
|
#include <Arduino.h> |
||||
|
#include "enc.h" |
||||
|
|
||||
|
const int PinA[ENCODER_COUNT]={14,12,26,33}; // контат AB
|
||||
|
const int PinC[ENCODER_COUNT]={27,13,25,32}; // контат BC
|
||||
|
|
||||
|
uint8_t pinA[ENCODER_COUNT] = {0,0,0,0}; // состояние контата AB
|
||||
|
uint8_t pinC[ENCODER_COUNT] = {0,0,0,0}; // состояние контата BC
|
||||
|
|
||||
|
uint8_t blockA[ENCODER_COUNT] = {false,false,false,false}; // блокировка прерывания на контакте AB
|
||||
|
uint8_t blockC[ENCODER_COUNT] = {false,false,false,false}; // блокировка прерывания на контакте BC
|
||||
|
|
||||
|
uint8_t encoderResult[ENCODER_COUNT] = {0,0,0,0}; // Результирующая переменная
|
||||
|
int16_t encoderCount[ENCODER_COUNT] = {0,0,0,0}; // Счетчик щелчков энкодера
|
||||
|
int16_t oldencoderCount[ENCODER_COUNT] = {0,0,0,0}; // Счетчик старое значение
|
||||
|
|
||||
|
int16_t getEncoderCount(int enc) { |
||||
|
return encoderCount[enc]; |
||||
|
} |
||||
|
|
||||
|
int16_t getOldencoderCount(int enc) { |
||||
|
return oldencoderCount[enc]; |
||||
|
} |
||||
|
|
||||
|
void setOldencoderCount(int enc, int16_t val) { |
||||
|
oldencoderCount[enc] = val; |
||||
|
} |
||||
|
|
||||
|
void encoderFilter (int enc) { |
||||
|
encoderResult[enc] = encoderResult[enc] & B00001111; |
||||
|
if (encoderResult[enc] == B1011) encoderCount[enc] ++; // по часовой стрелке
|
||||
|
if (encoderResult[enc] == B0111) encoderCount[enc] --; // против часосовой стрелки
|
||||
|
} |
||||
|
|
||||
|
void pinACHANGE (int enc) { // Изменилось состояние контакта АB
|
||||
|
if (blockA[enc] == true) return; |
||||
|
pinA[enc] = !digitalRead(PinA[enc]); |
||||
|
pinC[enc] = !digitalRead(PinC[enc]); |
||||
|
encoderResult[enc] <<= 1; |
||||
|
bitWrite(encoderResult[enc], 0, pinA[enc]); |
||||
|
encoderResult[enc] <<= 1; |
||||
|
bitWrite(encoderResult[enc], 0, pinC[enc]); |
||||
|
encoderFilter(enc); |
||||
|
if (!pinA[enc] && !pinC[enc]) blockA[enc] = false; else blockA[enc] = true; |
||||
|
blockC[enc] = false; |
||||
|
} |
||||
|
|
||||
|
void pinCCHANGE (int enc) { // Изменилось состояние контакта BC
|
||||
|
if (blockC[enc] == true) return; |
||||
|
pinA[enc] = !digitalRead(PinA[enc]); |
||||
|
pinC[enc] = !digitalRead(PinC[enc]); |
||||
|
encoderResult[enc] <<= 1; |
||||
|
bitWrite(encoderResult[enc], 0, pinA[enc]); |
||||
|
encoderResult[enc] <<= 1; |
||||
|
bitWrite(encoderResult[enc], 0, pinC[enc]); |
||||
|
encoderFilter(enc); |
||||
|
if (!pinA[enc] && !pinC[enc]) blockC[enc] = false; else blockC[enc] = true; |
||||
|
blockA[enc] = false; |
||||
|
} |
||||
|
|
||||
|
#ifdef ENC1 |
||||
|
void enc_1_A() { |
||||
|
pinACHANGE(ENC1); |
||||
|
} |
||||
|
|
||||
|
void enc_1_C() { |
||||
|
pinCCHANGE(ENC1); |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
#ifdef ENC2 |
||||
|
void enc_2_A() { |
||||
|
pinACHANGE(ENC2); |
||||
|
} |
||||
|
|
||||
|
void enc_2_C() { |
||||
|
pinCCHANGE(ENC2); |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
#ifdef ENC3 |
||||
|
void enc_3_A() { |
||||
|
pinACHANGE(ENC3); |
||||
|
} |
||||
|
|
||||
|
void enc_3_C() { |
||||
|
pinCCHANGE(ENC3); |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
#ifdef ENC4 |
||||
|
void enc_4_A() { |
||||
|
pinACHANGE(ENC4); |
||||
|
} |
||||
|
|
||||
|
void enc_4_C() { |
||||
|
pinCCHANGE(ENC4); |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
void setupEncoder() { |
||||
|
//привязываем прерывания
|
||||
|
//первый енкодер
|
||||
|
#ifdef ENC1 |
||||
|
pinMode (PinA[ENC1], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
pinMode (PinC[ENC1], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
attachInterrupt(digitalPinToInterrupt(PinA[ENC1]), enc_1_A, CHANGE); |
||||
|
attachInterrupt(digitalPinToInterrupt(PinC[ENC1]), enc_1_C, CHANGE); |
||||
|
#endif |
||||
|
|
||||
|
//второй энкодер
|
||||
|
#ifdef ENC2 |
||||
|
pinMode (PinA[ENC2], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
pinMode (PinC[ENC2], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
attachInterrupt(digitalPinToInterrupt(PinA[ENC2]), enc_2_A, CHANGE); |
||||
|
attachInterrupt(digitalPinToInterrupt(PinC[ENC2]), enc_2_C, CHANGE); |
||||
|
#endif |
||||
|
|
||||
|
//третий энкодер
|
||||
|
#ifdef ENC3 |
||||
|
pinMode (PinA[ENC3], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
pinMode (PinC[ENC3], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
attachInterrupt(digitalPinToInterrupt(PinA[ENC3]), enc_3_A, CHANGE); |
||||
|
attachInterrupt(digitalPinToInterrupt(PinC[ENC3]), enc_3_C, CHANGE); |
||||
|
#endif |
||||
|
|
||||
|
//четвертый энкодер
|
||||
|
#ifdef ENC4 |
||||
|
pinMode (PinA[ENC4], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
pinMode (PinC[ENC4], INPUT_PULLUP); // Как вход и внутренняя подтяжка
|
||||
|
attachInterrupt(digitalPinToInterrupt(PinA[ENC4]), enc_4_A, CHANGE); |
||||
|
attachInterrupt(digitalPinToInterrupt(PinC[ENC4]), enc_4_C, CHANGE); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
/*void loop() {
|
||||
|
|
||||
|
if (oldencoderCount != encoderCount) { |
||||
|
Serial.println (encoderCount); |
||||
|
oldencoderCount = encoderCount; |
||||
|
} |
||||
|
|
||||
|
}*/ |
||||
@ -0,0 +1,11 @@ |
|||||
|
#define ENCODER_COUNT 4 |
||||
|
|
||||
|
#define ENC1 (0) |
||||
|
#define ENC2 (1) |
||||
|
#define ENC3 (2) |
||||
|
#define ENC4 (3) |
||||
|
|
||||
|
void setupEncoder(); |
||||
|
int16_t getEncoderCount(int enc); |
||||
|
int16_t getOldencoderCount(int enc); |
||||
|
void setOldencoderCount(int enc, int16_t val); |
||||
@ -1,82 +0,0 @@ |
|||||
#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; |
|
||||
} |
|
||||
|
|
||||
}*/ |
|
||||
@ -1,4 +0,0 @@ |
|||||
void setupEncoder1(); |
|
||||
int16_t getEncoderCount(); |
|
||||
int16_t getOldencoderCount(); |
|
||||
void setOldencoderCount(int16_t val); |
|
||||
@ -1,46 +1,89 @@ |
|||||
#include "encoder1.h" |
#include "enc.h" |
||||
|
#include <arduino.h> |
||||
|
#include <esp32-hal-adc.h> |
||||
|
#include <HardwareSerial.h> |
||||
|
|
||||
const int input[] = {36, 39, 34 ,35 ,32 ,33 ,25 ,26}; |
const int input[] = {}; |
||||
long inputValue[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
long inputCurrentValue[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
||||
|
long inputPreviewValue[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
||||
const int numPins = sizeof(input) / sizeof(input[0]); |
const int numPins = sizeof(input) / sizeof(input[0]); |
||||
|
|
||||
|
const int buttonInput[2] = {36,39}; |
||||
|
int buttonLastState[2] = {HIGH, HIGH}; |
||||
|
int buttonCurrentState[2]; |
||||
|
|
||||
#define RXD2 (16) |
#define RXD2 (16) |
||||
#define TXD2 (17) |
#define TXD2 (17) |
||||
|
|
||||
|
//#define DEBUGIO
|
||||
|
|
||||
void setup() { |
void setup() { |
||||
Serial.begin(115200); |
#ifdef DEBUGIO |
||||
//#ifdef encoder1
|
Serial.begin(115200); |
||||
setupEncoder1(); |
#else |
||||
//#endif
|
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); |
||||
|
#endif |
||||
|
|
||||
|
setupEncoder(); |
||||
|
|
||||
|
pinMode(buttonInput[0], INPUT); |
||||
|
|
||||
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); |
|
||||
analogReadResolution(12); |
analogReadResolution(12); |
||||
analogSetAttenuation(ADC_11db); |
analogSetAttenuation(ADC_11db); |
||||
|
|
||||
} |
} |
||||
|
|
||||
void values2uart() { |
void loop() { |
||||
for (int i = 0; i < numPins; i++) { |
for (int i = 0; i < 2; i++) { |
||||
Serial2.print(input[i]); |
buttonCurrentState[i] = digitalRead(buttonInput[i]); |
||||
Serial2.print("-"); |
if (buttonLastState[i] == LOW && buttonCurrentState[i]){ |
||||
Serial2.print(inputValue[i]); |
#ifdef DEBUGIO |
||||
Serial2.print("\n"); |
Serial.print(1001+i); |
||||
|
Serial.print("*"); |
||||
|
Serial.print(buttonCurrentState[i]); |
||||
|
Serial.print("\n"); |
||||
|
#else |
||||
|
Serial2.print(1001+i); |
||||
|
Serial2.print("*"); |
||||
|
Serial2.print(buttonCurrentState[i]); |
||||
|
Serial2.print("\n"); |
||||
|
#endif |
||||
|
} |
||||
|
buttonLastState[i] = buttonCurrentState[i]; |
||||
} |
} |
||||
} |
|
||||
|
|
||||
void loop() { |
|
||||
for (int i = 0; i < numPins; i++) { |
for (int i = 0; i < numPins; i++) { |
||||
inputValue[i] = map(analogRead(input[i]), 0, 4096, 0, 25); |
inputCurrentValue[i] = map(analogRead(input[i]), 0, 4096, 0, 16); |
||||
|
if (inputCurrentValue[i] != inputPreviewValue[i]) { |
||||
|
inputPreviewValue[i] = inputCurrentValue[i]; |
||||
|
#ifdef DEBUGIO |
||||
|
Serial.print(input[i]); |
||||
|
Serial.print("*"); |
||||
|
Serial.print(inputCurrentValue[i]); |
||||
|
Serial.print("\n"); |
||||
|
#else |
||||
|
Serial2.print(input[i]); |
||||
|
Serial2.print("*"); |
||||
|
Serial2.print(inputCurrentValue[i]); |
||||
|
Serial2.print("\n"); |
||||
|
#endif |
||||
|
} |
||||
} |
} |
||||
|
|
||||
//#ifdef encoder1
|
for (int enc = 0; enc < ENCODER_COUNT; enc++) { |
||||
// Serial.print(0);
|
if (getOldencoderCount(enc) != getEncoderCount(enc)) { |
||||
if (getOldencoderCount() != getEncoderCount()) { |
#ifdef DEBUGIO |
||||
Serial.println (getEncoderCount()); |
Serial.print(101+enc); |
||||
setOldencoderCount(getEncoderCount()); |
Serial.print("*"); |
||||
|
Serial.println (getEncoderCount(enc)); |
||||
|
#else |
||||
|
Serial2.print(101+enc); |
||||
|
Serial2.print("*"); |
||||
|
Serial2.println (getEncoderCount(enc)); |
||||
|
#endif |
||||
|
setOldencoderCount(enc, getEncoderCount(enc)); |
||||
|
} |
||||
} |
} |
||||
//#endif
|
|
||||
|
|
||||
values2uart(); |
|
||||
delay(50); |
delay(50); |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue