From 7e58165db9aa35e1ca60e6c7e053099c97935674 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Tue, 11 May 2021 16:33:12 +0100 Subject: [PATCH] First working --- DCCTimer.cpp | 25 +++++++++++-------------- DCCWaveform.cpp | 6 +++--- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/DCCTimer.cpp b/DCCTimer.cpp index 7ffa3a5..1a01a30 100644 --- a/DCCTimer.cpp +++ b/DCCTimer.cpp @@ -164,7 +164,7 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { #define TIMER1_B_PIN 12 #define TIMER1_C_PIN 13 //railcom timer facility - // #define TIMER4_A_PIN 6 + #define TIMER4_A_PIN 6 #define TIMER4_B_PIN 7 #define TIMER4_C_PIN 8 #else @@ -178,19 +178,20 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { ADCSRA = (ADCSRA & 0b11111000) | 0b00000100; // speed up analogRead sample time TCCR1A = 0; ICR1 = CLOCK_CYCLES; - TCNT1 = 0; TCCR1B = _BV(WGM13) | _BV(CS10); // Mode 8, clock select 1 TIMSK1 = _BV(TOIE1); // Enable Software interrupt + TCNT1 = 0; #if defined(TIMER4_A_PIN) //railcom timer facility TCCR4A = 0; ICR4 = CLOCK_CYCLES; - TCNT4 = CLOCK_CYCLES/2; // this timer fires half cycle after Timer 1 - TCCR4B = _BV(WGM13) | _BV(CS10); // Mode 8, clock select 1 + 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(); } @@ -208,7 +209,7 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { } // Alternative pin manipulation via PWM control. bool DCCTimer::isRailcomPin(byte pin) { - return + return #ifdef TIMER4_A_PIN pin==TIMER4_A_PIN || pin==TIMER4_B_PIN || @@ -218,7 +219,7 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { } void DCCTimer::setPWM(byte pin, bool high) { - byte val=high?1024:0; + uint16_t val=high?1024:0; if (pin==TIMER1_A_PIN) { TCCR1A |= _BV(COM1A1); OCR1A= val; @@ -235,19 +236,15 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) { #endif #ifdef TIMER4_A_PIN else if (pin==TIMER4_A_PIN) { - TCCR4A |= _BV(COM4A1); //?????????????????????????????????? + TCCR4A |= _BV(COM4A1); OCR4A= val; } - #endif - #ifdef TIMER4_B_PIN else if (pin==TIMER4_B_PIN) { - TCCR4B |= _BV(COM4B1); //?????????????????????????????????? + TCCR4A |= _BV(COM4B1); OCR4B= val; } - #endif - #ifdef TIMER4_C_PIN else if (pin==TIMER4_C_PIN) { - TCCR4C |= _BV(COM4C1); //?????????????????????????????????? + TCCR4A |= _BV(COM4C1); OCR4C= val; } #endif diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index e75060e..2ab014f 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -210,12 +210,12 @@ void DCCWaveform::interrupt2() { state=WAVE_MID_1; // switch state to trigger LOW on next interrupt remainingPreambles--; // Railcom cutout - if (remainingPreambles==requiredPreambles-2) motorDriver->setRailcomCutout(true); - else if (remainingPreambles==requiredPreambles-4) motorDriver->setRailcomCutout(false); + if (remainingPreambles==(requiredPreambles-2)) motorDriver->setRailcomCutout(true); + else if (remainingPreambles==(requiredPreambles-4)) motorDriver->setRailcomCutout(false); // Update free memory diagnostic as we don't have anything else to do this time. // Allow for checkAck and its called functions using 22 bytes more. - updateMinimumFreeMemory(22); + else updateMinimumFreeMemory(22); return; }