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

DC via power pin 1st try

This commit is contained in:
Harald Barth 2023-02-12 23:31:13 +01:00
parent 2ada89f918
commit 9482041799
4 changed files with 22 additions and 15 deletions

View File

@ -1 +1 @@
#define GITHUB_SHA "devel-202302121935Z" #define GITHUB_SHA "devel-202302122239Z"

View File

@ -146,12 +146,16 @@ void MotorDriver::setPower(POWERMODE mode) {
if (on) { if (on) {
noInterrupts(); noInterrupts();
IODevice::write(powerPin,invertPower ? LOW : HIGH); IODevice::write(powerPin,invertPower ? LOW : HIGH);
if (DCinuse)
setDCSignal(curSpeedCode);
interrupts(); interrupts();
if (isProgTrack) if (isProgTrack)
DCCWaveform::progTrack.clearResets(); DCCWaveform::progTrack.clearResets();
} }
else { else {
noInterrupts(); noInterrupts();
if (DCinuse)
detachDCSignal();
IODevice::write(powerPin,invertPower ? HIGH : LOW); IODevice::write(powerPin,invertPower ? HIGH : LOW);
interrupts(); interrupts();
} }
@ -245,8 +249,9 @@ uint16_t taurustones[28] = { 165, 175, 196, 220,
220, 196, 175, 165 }; 220, 196, 175, 165 };
#endif #endif
void MotorDriver::setDCSignal(byte speedcode) { void MotorDriver::setDCSignal(byte speedcode) {
if (brakePin == UNUSED_PIN) curSpeedCode = speedcode;
return; DCinuse = true;
#if defined(ARDUINO_AVR_UNO) #if defined(ARDUINO_AVR_UNO)
TCCR2B = (TCCR2B & B11111000) | B00000110; // set divisor on timer 2 to result in (approx) 122.55Hz TCCR2B = (TCCR2B & B11111000) | B00000110; // set divisor on timer 2 to result in (approx) 122.55Hz
#endif #endif
@ -257,7 +262,7 @@ void MotorDriver::setDCSignal(byte speedcode) {
// spedcoode is a dcc speed & direction // spedcoode is a dcc speed & direction
byte tSpeed=speedcode & 0x7F; // DCC Speed with 0,1 stop and speed steps 2 to 127 byte tSpeed=speedcode & 0x7F; // DCC Speed with 0,1 stop and speed steps 2 to 127
byte tDir=speedcode & 0x80; byte tDir=speedcode & 0x80;
byte brake; byte pwmratio;
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
{ {
int f = 131; int f = 131;
@ -266,18 +271,18 @@ void MotorDriver::setDCSignal(byte speedcode) {
f = taurustones[ (tSpeed-2)/2 ] ; f = taurustones[ (tSpeed-2)/2 ] ;
} }
} }
DCCEXanalogWriteFrequency(brakePin, f); // set DC PWM frequency to 100Hz XXX May move to setup DCCEXanalogWriteFrequency(powerPin, f); // set DC PWM frequency to 100Hz XXX May move to setup
} }
#endif #endif
if (tSpeed <= 1) brake = 255; if (tSpeed <= 1) pwmratio = 0;
else if (tSpeed >= 127) brake = 0; else if (tSpeed >= 127) pwmratio = 255;
else brake = 2 * (128-tSpeed); else pwmratio = 2 * tSpeed;
if (invertBrake) if (invertPower)
brake=255-brake; pwmratio =255-pwmratio;
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
DCCEXanalogWrite(brakePin,brake); DCCEXanalogWrite(powerPin,pwmratio);
#else #else
analogWrite(brakePin,brake); analogWrite(powerPin,pwmratio);
#endif #endif
//DIAG(F("DCSignal %d"), speedcode); //DIAG(F("DCSignal %d"), speedcode);
if (HAVE_PORTA(fastSignalPin.shadowinout == &PORTA)) { if (HAVE_PORTA(fastSignalPin.shadowinout == &PORTA)) {

View File

@ -152,6 +152,7 @@ class MotorDriver {
#else #else
setDCSignal(128); setDCSignal(128);
#endif #endif
DCinuse = false;
}; };
int getCurrentRaw(bool fromISR=false); int getCurrentRaw(bool fromISR=false);
unsigned int raw2mA( int raw); unsigned int raw2mA( int raw);
@ -232,6 +233,7 @@ class MotorDriver {
static const int TRIP_CURRENT_PROG=250; static const int TRIP_CURRENT_PROG=250;
unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT; unsigned long power_sample_overload_wait = POWER_SAMPLE_OVERLOAD_WAIT;
unsigned int power_good_counter = 0; unsigned int power_good_counter = 0;
bool DCinuse = false;
byte curSpeedCode = 0;
}; };
#endif #endif

View File

@ -218,7 +218,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
if (!(mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX)) { if (!(mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX)) {
// DCC tracks need to have set the PWM to zero or they will not work. // DCC tracks need to have set the PWM to zero or they will not work.
track[trackToSet]->detachDCSignal(); track[trackToSet]->detachDCSignal();
track[trackToSet]->setBrake(false); //track[trackToSet]->setBrake(false);
} }
// EXT is a special case where the signal pin is // EXT is a special case where the signal pin is
@ -396,7 +396,7 @@ void TrackManager::setPower2(bool setProg,POWERMODE mode) {
case TRACK_MODE_DC: case TRACK_MODE_DC:
case TRACK_MODE_DCX: case TRACK_MODE_DCX:
if (setProg) break; if (setProg) break;
driver->setBrake(true); // DC starts with brake on //driver->setBrake(true); // DC starts with brake on
applyDCSpeed(t); // speed match DCC throttles applyDCSpeed(t); // speed match DCC throttles
driver->setPower(mode); driver->setPower(mode);
break; break;