From 3f57c1210d129404b62fdb9bccd9441cf52c5e4b Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Tue, 17 May 2022 08:06:10 +0200 Subject: [PATCH] turn off signal pin during cutout as that might switch some H-bridges between high and low side --- DCCTimer.cpp | 33 ++++++++++++++++++++++++++++++--- DCCTimer.h | 1 + MotorDriver.cpp | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/DCCTimer.cpp b/DCCTimer.cpp index 97f093d..147f1f0 100644 --- a/DCCTimer.cpp +++ b/DCCTimer.cpp @@ -227,6 +227,14 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { TCNT4 = 0; // this timer fires half cycle after Timer 1 (no idea why /4 !) #endif + // turn on PWM for the pins here (instead of in setPWM()) + // only needed if RailCom is inactive as otherwise RailCom + // takes care of that. Does not hurt tough + onoffPWM(TIMER1_A_PIN, true); + onoffPWM(TIMER1_B_PIN, true); +#ifdef TIMER1_C_PIN + onoffPWM(TIMER1_C_PIN, true); +#endif interrupts(); } @@ -253,19 +261,38 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { false; } +void DCCTimer::onoffPWM(byte pin, bool on) { + if (pin==TIMER1_A_PIN) { + if (on) + TCCR1A |= _BV(COM1A1); + else + TCCR1A &= ~(_BV(COM1A1)); + } + else if (pin==TIMER1_B_PIN) { + if (on) + TCCR1A |= _BV(COM1B1); + else + TCCR1A &= ~(_BV(COM1B1)); + } +#ifdef TIMER1_C_PIN + else if (pin==TIMER1_C_PIN) { + if (on) + TCCR1A |= _BV(COM1C1); + else + TCCR1A &= ~(_BV(COM1C1)); + } +#endif +} void DCCTimer::setPWM(byte pin, bool high) { uint16_t val=high?1024:0; if (pin==TIMER1_A_PIN) { - TCCR1A |= _BV(COM1A1); OCR1A= val; } else if (pin==TIMER1_B_PIN) { - TCCR1A |= _BV(COM1B1); OCR1B= val; } #ifdef TIMER1_C_PIN else if (pin==TIMER1_C_PIN) { - TCCR1A |= _BV(COM1C1); OCR1C= val; } #endif diff --git a/DCCTimer.h b/DCCTimer.h index 0452bb7..beda7fd 100644 --- a/DCCTimer.h +++ b/DCCTimer.h @@ -11,6 +11,7 @@ class DCCTimer { static bool isPWMPin(byte pin); static bool isRailcomPin(byte pin); static void setPWM(byte pin, bool high); + static void onoffPWM(byte pin, bool on); #if (defined(TEENSYDUINO) && !defined(__IMXRT1062__)) static void read_mac(byte mac[6]); static void read(uint8_t word, uint8_t *mac, uint8_t offset); diff --git a/MotorDriver.cpp b/MotorDriver.cpp index babd102..86574ae 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -115,6 +115,7 @@ void MotorDriver::setBrake(bool on) { } void MotorDriver::setRailcomCutout(bool on) { + DCCTimer::onoffPWM(signalPin,!on); DCCTimer::setPWM(brakePin,on ^ invertBrake); }