From ce892974ab83ef9a6d4dd9d2d6707db08fc51a56 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 3 Jul 2022 10:22:30 +0200 Subject: [PATCH] only set TCCR1A register for pins that are signal pins, done by calling setRailcomCutout() at right place instead of doing it for all pins in the timer code --- DCCTimer.cpp | 25 ++++++++----------------- MotorDriver.cpp | 4 ++++ 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/DCCTimer.cpp b/DCCTimer.cpp index 147f1f0..efc74a7 100644 --- a/DCCTimer.cpp +++ b/DCCTimer.cpp @@ -217,23 +217,14 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { TIMSK1 = _BV(TOIE1); // Enable Software interrupt TCNT1 = 0; - #if defined(TIMER4_A_PIN) - //railcom timer facility - TCCR4A = 0; - ICR4 = CLOCK_CYCLES; - TCCR4B = _BV(WGM43) | _BV(CS40); // Mode 8, clock select 1 - TIMSK4 = 0; // Disable Software interrupt - delayMicroseconds(DCC_SIGNAL_TIME/2); - 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); +#if defined(TIMER4_A_PIN) + //railcom timer facility + TCCR4A = 0; + ICR4 = CLOCK_CYCLES; + TCCR4B = _BV(WGM43) | _BV(CS40); // Mode 8, clock select 1 + TIMSK4 = 0; // Disable Software interrupt + delayMicroseconds(DCC_SIGNAL_TIME/2); + TCNT4 = 0; // this timer fires half cycle after Timer 1 (no idea why /4 !) #endif interrupts(); } diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 86574ae..d08e41a 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -56,6 +56,10 @@ MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8 setBrake(false); } else brakePin=UNUSED_PIN; + + // Initiate state of railcom cutout as off. This must be done + // independent of if railcom is used later or not. + setRailcomCutout(false); currentPin=current_pin; if (currentPin!=UNUSED_PIN) {