From 31059a615c20bfb1814685ea0e9b9f09383c8fe3 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Wed, 27 Oct 2021 23:03:37 +0200 Subject: [PATCH] use ESP-IDF ADC functions instead of analogRead() which breaks waveform --- MotorDriver.cpp | 12 ++++++++++++ config.example.h | 15 ++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 8bcb03c..8959f42 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -20,6 +20,10 @@ #include "MotorDriver.h" #include "DCCTimer.h" #include "DIAG.h" +#if defined(ARDUINO_ARCH_ESP32) +#include +#define pinToADC1Channel(X) (adc1_channel_t)(((X) > 35) ? (X)-36 : (X)-28) +#endif bool MotorDriver::usePWM=false; bool MotorDriver::commonFaultPin=false; @@ -55,7 +59,13 @@ MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8 currentPin=current_pin; if (currentPin!=UNUSED_PIN) { pinMode(currentPin, INPUT); +#if defined(ARDUINO_ARCH_ESP32) + adc1_config_width(ADC_WIDTH_BIT_12); + adc1_config_channel_atten(pinToADC1Channel(currentPin),ADC_ATTEN_DB_11); + senseOffset = adc1_get_raw(pinToADC1Channel(currentPin)); +#else senseOffset=analogRead(currentPin); // value of sensor at zero current +#endif } faultPin=fault_pin; @@ -150,6 +160,8 @@ int MotorDriver::getCurrentRaw() { current = analogRead(currentPin)-senseOffset; overflow_count = 0; SREG = sreg_backup; /* restore interrupt state */ +#elif defined(ARDUINO_ARCH_ESP32) + current = adc1_get_raw(pinToADC1Channel(currentPin))-senseOffset; #else current = analogRead(currentPin)-senseOffset; #endif diff --git a/config.example.h b/config.example.h index 7338051..2d345e2 100644 --- a/config.example.h +++ b/config.example.h @@ -43,19 +43,24 @@ The configuration file for DCC-EX Command Station // //#define MOTOR_SHIELD_TYPE STANDARD_MOTOR_SHIELD +// https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/ +// 4 high at boot #define ESP8266_MOTOR_SHIELD F("ESP8266"), \ new MotorDriver(D3, D5, UNUSED_PIN, UNUSED_PIN, UNUSED_PIN, 2.99, 2000, UNUSED_PIN),\ new MotorDriver(D2, D6, UNUSED_PIN, UNUSED_PIN, A0 , 2.99, 2000, UNUSED_PIN) -// ADC1 CH4 = GPIO32 -// ADC1 CH5 = GPIO33 +// ESP32 ADC1 only supported GPIO pins 32 to 39, for example +// ADC1 CH4 = GPIO32, ADC1 CH5 = GPIO33, ADC1 CH0 = GPIO36 +// // Adjust pin usage according to info in // https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/ // https://randomnerdtutorials.com/esp32-pinout-reference-gpios/ -// Adjust conversion factor according to your voltage divider +// +// Adjust conversion factor according to your voltage divider. +// #define ESP32_MOTOR_SHIELD F("ESP32"), \ - new MotorDriver(16, 17, UNUSED_PIN, UNUSED_PIN, 32, 2.99, 2000, UNUSED_PIN),\ - new MotorDriver(18, 19, UNUSED_PIN, UNUSED_PIN, 33, 2.99, 2000, UNUSED_PIN) + new MotorDriver(16, 17, UNUSED_PIN, UNUSED_PIN, 32, 2.00, 2000, UNUSED_PIN),\ + new MotorDriver(18, 19, UNUSED_PIN, UNUSED_PIN, 33, 2.00, 2000, UNUSED_PIN) #define MOTOR_SHIELD_TYPE ESP32_MOTOR_SHIELD