Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Committer:
carlosperales95
Date:
Thu Jun 07 12:54:59 2018 +0000
Revision:
19:ff21ba3a4dc5
Parent:
17:0a657e338356
Child:
20:608ab9bf340d
Added DCC vars for switches

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mglmx 0:4d06a6a8e785 1 #include "mbed.h"
mglmx 3:fe7010b693a0 2 #include "TextLCD.h"
mglmx 1:0ab26889af9b 3 //mbed DCC Model Train Demo
mglmx 1:0ab26889af9b 4
carlosperales95 7:e2b8461d4f05 5 /******PINS AND DECLARATIONS*******/
carlosperales95 7:e2b8461d4f05 6
carlosperales95 7:e2b8461d4f05 7 ///p5
carlosperales95 7:e2b8461d4f05 8 ///p6
carlosperales95 7:e2b8461d4f05 9 ///p7
carlosperales95 7:e2b8461d4f05 10 ///p8
carlosperales95 7:e2b8461d4f05 11
carlosperales95 10:2088b1935a93 12 //RAIL SENSORS - INT0,INT1
carlosperales95 7:e2b8461d4f05 13 //INT0 - p9
carlosperales95 10:2088b1935a93 14 InterruptIn sensors0(p9);
carlosperales95 7:e2b8461d4f05 15 //INT1 - p10
carlosperales95 10:2088b1935a93 16 InterruptIn sensors1(p10);
carlosperales95 7:e2b8461d4f05 17
carlosperales95 7:e2b8461d4f05 18 ///p11
carlosperales95 7:e2b8461d4f05 19 ///p12
carlosperales95 12:e914ca5cd44b 20
carlosperales95 7:e2b8461d4f05 21 //M0 - p13
carlosperales95 7:e2b8461d4f05 22 DigitalIn d21(p13);
carlosperales95 7:e2b8461d4f05 23 //M1 - p14
carlosperales95 7:e2b8461d4f05 24 DigitalIn d22(p14);
carlosperales95 7:e2b8461d4f05 25 //M2 - p15
carlosperales95 7:e2b8461d4f05 26 DigitalIn d23(p15);
carlosperales95 12:e914ca5cd44b 27
carlosperales95 7:e2b8461d4f05 28 //p16
carlosperales95 12:e914ca5cd44b 29 //p17
carlosperales95 12:e914ca5cd44b 30
carlosperales95 11:021210c59a95 31 //BUZZER - p18
carlosperales95 11:021210c59a95 32 DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps
carlosperales95 7:e2b8461d4f05 33
carlosperales95 7:e2b8461d4f05 34 //POTENTIOMETER - p19
carlosperales95 13:dbf1ead12cee 35 AnalogIn pot(p19); //Gives float value pot.read(). Convert analog input to V with f*3.3
carlosperales95 7:e2b8461d4f05 36
carlosperales95 7:e2b8461d4f05 37 //DAT - p20
mglmx 3:fe7010b693a0 38 DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge
carlosperales95 7:e2b8461d4f05 39
carlosperales95 7:e2b8461d4f05 40 //LCD SCREEN - p21, p22, p23, p24, p25, p26
carlosperales95 11:021210c59a95 41 TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7 // ldc.cls() to clear and printf(String up to 16char)
carlosperales95 7:e2b8461d4f05 42
carlosperales95 7:e2b8461d4f05 43 ///p27
carlosperales95 7:e2b8461d4f05 44 ///p28
carlosperales95 7:e2b8461d4f05 45
carlosperales95 7:e2b8461d4f05 46 //LED1 - p29
mglmx 3:fe7010b693a0 47 DigitalOut redled(p29);
carlosperales95 7:e2b8461d4f05 48 //LED2 - p30
mglmx 3:fe7010b693a0 49 DigitalOut greenled(p30);
carlosperales95 7:e2b8461d4f05 50
mglmx 5:ce0f66ea12c5 51
mglmx 16:2a2da0e67793 52 //Rail sensors
mglmx 16:2a2da0e67793 53 InterruptIn int0(p9);
mglmx 16:2a2da0e67793 54 InterruptIn int1(p10);
mglmx 16:2a2da0e67793 55
mglmx 16:2a2da0e67793 56 //MCP
mglmx 16:2a2da0e67793 57 MCP23017 *mcp;
carlosperales95 12:e914ca5cd44b 58
carlosperales95 11:021210c59a95 59 //**************** FUNCTIONS FOR DENVER TRAIN ****************//
carlosperales95 10:2088b1935a93 60
mglmx 16:2a2da0e67793 61 void interrupt0(){
mglmx 16:2a2da0e67793 62 int data = mcp->readRegister(GPIO);
mglmx 17:0a657e338356 63 lcd.cls();
mglmx 16:2a2da0e67793 64 lcd.printf("int0 %d",data);
mglmx 17:0a657e338356 65 buzz = 1;
mglmx 17:0a657e338356 66 wait(0.5);
mglmx 17:0a657e338356 67 buzz = 0;
mglmx 16:2a2da0e67793 68 }
mglmx 16:2a2da0e67793 69
mglmx 16:2a2da0e67793 70 void interrupt1(){
mglmx 16:2a2da0e67793 71 int data = mcp->readRegister(GPIO);
mglmx 17:0a657e338356 72 lcd.cls();
mglmx 17:0a657e338356 73 lcd.printf("int1 %d",data);
mglmx 17:0a657e338356 74 buzz = 1;
mglmx 17:0a657e338356 75 wait(0.5);
mglmx 17:0a657e338356 76 buzz = 0;
mglmx 16:2a2da0e67793 77 }
mglmx 16:2a2da0e67793 78
mglmx 16:2a2da0e67793 79 void initalize_mcp(){
mglmx 16:2a2da0e67793 80 mcp = new MPC23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40
mglmx 16:2a2da0e67793 81
mglmx 16:2a2da0e67793 82 mcp->reset();
mglmx 16:2a2da0e67793 83 mcp->writeRegister(0x00, (unsigned char )0xff);
mglmx 16:2a2da0e67793 84 mcp->writeRegister(0x01, (unsigned char )0xff);
mglmx 16:2a2da0e67793 85 mcp->writeRegister(0x02, (unsigned char )0x00);
mglmx 16:2a2da0e67793 86 mcp->writeRegister(0x03, (unsigned char )0x00);
mglmx 16:2a2da0e67793 87 mcp->writeRegister(0x04, (unsigned char )0xff);
mglmx 16:2a2da0e67793 88 mcp->writeRegister(0x05, (unsigned char )0xff);
mglmx 16:2a2da0e67793 89 mcp->writeRegister(0x06, (unsigned char )0xff);
mglmx 16:2a2da0e67793 90 mcp->writeRegister(0x07, (unsigned char )0xff);
mglmx 16:2a2da0e67793 91 mcp->writeRegister(0x08, (unsigned char )0xff);
mglmx 16:2a2da0e67793 92 mcp->writeRegister(0x09, (unsigned char )0xff);
mglmx 16:2a2da0e67793 93 mcp->writeRegister(0x0a, (unsigned char )0x42);
mglmx 16:2a2da0e67793 94 mcp->writeRegister(0x0b, (unsigned char )0x42);
mglmx 16:2a2da0e67793 95 mcp->writeRegister(0x0c, (unsigned char )0x00);
mglmx 16:2a2da0e67793 96 mcp->writeRegister(0x0d, (unsigned char )0x00);
mglmx 16:2a2da0e67793 97 }
mglmx 5:ce0f66ea12c5 98
mglmx 1:0ab26889af9b 99
mglmx 1:0ab26889af9b 100 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
mglmx 1:0ab26889af9b 101 {
mglmx 1:0ab26889af9b 102 unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
mglmx 1:0ab26889af9b 103 unsigned __int64 temp_command = 0x0000000000000000;
mglmx 1:0ab26889af9b 104 unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
mglmx 1:0ab26889af9b 105 unsigned int error = 0x00; //error byte
mglmx 1:0ab26889af9b 106 //calculate error detection byte with xor
mglmx 1:0ab26889af9b 107 error = address ^ inst;
mglmx 1:0ab26889af9b 108 //combine packet bits in basic DCC format
mglmx 1:0ab26889af9b 109 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
mglmx 1:0ab26889af9b 110 //printf("\n\r %llx \n\r",command);
mglmx 1:0ab26889af9b 111 int i=0;
mglmx 1:0ab26889af9b 112 //repeat DCC command lots of times
mglmx 1:0ab26889af9b 113 while(i < repeat_count) {
mglmx 1:0ab26889af9b 114 temp_command = command;
mglmx 1:0ab26889af9b 115 //loops through packet bits encoding and sending out digital pulses for a DCC command
mglmx 1:0ab26889af9b 116 for (int j=0; j<64; j++) {
mglmx 1:0ab26889af9b 117 if((temp_command&0x8000000000000000)==0) { //test packet bit
mglmx 1:0ab26889af9b 118 //send data for a "0" bit
mglmx 1:0ab26889af9b 119 Track=0;
mglmx 1:0ab26889af9b 120 wait_us(100);
mglmx 1:0ab26889af9b 121 Track=1;
mglmx 1:0ab26889af9b 122 wait_us(100);
mglmx 1:0ab26889af9b 123 //printf("0011");
mglmx 1:0ab26889af9b 124 } else {
mglmx 1:0ab26889af9b 125 //send data for a "1"bit
mglmx 1:0ab26889af9b 126 Track=0;
mglmx 1:0ab26889af9b 127 wait_us(58);
mglmx 1:0ab26889af9b 128 Track=1;
mglmx 1:0ab26889af9b 129 wait_us(58);
mglmx 1:0ab26889af9b 130 //printf("01");
mglmx 1:0ab26889af9b 131 }
mglmx 1:0ab26889af9b 132 // next bit in packet
mglmx 1:0ab26889af9b 133 temp_command = temp_command<<1;
mglmx 1:0ab26889af9b 134 }
mglmx 1:0ab26889af9b 135 i++;
mglmx 0:4d06a6a8e785 136 }
mglmx 0:4d06a6a8e785 137 }
carlosperales95 11:021210c59a95 138
carlosperales95 11:021210c59a95 139
carlosperales95 12:e914ca5cd44b 140
carlosperales95 11:021210c59a95 141 //**************** MAIN PROGRAM FOR DENVER TRAIN ****************//
carlosperales95 11:021210c59a95 142
mglmx 1:0ab26889af9b 143 int main()
mglmx 1:0ab26889af9b 144 {
mglmx 2:f580707c44fa 145 led1 = 1;
mglmx 2:f580707c44fa 146 wait(0.5);
mglmx 2:f580707c44fa 147 led1 = 0;
mglmx 2:f580707c44fa 148 wait(0.5);
mglmx 2:f580707c44fa 149 led1 = 1;
mglmx 16:2a2da0e67793 150
mglmx 16:2a2da0e67793 151 initialize_mcp();
mglmx 16:2a2da0e67793 152
mglmx 16:2a2da0e67793 153 int0.rise($interrupt0);
mglmx 16:2a2da0e67793 154 int1.rise($interrupt1);
mglmx 16:2a2da0e67793 155
mglmx 16:2a2da0e67793 156
mglmx 1:0ab26889af9b 157 //typical out of box default engine DCC address is 3 (at least for Bachmann trains)
mglmx 1:0ab26889af9b 158 //Note: A DCC controller can reprogram the address whenever needed
mglmx 1:0ab26889af9b 159 unsigned int DCCaddress = 0x01;
mglmx 1:0ab26889af9b 160 //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf
mglmx 1:0ab26889af9b 161 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
mglmx 1:0ab26889af9b 162 unsigned int DCCinst_forward = 0x68; //forward half speed
mglmx 1:0ab26889af9b 163 unsigned int DCCinst_reverse = 0x48; //reverse half speed
mglmx 4:50879dfb82d5 164 unsigned int DCCinst_stop = 0x50;
mglmx 1:0ab26889af9b 165 //100DDDDD for basic headlight functions
mglmx 1:0ab26889af9b 166 unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function
mglmx 1:0ab26889af9b 167 unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight
mglmx 1:0ab26889af9b 168 //
mglmx 1:0ab26889af9b 169 //Basic DCC Demo Commands
mglmx 1:0ab26889af9b 170 DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full
mglmx 1:0ab26889af9b 171 DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light
mglmx 1:0ab26889af9b 172 DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again
mglmx 2:f580707c44fa 173 led2 = 1;
carlosperales95 14:7bb998edd819 174
carlosperales95 19:ff21ba3a4dc5 175 //Command variables for Switches using DCC
carlosperales95 19:ff21ba3a4dc5 176
carlosperales95 19:ff21ba3a4dc5 177 unsigned int SWBaddress = 0x06; //address for switch box
carlosperales95 19:ff21ba3a4dc5 178
carlosperales95 19:ff21ba3a4dc5 179 ////100DDDDD where DDDDD
carlosperales95 19:ff21ba3a4dc5 180 ///00001 to flip the first switch SW1 (F1 active)
carlosperales95 19:ff21ba3a4dc5 181 ///00010 to flip the second switch SW2 (F2 active)
carlosperales95 19:ff21ba3a4dc5 182 ///00100 to flip the third switch SW3 (F3 active)
carlosperales95 19:ff21ba3a4dc5 183 ///01000 to flip the fourth switch SW4 (F4 active)
carlosperales95 19:ff21ba3a4dc5 184 //example - 111111 0 00000101 0 10000000 0 10000101 1 - idle
carlosperales95 19:ff21ba3a4dc5 185
carlosperales95 19:ff21ba3a4dc5 186 unsigned int SWBflip_idle = 0x80; //No switches -- idle
carlosperales95 19:ff21ba3a4dc5 187 unsigned int SWBflip_f1 = 0x81; //FLIP 1
carlosperales95 19:ff21ba3a4dc5 188 unsigned int SWBflip_f2 = 0x82;
carlosperales95 19:ff21ba3a4dc5 189 unsigned int SWBflip_f3 = 0x84;
carlosperales95 19:ff21ba3a4dc5 190 unsigned int SWBflip_f4 = 0x90;
carlosperales95 19:ff21ba3a4dc5 191
carlosperales95 19:ff21ba3a4dc5 192 //Send using DCC_send_command()
carlosperales95 19:ff21ba3a4dc5 193
carlosperales95 19:ff21ba3a4dc5 194
carlosperales95 14:7bb998edd819 195 //Demo for stopping at the station
mglmx 1:0ab26889af9b 196 while(1) {
mglmx 4:50879dfb82d5 197
mglmx 4:50879dfb82d5 198 if(d21 == 1 || d22 == 1 || d23 == 1){
carlosperales95 14:7bb998edd819 199
mglmx 4:50879dfb82d5 200 lcd.cls();
mglmx 4:50879dfb82d5 201 lcd.printf("Choo Choo station");
carlosperales95 14:7bb998edd819 202 DCC_send_command(DCCaddress,DCCinst_stop,400); // Stop train address 3
mglmx 4:50879dfb82d5 203
mglmx 4:50879dfb82d5 204 }else{
carlosperales95 14:7bb998edd819 205 DCC_send_command(DCCaddress,DCCinst_forward,1); // Forward half speed train address 3
mglmx 4:50879dfb82d5 206 }
mglmx 4:50879dfb82d5 207
mglmx 4:50879dfb82d5 208
mglmx 4:50879dfb82d5 209 /*
mglmx 3:fe7010b693a0 210 float f = pot.read();
mglmx 3:fe7010b693a0 211 float vin = f * 3.3;
mglmx 3:fe7010b693a0 212
mglmx 2:f580707c44fa 213 led3 = 1;
mglmx 3:fe7010b693a0 214 if(switch1 == 1){
mglmx 3:fe7010b693a0 215 lcd.cls();
mglmx 3:fe7010b693a0 216 lcd.printf("Going forward");
mglmx 3:fe7010b693a0 217 greenled = 0;
mglmx 3:fe7010b693a0 218 redled = 1;
mglmx 3:fe7010b693a0 219 DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3
mglmx 3:fe7010b693a0 220 lcd.cls();
mglmx 3:fe7010b693a0 221
mglmx 3:fe7010b693a0 222 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 223 wait(0.2);
mglmx 3:fe7010b693a0 224
mglmx 3:fe7010b693a0 225 }else{
mglmx 3:fe7010b693a0 226 lcd.cls();
mglmx 3:fe7010b693a0 227 lcd.printf("Going backwards");
mglmx 3:fe7010b693a0 228 greenled = 1;
mglmx 3:fe7010b693a0 229 redled = 0;
mglmx 3:fe7010b693a0 230 DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3
mglmx 3:fe7010b693a0 231 lcd.cls();
mglmx 3:fe7010b693a0 232
mglmx 3:fe7010b693a0 233 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 234 wait(0.2);
mglmx 3:fe7010b693a0 235
mglmx 3:fe7010b693a0 236 }
mglmx 4:50879dfb82d5 237 */
mglmx 1:0ab26889af9b 238 }
mglmx 3:fe7010b693a0 239 }