Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Committer:
mglmx
Date:
Thu Jun 07 09:24:20 2018 +0000
Revision:
4:50879dfb82d5
Parent:
3:fe7010b693a0
Child:
5:ce0f66ea12c5
Train stopping at the train station.

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
mglmx 3:fe7010b693a0 5 DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge
mglmx 2:f580707c44fa 6 DigitalOut led1(LED1);
mglmx 2:f580707c44fa 7 DigitalOut led2(LED2);
mglmx 2:f580707c44fa 8 DigitalOut led3(LED3);
mglmx 3:fe7010b693a0 9 DigitalOut redled(p29);
mglmx 3:fe7010b693a0 10 DigitalOut greenled(p30);
mglmx 3:fe7010b693a0 11 DigitalIn switch1(p9);
mglmx 3:fe7010b693a0 12 TextLCD lcd(p22,p21,p23,p24,p25,p26);
mglmx 3:fe7010b693a0 13 AnalogIn pot(p19);
mglmx 4:50879dfb82d5 14 DigitalIn d21(p13);
mglmx 4:50879dfb82d5 15 DigitalIn d22(p14);
mglmx 4:50879dfb82d5 16 DigitalIn d23(p15);
mglmx 1:0ab26889af9b 17
mglmx 1:0ab26889af9b 18 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
mglmx 1:0ab26889af9b 19 {
mglmx 1:0ab26889af9b 20 unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
mglmx 1:0ab26889af9b 21 unsigned __int64 temp_command = 0x0000000000000000;
mglmx 1:0ab26889af9b 22 unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
mglmx 1:0ab26889af9b 23 unsigned int error = 0x00; //error byte
mglmx 1:0ab26889af9b 24 //calculate error detection byte with xor
mglmx 1:0ab26889af9b 25 error = address ^ inst;
mglmx 1:0ab26889af9b 26 //combine packet bits in basic DCC format
mglmx 1:0ab26889af9b 27 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
mglmx 1:0ab26889af9b 28 //printf("\n\r %llx \n\r",command);
mglmx 1:0ab26889af9b 29 int i=0;
mglmx 1:0ab26889af9b 30 //repeat DCC command lots of times
mglmx 1:0ab26889af9b 31 while(i < repeat_count) {
mglmx 1:0ab26889af9b 32 temp_command = command;
mglmx 1:0ab26889af9b 33 //loops through packet bits encoding and sending out digital pulses for a DCC command
mglmx 1:0ab26889af9b 34 for (int j=0; j<64; j++) {
mglmx 1:0ab26889af9b 35 if((temp_command&0x8000000000000000)==0) { //test packet bit
mglmx 1:0ab26889af9b 36 //send data for a "0" bit
mglmx 1:0ab26889af9b 37 Track=0;
mglmx 1:0ab26889af9b 38 wait_us(100);
mglmx 1:0ab26889af9b 39 Track=1;
mglmx 1:0ab26889af9b 40 wait_us(100);
mglmx 1:0ab26889af9b 41 //printf("0011");
mglmx 1:0ab26889af9b 42 } else {
mglmx 1:0ab26889af9b 43 //send data for a "1"bit
mglmx 1:0ab26889af9b 44 Track=0;
mglmx 1:0ab26889af9b 45 wait_us(58);
mglmx 1:0ab26889af9b 46 Track=1;
mglmx 1:0ab26889af9b 47 wait_us(58);
mglmx 1:0ab26889af9b 48 //printf("01");
mglmx 1:0ab26889af9b 49 }
mglmx 1:0ab26889af9b 50 // next bit in packet
mglmx 1:0ab26889af9b 51 temp_command = temp_command<<1;
mglmx 1:0ab26889af9b 52 }
mglmx 1:0ab26889af9b 53 i++;
mglmx 0:4d06a6a8e785 54 }
mglmx 0:4d06a6a8e785 55 }
mglmx 1:0ab26889af9b 56 //DCC train demo turns on headlight, dims headlight, and moves back and forth at half speed forever
mglmx 1:0ab26889af9b 57 int main()
mglmx 1:0ab26889af9b 58 {
mglmx 2:f580707c44fa 59 led1 = 1;
mglmx 2:f580707c44fa 60 wait(0.5);
mglmx 2:f580707c44fa 61 led1 = 0;
mglmx 2:f580707c44fa 62 wait(0.5);
mglmx 2:f580707c44fa 63 led1 = 1;
mglmx 1:0ab26889af9b 64 //typical out of box default engine DCC address is 3 (at least for Bachmann trains)
mglmx 1:0ab26889af9b 65 //Note: A DCC controller can reprogram the address whenever needed
mglmx 1:0ab26889af9b 66 unsigned int DCCaddress = 0x01;
mglmx 1:0ab26889af9b 67 //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf
mglmx 1:0ab26889af9b 68 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
mglmx 1:0ab26889af9b 69 unsigned int DCCinst_forward = 0x68; //forward half speed
mglmx 1:0ab26889af9b 70 unsigned int DCCinst_reverse = 0x48; //reverse half speed
mglmx 4:50879dfb82d5 71 unsigned int DCCinst_stop = 0x50;
mglmx 1:0ab26889af9b 72 //100DDDDD for basic headlight functions
mglmx 1:0ab26889af9b 73 unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function
mglmx 1:0ab26889af9b 74 unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight
mglmx 1:0ab26889af9b 75 //
mglmx 1:0ab26889af9b 76 //Basic DCC Demo Commands
mglmx 1:0ab26889af9b 77 DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full
mglmx 1:0ab26889af9b 78 DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light
mglmx 1:0ab26889af9b 79 DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again
mglmx 2:f580707c44fa 80 led2 = 1;
mglmx 1:0ab26889af9b 81 while(1) {
mglmx 4:50879dfb82d5 82
mglmx 4:50879dfb82d5 83
mglmx 4:50879dfb82d5 84
mglmx 4:50879dfb82d5 85 if(d21 == 1 || d22 == 1 || d23 == 1){
mglmx 4:50879dfb82d5 86 lcd.cls();
mglmx 4:50879dfb82d5 87 lcd.printf("Choo Choo station");
mglmx 4:50879dfb82d5 88 DCC_send_command(DCCaddress,DCCinst_stop,400); // forward half speed train address 3
mglmx 4:50879dfb82d5 89
mglmx 4:50879dfb82d5 90 }else{
mglmx 4:50879dfb82d5 91 DCC_send_command(DCCaddress,DCCinst_forward,1); // forward half speed train address 3
mglmx 4:50879dfb82d5 92 }
mglmx 4:50879dfb82d5 93
mglmx 4:50879dfb82d5 94
mglmx 4:50879dfb82d5 95 /*
mglmx 3:fe7010b693a0 96 float f = pot.read();
mglmx 3:fe7010b693a0 97 float vin = f * 3.3;
mglmx 3:fe7010b693a0 98
mglmx 2:f580707c44fa 99 led3 = 1;
mglmx 3:fe7010b693a0 100 if(switch1 == 1){
mglmx 3:fe7010b693a0 101 lcd.cls();
mglmx 3:fe7010b693a0 102 lcd.printf("Going forward");
mglmx 3:fe7010b693a0 103 greenled = 0;
mglmx 3:fe7010b693a0 104 redled = 1;
mglmx 3:fe7010b693a0 105 DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3
mglmx 3:fe7010b693a0 106 lcd.cls();
mglmx 3:fe7010b693a0 107
mglmx 3:fe7010b693a0 108 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 109 wait(0.2);
mglmx 3:fe7010b693a0 110
mglmx 3:fe7010b693a0 111 }else{
mglmx 3:fe7010b693a0 112 lcd.cls();
mglmx 3:fe7010b693a0 113 lcd.printf("Going backwards");
mglmx 3:fe7010b693a0 114 greenled = 1;
mglmx 3:fe7010b693a0 115 redled = 0;
mglmx 3:fe7010b693a0 116 DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3
mglmx 3:fe7010b693a0 117 lcd.cls();
mglmx 3:fe7010b693a0 118
mglmx 3:fe7010b693a0 119 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 120 wait(0.2);
mglmx 3:fe7010b693a0 121
mglmx 3:fe7010b693a0 122 }
mglmx 4:50879dfb82d5 123 */
mglmx 1:0ab26889af9b 124 }
mglmx 3:fe7010b693a0 125 }