/*
Central DCC Bluetooth
Joaquim Herdy Menezes
< Joahermen >
2019C28
Original DCC++ BASE STATION
Autor Gregg E. Berman
CopyRight (c) 2013-2016
*********************************
* Criador Genial do Arduino *
* =============== *
* << Massimo Banzi >> *
* =============== *
*********************************
Modulos:
Arduino Uno R3
MotorShield L298 R3
Bluetooth HC-05 ou HC-06
Observacsoes:
Em hipotese alguma conectar o modulo Bluetooth nos pinos D0 e D1 do Arduino
enquanto ele estiver conectado via cabo USB para os testes
isolar o pino Vin do MotorShield L298R da ligacsao do Vin do Arduino
Fazer as conexoes dos pinos D5, D10, D12, D13 no interruptor Seletor conforme o esquema
ou sem interruptor Seletor jumpar o pino D5 com o pino D13 e o pino D10 com o pino D12
*/
#include "DCCpp_Uno.h"
#include "PacketRegister.h"
#include "CurrentMonitor.h"
#include "Sensor.h"
#include "SerialCommand.h"
#include "Accessories.h"
#include "EEStore.h"
#include "Config.h"
#include "Comm.h"
#include <SoftwareSerial.h>
// A T E N S A O:
// Arduino com Cabo USB o Bluetooth nao pode ser conectado nos pinos Rx Tx
SoftwareSerial Bluetooth(0,1);
void showConfiguration();
#if COMM_TYPE == 1
byte mac[] = MAC_ADDRESS;
EthernetServer INTERFACE(ETHERNET_PORT);
#endif
volatile RegisterList mainRegs(MAX_MAIN_REGISTERS);
volatile RegisterList progRegs(2);
CurrentMonitor mainMonitor(CURRENT_MONITOR_PIN_MAIN,"<p2>");
CurrentMonitor progMonitor(CURRENT_MONITOR_PIN_PROG,"<p3>");
void loop(){
SerialCommand::process();
if(CurrentMonitor::checkTime()){
mainMonitor.check();
progMonitor.check();
}
Sensor::check();
}
void setup(){
Serial.begin(9600);
// Serial.begin(57600);
// Serial.begin(115200);
Bluetooth.begin(9600);
// Bluetooth.begin(57600);
// Bluetooth.begin(115200);
Serial.flush();
#ifdef SDCARD_CS
pinMode(SDCARD_CS,OUTPUT);
digitalWrite(SDCARD_CS,HIGH);
#endif
EEStore::init();
pinMode(A3,INPUT);
digitalWrite(A3,HIGH);
if(!digitalRead(A3))
showConfiguration();
Serial.print(" = Central DCC RealJoia Bluetooth =\n < Arduino ");
Serial.print(ARDUINO_TYPE);
Serial.println(" >");
Serial.print(" > ");
Serial.print(MOTOR_SHIELD_NAME);
Serial.print(": V-");
Serial.print(VERSION);
Serial.println(" < ");
Serial.print(" | ");
Serial.print(__DATE__);
Serial.print(" ");
Serial.print(__TIME__);
Serial.println(" | ");
#if COMM_TYPE == 1
#ifdef IP_ADDRESS
Ethernet.begin(mac,IP_ADDRESS);
#else
Ethernet.begin(mac);
#endif
INTERFACE.begin();
#endif
SerialCommand::init(&mainRegs, &progRegs, &mainMonitor);
Serial.print("<N ");
Serial.print(COMM_TYPE);
#if COMM_TYPE == 0
Serial.println(" Serial>");
#elif COMM_TYPE == 1
Serial.print(" ");
Serial.print(Ethernet.localIP());
Serial.println(" >");
#endif
#define DCC_ZERO_BIT_TOTAL_DURATION_TIMER1 3199
#define DCC_ZERO_BIT_PULSE_DURATION_TIMER1 1599
#define DCC_ONE_BIT_TOTAL_DURATION_TIMER1 1855
#define DCC_ONE_BIT_PULSE_DURATION_TIMER1 927
pinMode(DIRECTION_MOTOR_CHANNEL_PIN_A,INPUT);
digitalWrite(DIRECTION_MOTOR_CHANNEL_PIN_A,LOW);
pinMode(DCC_SIGNAL_PIN_MAIN, OUTPUT);
bitSet(TCCR1A,WGM10);
bitSet(TCCR1A,WGM11);
bitSet(TCCR1B,WGM12);
bitSet(TCCR1B,WGM13);
bitSet(TCCR1A,COM1B1);
bitSet(TCCR1A,COM1B0);
bitClear(TCCR1B,CS12);
bitClear(TCCR1B,CS11);
bitSet(TCCR1B,CS10);
OCR1A=DCC_ONE_BIT_TOTAL_DURATION_TIMER1;
OCR1B=DCC_ONE_BIT_PULSE_DURATION_TIMER1;
pinMode(SIGNAL_ENABLE_PIN_MAIN,OUTPUT);
mainRegs.loadPacket(1,RegisterList::idlePacket,2,0);
bitSet(TIMSK1,OCIE1B);
#ifdef ARDUINO_AVR_UNO
#define DCC_ZERO_BIT_TOTAL_DURATION_TIMER0 49
#define DCC_ZERO_BIT_PULSE_DURATION_TIMER0 24
#define DCC_ONE_BIT_TOTAL_DURATION_TIMER0 28
#define DCC_ONE_BIT_PULSE_DURATION_TIMER0 14
pinMode(DIRECTION_MOTOR_CHANNEL_PIN_B,INPUT);
digitalWrite(DIRECTION_MOTOR_CHANNEL_PIN_B,LOW);
pinMode(DCC_SIGNAL_PIN_PROG,OUTPUT);
bitSet(TCCR0A,WGM00);
bitSet(TCCR0A,WGM01);
bitSet(TCCR0B,WGM02);
bitSet(TCCR0A,COM0B1);
bitSet(TCCR0A,COM0B0);
bitClear(TCCR0B,CS02);
bitSet(TCCR0B,CS01);
bitSet(TCCR0B,CS00);
OCR0A=DCC_ONE_BIT_TOTAL_DURATION_TIMER0;
OCR0B=DCC_ONE_BIT_PULSE_DURATION_TIMER0;
pinMode(SIGNAL_ENABLE_PIN_PROG,OUTPUT);
progRegs.loadPacket(1,RegisterList::idlePacket,2,0);
bitSet(TIMSK0,OCIE0B);
#else
#define DCC_ZERO_BIT_TOTAL_DURATION_TIMER3 3199
#define DCC_ZERO_BIT_PULSE_DURATION_TIMER3 1599
#define DCC_ONE_BIT_TOTAL_DURATION_TIMER3 1855
#define DCC_ONE_BIT_PULSE_DURATION_TIMER3 927
pinMode(DIRECTION_MOTOR_CHANNEL_PIN_B,INPUT);
digitalWrite(DIRECTION_MOTOR_CHANNEL_PIN_B,LOW);
pinMode(DCC_SIGNAL_PIN_PROG,OUTPUT);
bitSet(TCCR3A,WGM30);
bitSet(TCCR3A,WGM31);
bitSet(TCCR3B,WGM32);
bitSet(TCCR3B,WGM33);
bitSet(TCCR3A,COM3B1);
bitSet(TCCR3A,COM3B0);
bitClear(TCCR3B,CS32);
bitClear(TCCR3B,CS31);
bitSet(TCCR3B,CS30);
OCR3A=DCC_ONE_BIT_TOTAL_DURATION_TIMER3;
OCR3B=DCC_ONE_BIT_PULSE_DURATION_TIMER3;
pinMode(SIGNAL_ENABLE_PIN_PROG,OUTPUT);
progRegs.loadPacket(1,RegisterList::idlePacket,2,0);
bitSet(TIMSK3,OCIE3B);
#endif
}
#define DCC_SIGNAL(R,N) \
if(R.currentBit==R.currentReg->activePacket->nBits){\
R.currentBit=0;\
if(R.nRepeat>0 && R.currentReg==R.reg){\
R.nRepeat--;\
} else if(R.nextReg!=NULL){\
R.currentReg=R.nextReg;\
R.nextReg=NULL;\
R.tempPacket=R.currentReg->activePacket;\
R.currentReg->activePacket=R.currentReg->updatePacket;\
R.currentReg->updatePacket=R.tempPacket;\
} else{\
if(R.currentReg==R.maxLoadedReg)\
R.currentReg=R.reg;\
R.currentReg++;\
}\
}\
if(R.currentReg->activePacket->buf[R.currentBit/8] & R.bitMask[R.currentBit%8]){\
OCR ## N ## A=DCC_ONE_BIT_TOTAL_DURATION_TIMER ## N;\
OCR ## N ## B=DCC_ONE_BIT_PULSE_DURATION_TIMER ## N;\
} else{\
OCR ## N ## A=DCC_ZERO_BIT_TOTAL_DURATION_TIMER ## N;\
OCR ## N ## B=DCC_ZERO_BIT_PULSE_DURATION_TIMER ## N;\
}\
R.currentBit++;
ISR(TIMER1_COMPB_vect){
DCC_SIGNAL(mainRegs,1)
}
#ifdef ARDUINO_AVR_UNO
ISR(TIMER0_COMPB_vect){
DCC_SIGNAL(progRegs,0)
}
#else
ISR(TIMER3_COMPB_vect){
DCC_SIGNAL(progRegs,3)
}
#endif
void showConfiguration(){
int mac_address[]=MAC_ADDRESS;
Serial.print("\n*** DCC++ Configuracsao ***\n");
Serial.print("\nVersao: ");
Serial.print(VERSION);
Serial.print("\nCompilado: ");
Serial.print(__DATE__);
Serial.print(" ");
Serial.print(__TIME__);
Serial.print("\nArduino: ");
Serial.print(ARDUINO_TYPE);
Serial.print("\n\nMotorShield: ");
Serial.print(MOTOR_SHIELD_NAME);
Serial.print("\n\nDCC Sig Main: ");
Serial.print(DCC_SIGNAL_PIN_MAIN);
Serial.print("\n Direction: ");
Serial.print(DIRECTION_MOTOR_CHANNEL_PIN_A);
Serial.print("\n Enable: ");
Serial.print(SIGNAL_ENABLE_PIN_MAIN);
Serial.print("\n Monitor: ");
Serial.print(CURRENT_MONITOR_PIN_MAIN);
Serial.print("\n\nDCC Sig Prog: ");
Serial.print(DCC_SIGNAL_PIN_PROG);
Serial.print("\n Direction: ");
Serial.print(DIRECTION_MOTOR_CHANNEL_PIN_B);
Serial.print("\n Enable: ");
Serial.print(SIGNAL_ENABLE_PIN_PROG);
Serial.print("\n Monitor: ");
Serial.print(CURRENT_MONITOR_PIN_PROG);
Serial.print("\n\nNum Turnouts: ");
Serial.print(EEStore::eeStore->data.nTurnouts);
Serial.print("\n Sensores: ");
Serial.print(EEStore::eeStore->data.nSensors);
Serial.print("\n Outputs: ");
Serial.print(EEStore::eeStore->data.nOutputs);
Serial.print("\n\nInterface: ");
#if COMM_TYPE == 0
Serial.print("Serial");
#elif COMM_TYPE == 1
Serial.print(COMM_SHIELD_NAME);
Serial.print("\nMAC Address: ");
for(int i=0;i<5;i++){
Serial.print(mac_address[i],HEX);
Serial.print(":");
}
Serial.print(mac_address[5],HEX);
Serial.print("\nPorta: ");
Serial.print(ETHERNET_PORT);
Serial.print("\nIP Address: ");
#ifdef IP_ADDRESS
Ethernet.begin(mac,IP_ADDRESS);
#else
Ethernet.begin(mac);
#endif
Serial.print(Ethernet.localIP());
#ifdef IP_ADDRESS
Serial.print(" (Static)");
#else
Serial.print(" (DHCP)");
#endif
#endif
Serial.print("\n\nPrograma Parado - Resete o Arduino");
while(true);
}
// Joahermen - 21 de Agosto de 2019
|