1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00

use ESP-IDF ADC functions instead of analogRead() which breaks waveform

This commit is contained in:
Harald Barth 2021-10-27 23:03:37 +02:00
parent 7d7b337f82
commit 31059a615c
2 changed files with 22 additions and 5 deletions

View File

@ -20,6 +20,10 @@
#include "MotorDriver.h"
#include "DCCTimer.h"
#include "DIAG.h"
#if defined(ARDUINO_ARCH_ESP32)
#include <driver/adc.h>
#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

View File

@ -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