Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Committer:
carlosperales95
Date:
Thu Jun 07 10:35:45 2018 +0000
Revision:
11:021210c59a95
Parent:
10:2088b1935a93
Child:
12:e914ca5cd44b
Separated file in 3 parts (declarations, functions and main program), added some comments about the components, added buzzer and pins - UPTODATE

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 7:e2b8461d4f05 20 //M0 - p13
carlosperales95 7:e2b8461d4f05 21 DigitalIn d21(p13);
carlosperales95 7:e2b8461d4f05 22 //M1 - p14
carlosperales95 7:e2b8461d4f05 23 DigitalIn d22(p14);
carlosperales95 7:e2b8461d4f05 24 //M2 - p15
carlosperales95 7:e2b8461d4f05 25 DigitalIn d23(p15);
carlosperales95 7:e2b8461d4f05 26 //p16
carlosperales95 7:e2b8461d4f05 27 ///p17
carlosperales95 11:021210c59a95 28 //BUZZER - p18
carlosperales95 11:021210c59a95 29 DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps
carlosperales95 7:e2b8461d4f05 30
carlosperales95 7:e2b8461d4f05 31 //POTENTIOMETER - p19
carlosperales95 11:021210c59a95 32 AnalogIn pot(p19); //Gives float value pot.read(). Convert to V with f*3.3
carlosperales95 7:e2b8461d4f05 33
carlosperales95 7:e2b8461d4f05 34 //DAT - p20
mglmx 3:fe7010b693a0 35 DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge
carlosperales95 7:e2b8461d4f05 36
carlosperales95 7:e2b8461d4f05 37 //LCD SCREEN - p21, p22, p23, p24, p25, p26
carlosperales95 11:021210c59a95 38 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 39
carlosperales95 7:e2b8461d4f05 40 ///p27
carlosperales95 7:e2b8461d4f05 41 ///p28
carlosperales95 7:e2b8461d4f05 42
carlosperales95 7:e2b8461d4f05 43 //LED1 - p29
mglmx 3:fe7010b693a0 44 DigitalOut redled(p29);
carlosperales95 7:e2b8461d4f05 45 //LED2 - p30
mglmx 3:fe7010b693a0 46 DigitalOut greenled(p30);
carlosperales95 7:e2b8461d4f05 47
mglmx 5:ce0f66ea12c5 48
carlosperales95 11:021210c59a95 49 //**************** FUNCTIONS FOR DENVER TRAIN ****************//
carlosperales95 10:2088b1935a93 50
mglmx 5:ce0f66ea12c5 51
mglmx 5:ce0f66ea12c5 52 void interrupted(){
mglmx 5:ce0f66ea12c5 53
mglmx 5:ce0f66ea12c5 54 }
mglmx 1:0ab26889af9b 55
mglmx 1:0ab26889af9b 56 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
mglmx 1:0ab26889af9b 57 {
mglmx 1:0ab26889af9b 58 unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
mglmx 1:0ab26889af9b 59 unsigned __int64 temp_command = 0x0000000000000000;
mglmx 1:0ab26889af9b 60 unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
mglmx 1:0ab26889af9b 61 unsigned int error = 0x00; //error byte
mglmx 1:0ab26889af9b 62 //calculate error detection byte with xor
mglmx 1:0ab26889af9b 63 error = address ^ inst;
mglmx 1:0ab26889af9b 64 //combine packet bits in basic DCC format
mglmx 1:0ab26889af9b 65 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
mglmx 1:0ab26889af9b 66 //printf("\n\r %llx \n\r",command);
mglmx 1:0ab26889af9b 67 int i=0;
mglmx 1:0ab26889af9b 68 //repeat DCC command lots of times
mglmx 1:0ab26889af9b 69 while(i < repeat_count) {
mglmx 1:0ab26889af9b 70 temp_command = command;
mglmx 1:0ab26889af9b 71 //loops through packet bits encoding and sending out digital pulses for a DCC command
mglmx 1:0ab26889af9b 72 for (int j=0; j<64; j++) {
mglmx 1:0ab26889af9b 73 if((temp_command&0x8000000000000000)==0) { //test packet bit
mglmx 1:0ab26889af9b 74 //send data for a "0" bit
mglmx 1:0ab26889af9b 75 Track=0;
mglmx 1:0ab26889af9b 76 wait_us(100);
mglmx 1:0ab26889af9b 77 Track=1;
mglmx 1:0ab26889af9b 78 wait_us(100);
mglmx 1:0ab26889af9b 79 //printf("0011");
mglmx 1:0ab26889af9b 80 } else {
mglmx 1:0ab26889af9b 81 //send data for a "1"bit
mglmx 1:0ab26889af9b 82 Track=0;
mglmx 1:0ab26889af9b 83 wait_us(58);
mglmx 1:0ab26889af9b 84 Track=1;
mglmx 1:0ab26889af9b 85 wait_us(58);
mglmx 1:0ab26889af9b 86 //printf("01");
mglmx 1:0ab26889af9b 87 }
mglmx 1:0ab26889af9b 88 // next bit in packet
mglmx 1:0ab26889af9b 89 temp_command = temp_command<<1;
mglmx 1:0ab26889af9b 90 }
mglmx 1:0ab26889af9b 91 i++;
mglmx 0:4d06a6a8e785 92 }
mglmx 0:4d06a6a8e785 93 }
carlosperales95 11:021210c59a95 94
carlosperales95 11:021210c59a95 95
carlosperales95 11:021210c59a95 96 //**************** MAIN PROGRAM FOR DENVER TRAIN ****************//
carlosperales95 11:021210c59a95 97
mglmx 1:0ab26889af9b 98 int main()
mglmx 1:0ab26889af9b 99 {
mglmx 2:f580707c44fa 100 led1 = 1;
mglmx 2:f580707c44fa 101 wait(0.5);
mglmx 2:f580707c44fa 102 led1 = 0;
mglmx 2:f580707c44fa 103 wait(0.5);
mglmx 2:f580707c44fa 104 led1 = 1;
mglmx 1:0ab26889af9b 105 //typical out of box default engine DCC address is 3 (at least for Bachmann trains)
mglmx 1:0ab26889af9b 106 //Note: A DCC controller can reprogram the address whenever needed
mglmx 1:0ab26889af9b 107 unsigned int DCCaddress = 0x01;
mglmx 1:0ab26889af9b 108 //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf
mglmx 1:0ab26889af9b 109 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
mglmx 1:0ab26889af9b 110 unsigned int DCCinst_forward = 0x68; //forward half speed
mglmx 1:0ab26889af9b 111 unsigned int DCCinst_reverse = 0x48; //reverse half speed
mglmx 4:50879dfb82d5 112 unsigned int DCCinst_stop = 0x50;
mglmx 1:0ab26889af9b 113 //100DDDDD for basic headlight functions
mglmx 1:0ab26889af9b 114 unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function
mglmx 1:0ab26889af9b 115 unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight
mglmx 1:0ab26889af9b 116 //
mglmx 1:0ab26889af9b 117 //Basic DCC Demo Commands
mglmx 1:0ab26889af9b 118 DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full
mglmx 1:0ab26889af9b 119 DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light
mglmx 1:0ab26889af9b 120 DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again
mglmx 2:f580707c44fa 121 led2 = 1;
mglmx 1:0ab26889af9b 122 while(1) {
mglmx 4:50879dfb82d5 123
mglmx 4:50879dfb82d5 124
mglmx 4:50879dfb82d5 125
mglmx 4:50879dfb82d5 126 if(d21 == 1 || d22 == 1 || d23 == 1){
mglmx 4:50879dfb82d5 127 lcd.cls();
mglmx 4:50879dfb82d5 128 lcd.printf("Choo Choo station");
mglmx 4:50879dfb82d5 129 DCC_send_command(DCCaddress,DCCinst_stop,400); // forward half speed train address 3
mglmx 4:50879dfb82d5 130
mglmx 4:50879dfb82d5 131 }else{
mglmx 4:50879dfb82d5 132 DCC_send_command(DCCaddress,DCCinst_forward,1); // forward half speed train address 3
mglmx 4:50879dfb82d5 133 }
mglmx 4:50879dfb82d5 134
mglmx 4:50879dfb82d5 135
mglmx 4:50879dfb82d5 136 /*
mglmx 3:fe7010b693a0 137 float f = pot.read();
mglmx 3:fe7010b693a0 138 float vin = f * 3.3;
mglmx 3:fe7010b693a0 139
mglmx 2:f580707c44fa 140 led3 = 1;
mglmx 3:fe7010b693a0 141 if(switch1 == 1){
mglmx 3:fe7010b693a0 142 lcd.cls();
mglmx 3:fe7010b693a0 143 lcd.printf("Going forward");
mglmx 3:fe7010b693a0 144 greenled = 0;
mglmx 3:fe7010b693a0 145 redled = 1;
mglmx 3:fe7010b693a0 146 DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3
mglmx 3:fe7010b693a0 147 lcd.cls();
mglmx 3:fe7010b693a0 148
mglmx 3:fe7010b693a0 149 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 150 wait(0.2);
mglmx 3:fe7010b693a0 151
mglmx 3:fe7010b693a0 152 }else{
mglmx 3:fe7010b693a0 153 lcd.cls();
mglmx 3:fe7010b693a0 154 lcd.printf("Going backwards");
mglmx 3:fe7010b693a0 155 greenled = 1;
mglmx 3:fe7010b693a0 156 redled = 0;
mglmx 3:fe7010b693a0 157 DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3
mglmx 3:fe7010b693a0 158 lcd.cls();
mglmx 3:fe7010b693a0 159
mglmx 3:fe7010b693a0 160 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 161 wait(0.2);
mglmx 3:fe7010b693a0 162
mglmx 3:fe7010b693a0 163 }
mglmx 4:50879dfb82d5 164 */
mglmx 1:0ab26889af9b 165 }
mglmx 3:fe7010b693a0 166 }