9 #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega2560__) // Configuration for UNO/MEGA 15 #ifndef USE_ONLY1_INTERRUPT 23 #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER1 3199 24 #define DCC_ZERO_BIT_PULSE_DURATION_TIMER1 1599 26 #define DCC_ONE_BIT_TOTAL_DURATION_TIMER1 1855 27 #define DCC_ONE_BIT_PULSE_DURATION_TIMER1 927 28 if (DCCppConfig::DirectionMotorA != UNDEFINED_PIN)
30 pinMode(DCCppConfig::DirectionMotorA, INPUT);
31 digitalWrite(DCCppConfig::DirectionMotorA, LOW);
34 if (inSignalPin != UNDEFINED_PIN)
35 pinMode(inSignalPin, OUTPUT);
37 bitSet(TCCR1A, WGM10);
38 bitSet(TCCR1A, WGM11);
39 bitSet(TCCR1B, WGM12);
40 bitSet(TCCR1B, WGM13);
42 bitSet(TCCR1A, COM1B1);
43 bitSet(TCCR1A, COM1B0);
45 bitClear(TCCR1B, CS12);
46 bitClear(TCCR1B, CS11);
49 OCR1A = DCC_ONE_BIT_TOTAL_DURATION_TIMER1;
50 OCR1B = DCC_ONE_BIT_PULSE_DURATION_TIMER1;
52 if (DCCppConfig::SignalEnablePinMain != UNDEFINED_PIN)
53 pinMode(DCCppConfig::SignalEnablePinMain, OUTPUT);
55 mainRegs.loadPacket(1, RegisterList::idlePacket, 2, 0);
57 bitSet(TIMSK1, OCIE1B);
59 if (inSignalPin != UNDEFINED_PIN) {
60 pinMode(inSignalPin, OUTPUT);
61 DCCppConfig::SignalPortMaskMain = digitalPinToBitMask(inSignalPin);
62 DCCppConfig::SignalPortInMain = portInputRegister(digitalPinToPort(inSignalPin));
65 if (DCCppConfig::SignalEnablePinMain != UNDEFINED_PIN)
66 pinMode(DCCppConfig::SignalEnablePinMain, OUTPUT);
85 TCCR2B = (0 << CS22) | (1 << CS21) | (0 << CS20);
88 bitSet (TIMSK2, OCIE2A);
92 mainRegs.loadPacket(1, RegisterList::idlePacket, 2, 0);
98 #ifndef USE_ONLY1_INTERRUPT 101 #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) // Configuration for UNO 108 #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER0 49 109 #define DCC_ZERO_BIT_PULSE_DURATION_TIMER0 24 111 #define DCC_ONE_BIT_TOTAL_DURATION_TIMER0 28 112 #define DCC_ONE_BIT_PULSE_DURATION_TIMER0 14 114 if (DCCppConfig::DirectionMotorB != UNDEFINED_PIN)
116 pinMode(DCCppConfig::DirectionMotorB, INPUT);
117 digitalWrite(DCCppConfig::DirectionMotorB, LOW);
120 if (inSignalPin != UNDEFINED_PIN)
121 pinMode(inSignalPin, OUTPUT);
123 bitSet(TCCR0A, WGM00);
124 bitSet(TCCR0A, WGM01);
125 bitSet(TCCR0B, WGM02);
127 bitSet(TCCR0A, COM0B1);
128 bitSet(TCCR0A, COM0B0);
130 bitClear(TCCR0B, CS02);
131 bitSet(TCCR0B, CS01);
132 bitSet(TCCR0B, CS00);
134 OCR0A = DCC_ONE_BIT_TOTAL_DURATION_TIMER0;
135 OCR0B = DCC_ONE_BIT_PULSE_DURATION_TIMER0;
137 if (DCCppConfig::SignalEnablePinProg != UNDEFINED_PIN)
138 pinMode(DCCppConfig::SignalEnablePinProg, OUTPUT);
140 progRegs.loadPacket(1, RegisterList::idlePacket, 2, 0);
142 bitSet(TIMSK0, OCIE0B);
144 #else // Configuration for MEGA 151 #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER3 3199 152 #define DCC_ZERO_BIT_PULSE_DURATION_TIMER3 1599 154 #define DCC_ONE_BIT_TOTAL_DURATION_TIMER3 1855 155 #define DCC_ONE_BIT_PULSE_DURATION_TIMER3 927 157 if (DCCppConfig::DirectionMotorB != UNDEFINED_PIN)
159 pinMode(DCCppConfig::DirectionMotorB, INPUT);
160 digitalWrite(DCCppConfig::DirectionMotorB, LOW);
163 pinMode(DCC_SIGNAL_PIN_PROG, OUTPUT);
165 bitSet(TCCR3A, WGM30);
166 bitSet(TCCR3A, WGM31);
167 bitSet(TCCR3B, WGM32);
168 bitSet(TCCR3B, WGM33);
170 bitSet(TCCR3A, COM3B1);
171 bitSet(TCCR3A, COM3B0);
173 bitClear(TCCR3B, CS32);
174 bitClear(TCCR3B, CS31);
175 bitSet(TCCR3B, CS30);
177 OCR3A = DCC_ONE_BIT_TOTAL_DURATION_TIMER3;
178 OCR3B = DCC_ONE_BIT_PULSE_DURATION_TIMER3;
180 if (DCCppConfig::SignalEnablePinProg != UNDEFINED_PIN)
181 pinMode(DCCppConfig::SignalEnablePinProg, OUTPUT);
183 progRegs.loadPacket(1, RegisterList::idlePacket, 2, 0);
185 bitSet(TIMSK3, OCIE3B);
188 if (DCCppConfig::SignalEnablePinProg != UNDEFINED_PIN)
189 digitalWrite(DCCppConfig::SignalEnablePinProg, LOW);
192 if (inSignalPin != UNDEFINED_PIN) {
193 pinMode(inSignalPin, OUTPUT);
194 DCCppConfig::SignalPortMaskProg = digitalPinToBitMask(inSignalPin);
195 DCCppConfig::SignalPortInProg = portInputRegister(digitalPinToPort(inSignalPin));
198 if (DCCppConfig::SignalEnablePinProg != UNDEFINED_PIN)
199 pinMode(DCCppConfig::SignalEnablePinProg, OUTPUT);
204 bitSet (TCCR2A, WGM21);
206 TCCR2B = (0 << CS22) | (1 << CS21) | (0 << CS20);
208 bitSet (TIMSK2, OCIE2A);
214 #ifndef USE_ONLY1_INTERRUPT 247 #define DCC_SIGNAL(R,N) 248 if(R.currentBit==R.currentReg->activePacket->nBits){ 250 if (R.nRepeat>0 && R.currentReg == R.reg) { 253 else if (R.nextReg != NULL) { 254 R.currentReg = R.nextReg; 256 R.tempPacket = R.currentReg->activePacket; 257 R.currentReg->activePacket = R.currentReg->updatePacket; 258 R.currentReg->updatePacket = R.tempPacket; 261 if (R.currentReg == R.maxLoadedReg) 262 R.currentReg = R.reg; 267 if (R.currentReg->activePacket->buf[R.currentBit / 8] & R.bitMask[R.currentBit % 8]) { 268 OCR ## N ## A = DCC_ONE_BIT_TOTAL_DURATION_TIMER ## N; 269 OCR ## N ## B=DCC_ONE_BIT_PULSE_DURATION_TIMER ## N; 271 OCR ## N ## A=DCC_ZERO_BIT_TOTAL_DURATION_TIMER ## N; 272 OCR ## N ## B=DCC_ZERO_BIT_PULSE_DURATION_TIMER ## N; 280 ISR(TIMER1_COMPB_vect) {
281 DCC_SIGNAL(DCCpp::mainRegs, 1)
284 #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) // Configuration for UNO 286 ISR(TIMER0_COMPB_vect) {
287 DCC_SIGNAL(DCCpp::progRegs, 0)
290 #else // Configuration for MEGA 292 ISR(TIMER3_COMPB_vect) {
293 DCC_SIGNAL(DCCpp::progRegs, 3)
314 #define DCC_NEXT_BIT(R) 315 if(R.currentBit==R.currentReg->activePacket->nBits){ 317 if (R.nRepeat>0 && R.currentReg == R.reg){ 320 else if (R.nextReg != NULL){ 321 R.currentReg = R.nextReg; 323 R.tempPacket = R.currentReg->activePacket; 324 R.currentReg->activePacket = R.currentReg->updatePacket; 325 R.currentReg->updatePacket = R.tempPacket; 328 if (R.currentReg == R.maxLoadedReg) 329 R.currentReg = R.reg; 333 if(R.currentReg->activePacket->buf[R.currentBit/8] & R.bitMask[R.currentBit%8]) { 336 R.timerPeriodsLeft = 2; 340 R.timerPeriodsLeft = 4; 352 #define CHECK_TIMER_PERIOD(R,N) 353 R.timerPeriodsLeft--; 354 if(R.timerPeriodsLeft == R.timerPeriods) { 355 *DCCppConfig::SignalPortIn ## N = DCCppConfig::SignalPortMask ## N; 357 if(R.timerPeriodsLeft == 0) { 358 *DCCppConfig::SignalPortIn ## N = DCCppConfig::SignalPortMask ## N; 364 ISR(TIMER2_COMPA_vect) {
365 if(DCCppConfig::SignalPortMaskMain != 0)
366 CHECK_TIMER_PERIOD(DCCpp::mainRegs, Main)
367 if(DCCppConfig::SignalPortMaskProg != 0)
368 CHECK_TIMER_PERIOD(DCCpp::progRegs, Prog)
375 bitClear(TCCR1B, CS12);
376 bitSet(TCCR1B, CS11);
377 bitClear(TCCR1B, CS10);
379 #if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) // Configuration for UNO 381 bitSet(TCCR0B, CS02);
382 bitClear(TCCR0B, CS01);
383 bitClear(TCCR0B, CS00);
385 #else // Configuration for MEGA 387 bitClear(TCCR3B, CS32);
388 bitSet(TCCR3B, CS31);
389 bitClear(TCCR3B, CS30);
static void setDebugDccMode()
static void beginProgDccSignal(uint8_t inSignalPin)
static void beginMainDccSignal(uint8_t inSignalPin)