mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2025-02-21 08:16:04 +01:00
DC via power pin 2nd part
This commit is contained in:
parent
9482041799
commit
0e36b3b997
@ -85,7 +85,6 @@ MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, i
|
|||||||
getFastPin(F("BRAKE"),brakePin,fastBrakePin);
|
getFastPin(F("BRAKE"),brakePin,fastBrakePin);
|
||||||
// if brake is used for railcom cutout we need to do PORTX register trick here as well
|
// if brake is used for railcom cutout we need to do PORTX register trick here as well
|
||||||
pinMode(brakePin, OUTPUT);
|
pinMode(brakePin, OUTPUT);
|
||||||
setBrake(true); // start with brake on in case we hace DC stuff going on
|
|
||||||
}
|
}
|
||||||
else brakePin=UNUSED_PIN;
|
else brakePin=UNUSED_PIN;
|
||||||
|
|
||||||
@ -146,16 +145,14 @@ void MotorDriver::setPower(POWERMODE mode) {
|
|||||||
if (on) {
|
if (on) {
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
IODevice::write(powerPin,invertPower ? LOW : HIGH);
|
IODevice::write(powerPin,invertPower ? LOW : HIGH);
|
||||||
|
interrupts();
|
||||||
if (DCinuse)
|
if (DCinuse)
|
||||||
setDCSignal(curSpeedCode);
|
setDCSignal(curSpeedCode);
|
||||||
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();
|
||||||
}
|
}
|
||||||
@ -253,11 +250,35 @@ void MotorDriver::setDCSignal(byte speedcode) {
|
|||||||
DCinuse = true;
|
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
|
if (powerPin == 3 || powerPin == 11)
|
||||||
|
TCCR2B = (TCCR2B & B11111000) | B00000110; // D3, D11: set divisor on timer 2 to result in (approx) 122.55Hz
|
||||||
#endif
|
#endif
|
||||||
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
||||||
TCCR2B = (TCCR2B & B11111000) | B00000110; // set divisor on timer 2 to result in (approx) 122.55Hz
|
// As timer 0 is the system timer, we leave it alone
|
||||||
TCCR4B = (TCCR4B & B11111000) | B00000100; // same for timer 4 but maxcount and thus divisor differs
|
//TCCR0B = (TCCR0B & B11111000) | B00000100; // D4, D13 : 122 or 244Hz?
|
||||||
|
// As we use timer 1 for DCC we leave it alone
|
||||||
|
//TCCR1B = (TCCR1B & B11111000) | B00000100; // D11, D12 : 122Hz
|
||||||
|
switch(powerPin) {
|
||||||
|
case 9:
|
||||||
|
case 10:
|
||||||
|
TCCR2B = (TCCR2B & B11111000) | B00000110; // D9, D10 : 122Hz
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
case 5:
|
||||||
|
TCCR3B = (TCCR3B & B11111000) | B00000100; // D2, D3, D5 : 122Hz but maxcount and thus divisor differs
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
case 7:
|
||||||
|
case 8:
|
||||||
|
TCCR4B = (TCCR4B & B11111000) | B00000100; // D6, D7, D8 : 122Hz but maxcount and thus divisor differs
|
||||||
|
break;
|
||||||
|
case 44:
|
||||||
|
case 45:
|
||||||
|
case 46:
|
||||||
|
TCCR5B = (TCCR5B & B11111000) | B00000100; // D44,D45,D46: 122Hz but maxcount and thus divisor differs
|
||||||
|
break;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
// 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
|
||||||
|
@ -146,9 +146,9 @@ class MotorDriver {
|
|||||||
void setDCSignal(byte speedByte);
|
void setDCSignal(byte speedByte);
|
||||||
inline void detachDCSignal() {
|
inline void detachDCSignal() {
|
||||||
#if defined(__arm__)
|
#if defined(__arm__)
|
||||||
pinMode(brakePin, OUTPUT);
|
pinMode(powerPin, OUTPUT);
|
||||||
#elif defined(ARDUINO_ARCH_ESP32)
|
#elif defined(ARDUINO_ARCH_ESP32)
|
||||||
ledcDetachPin(brakePin);
|
ledcDetachPin(powerPin);
|
||||||
#else
|
#else
|
||||||
setDCSignal(128);
|
setDCSignal(128);
|
||||||
#endif
|
#endif
|
||||||
@ -157,15 +157,16 @@ class MotorDriver {
|
|||||||
int getCurrentRaw(bool fromISR=false);
|
int getCurrentRaw(bool fromISR=false);
|
||||||
unsigned int raw2mA( int raw);
|
unsigned int raw2mA( int raw);
|
||||||
unsigned int mA2raw( unsigned int mA);
|
unsigned int mA2raw( unsigned int mA);
|
||||||
inline bool brakeCanPWM() {
|
inline bool powerPinCanPWM() {
|
||||||
#if defined(ARDUINO_ARCH_ESP32) || defined(__arm__)
|
#if defined(ARDUINO_ARCH_ESP32) || defined(__arm__)
|
||||||
// TODO: on ARM we can use digitalPinHasPWM, and may wish/need to
|
// TODO: on ARM we can use digitalPinHasPWM, and may wish/need to
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
#ifdef digitalPinToTimer
|
#ifdef digitalPinHasPWM
|
||||||
return ((brakePin!=UNUSED_PIN) && (digitalPinToTimer(brakePin)));
|
return digitalPinHasPWM(powerPin);
|
||||||
#else
|
#else
|
||||||
return (brakePin<14 && brakePin >1);
|
#warning No good digitalPinHasPWM doing approximation
|
||||||
|
return (powerPin<14 && powerPin >1);
|
||||||
#endif //digitalPinToTimer
|
#endif //digitalPinToTimer
|
||||||
#endif //ESP32/ARM
|
#endif //ESP32/ARM
|
||||||
}
|
}
|
||||||
|
@ -177,10 +177,10 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
|
|||||||
if (trackToSet>lastTrack || track[trackToSet]==NULL) return false;
|
if (trackToSet>lastTrack || track[trackToSet]==NULL) return false;
|
||||||
|
|
||||||
//DIAG(F("Track=%c"),trackToSet+'A');
|
//DIAG(F("Track=%c"),trackToSet+'A');
|
||||||
// DC tracks require a motorDriver that can set brake!
|
// DC tracks require a motorDriver that can set power pin PWM
|
||||||
if ((mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX)
|
if ((mode==TRACK_MODE_DC || mode==TRACK_MODE_DCX)
|
||||||
&& !track[trackToSet]->brakeCanPWM()) {
|
&& !track[trackToSet]->powerPinCanPWM()) {
|
||||||
DIAG(F("Brake pin can't PWM: No DC"));
|
DIAG(F("Power pin can't PWM: No DC"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,7 +218,6 @@ 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EXT is a special case where the signal pin is
|
// EXT is a special case where the signal pin is
|
||||||
@ -396,7 +395,6 @@ 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
|
|
||||||
applyDCSpeed(t); // speed match DCC throttles
|
applyDCSpeed(t); // speed match DCC throttles
|
||||||
driver->setPower(mode);
|
driver->setPower(mode);
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user