mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
make inverted power pin possible, lower DC frequency on Mega and for Uno if inverted def is used
This commit is contained in:
parent
cec6d6dbe7
commit
7d1d6bf1e1
|
@ -56,10 +56,17 @@ volatile portreg_t shadowPORTA;
|
||||||
volatile portreg_t shadowPORTB;
|
volatile portreg_t shadowPORTB;
|
||||||
volatile portreg_t shadowPORTC;
|
volatile portreg_t shadowPORTC;
|
||||||
|
|
||||||
MotorDriver::MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin,
|
MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin,
|
||||||
byte current_pin, float sense_factor, unsigned int trip_milliamps, byte fault_pin) {
|
byte current_pin, float sense_factor, unsigned int trip_milliamps, byte fault_pin) {
|
||||||
powerPin=power_pin;
|
powerPin=power_pin;
|
||||||
IODevice::write(powerPin,LOW);// set to OUTPUT and off
|
invertPower=power_pin < 0;
|
||||||
|
if (invertPower) {
|
||||||
|
powerPin = 0-power_pin;
|
||||||
|
IODevice::write(powerPin,HIGH);// set to OUTPUT and off
|
||||||
|
} else {
|
||||||
|
powerPin = power_pin;
|
||||||
|
IODevice::write(powerPin,LOW);// set to OUTPUT and off
|
||||||
|
}
|
||||||
|
|
||||||
signalPin=signal_pin;
|
signalPin=signal_pin;
|
||||||
getFastPin(F("SIG"),signalPin,fastSignalPin);
|
getFastPin(F("SIG"),signalPin,fastSignalPin);
|
||||||
|
@ -165,14 +172,14 @@ void MotorDriver::setPower(POWERMODE mode) {
|
||||||
bool on=mode==POWERMODE::ON;
|
bool on=mode==POWERMODE::ON;
|
||||||
if (on) {
|
if (on) {
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
IODevice::write(powerPin,HIGH);
|
IODevice::write(powerPin,invertPower ? LOW : HIGH);
|
||||||
interrupts();
|
interrupts();
|
||||||
if (isProgTrack)
|
if (isProgTrack)
|
||||||
DCCWaveform::progTrack.clearResets();
|
DCCWaveform::progTrack.clearResets();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
IODevice::write(powerPin,LOW);
|
IODevice::write(powerPin,invertPower ? HIGH : LOW);
|
||||||
interrupts();
|
interrupts();
|
||||||
}
|
}
|
||||||
powerMode=mode;
|
powerMode=mode;
|
||||||
|
@ -232,6 +239,13 @@ void MotorDriver::setDCSignal(byte speedcode) {
|
||||||
return;
|
return;
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
DCCEXanalogWriteFrequency(brakePin, 100); // set DC PWM frequency to 100Hz XXX May move to setup
|
DCCEXanalogWriteFrequency(brakePin, 100); // set DC PWM frequency to 100Hz XXX May move to setup
|
||||||
|
#endif
|
||||||
|
#if defined(ARDUINO_AVR_UNO)
|
||||||
|
TCCR2B = TCCR2B & B11111000 | B00000110; // set divisor on timer 2 to result in (approx) 122.55Hz
|
||||||
|
#endif
|
||||||
|
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
||||||
|
TCCR2B = TCCR2B & B11111000 | B00000110; // set divisor on timer 2 to result in (approx) 122.55Hz
|
||||||
|
TCCR4B = TCCR4B & B11111000 | B00000100; // same for timer 4 but maxcount and thus divisor differs
|
||||||
#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
|
||||||
|
|
|
@ -111,7 +111,7 @@ enum class POWERMODE : byte { OFF, ON, OVERLOAD };
|
||||||
class MotorDriver {
|
class MotorDriver {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
MotorDriver(VPIN power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin,
|
MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin,
|
||||||
byte current_pin, float senseFactor, unsigned int tripMilliamps, byte faultPin);
|
byte current_pin, float senseFactor, unsigned int tripMilliamps, byte faultPin);
|
||||||
void setPower( POWERMODE mode);
|
void setPower( POWERMODE mode);
|
||||||
POWERMODE getPower() { return powerMode;}
|
POWERMODE getPower() { return powerMode;}
|
||||||
|
@ -194,6 +194,7 @@ class MotorDriver {
|
||||||
FASTPIN fastSignalPin, fastSignalPin2, fastBrakePin,fastFaultPin;
|
FASTPIN fastSignalPin, fastSignalPin2, fastBrakePin,fastFaultPin;
|
||||||
bool dualSignal; // true to use signalPin2
|
bool dualSignal; // true to use signalPin2
|
||||||
bool invertBrake; // brake pin passed as negative means pin is inverted
|
bool invertBrake; // brake pin passed as negative means pin is inverted
|
||||||
|
bool invertPower; // power pin passed as negative means pin is inverted
|
||||||
|
|
||||||
// Raw to milliamp conversion factors avoiding float data types.
|
// Raw to milliamp conversion factors avoiding float data types.
|
||||||
// Milliamps=rawADCreading * sensefactorInternal / senseScale
|
// Milliamps=rawADCreading * sensefactorInternal / senseScale
|
||||||
|
|
Loading…
Reference in New Issue
Block a user