Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@5:ce0f66ea12c5, 2018-06-07 (annotated)
- Committer:
- mglmx
- Date:
- Thu Jun 07 10:00:42 2018 +0000
- Revision:
- 5:ce0f66ea12c5
- Parent:
- 4:50879dfb82d5
- Child:
- 7:e2b8461d4f05
Stopping at train station;
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:ce0f66ea12c5 | 17 | |
mglmx | 5:ce0f66ea12c5 | 18 | //Rail sensors |
mglmx | 5:ce0f66ea12c5 | 19 | InterruptIn sensors0(p9); |
mglmx | 5:ce0f66ea12c5 | 20 | InterruptIn sensors1(p10); |
mglmx | 5:ce0f66ea12c5 | 21 | |
mglmx | 5:ce0f66ea12c5 | 22 | void interrupted(){ |
mglmx | 5:ce0f66ea12c5 | 23 | |
mglmx | 5:ce0f66ea12c5 | 24 | } |
mglmx | 1:0ab26889af9b | 25 | |
mglmx | 1:0ab26889af9b | 26 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
mglmx | 1:0ab26889af9b | 27 | { |
mglmx | 1:0ab26889af9b | 28 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 29 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 30 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 31 | unsigned int error = 0x00; //error byte |
mglmx | 1:0ab26889af9b | 32 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 33 | error = address ^ inst; |
mglmx | 1:0ab26889af9b | 34 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 35 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 36 | //printf("\n\r %llx \n\r",command); |
mglmx | 1:0ab26889af9b | 37 | int i=0; |
mglmx | 1:0ab26889af9b | 38 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 39 | while(i < repeat_count) { |
mglmx | 1:0ab26889af9b | 40 | temp_command = command; |
mglmx | 1:0ab26889af9b | 41 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 42 | for (int j=0; j<64; j++) { |
mglmx | 1:0ab26889af9b | 43 | if((temp_command&0x8000000000000000)==0) { //test packet bit |
mglmx | 1:0ab26889af9b | 44 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 45 | Track=0; |
mglmx | 1:0ab26889af9b | 46 | wait_us(100); |
mglmx | 1:0ab26889af9b | 47 | Track=1; |
mglmx | 1:0ab26889af9b | 48 | wait_us(100); |
mglmx | 1:0ab26889af9b | 49 | //printf("0011"); |
mglmx | 1:0ab26889af9b | 50 | } else { |
mglmx | 1:0ab26889af9b | 51 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 52 | Track=0; |
mglmx | 1:0ab26889af9b | 53 | wait_us(58); |
mglmx | 1:0ab26889af9b | 54 | Track=1; |
mglmx | 1:0ab26889af9b | 55 | wait_us(58); |
mglmx | 1:0ab26889af9b | 56 | //printf("01"); |
mglmx | 1:0ab26889af9b | 57 | } |
mglmx | 1:0ab26889af9b | 58 | // next bit in packet |
mglmx | 1:0ab26889af9b | 59 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 60 | } |
mglmx | 1:0ab26889af9b | 61 | i++; |
mglmx | 0:4d06a6a8e785 | 62 | } |
mglmx | 0:4d06a6a8e785 | 63 | } |
mglmx | 1:0ab26889af9b | 64 | //DCC train demo turns on headlight, dims headlight, and moves back and forth at half speed forever |
mglmx | 1:0ab26889af9b | 65 | int main() |
mglmx | 1:0ab26889af9b | 66 | { |
mglmx | 2:f580707c44fa | 67 | led1 = 1; |
mglmx | 2:f580707c44fa | 68 | wait(0.5); |
mglmx | 2:f580707c44fa | 69 | led1 = 0; |
mglmx | 2:f580707c44fa | 70 | wait(0.5); |
mglmx | 2:f580707c44fa | 71 | led1 = 1; |
mglmx | 1:0ab26889af9b | 72 | //typical out of box default engine DCC address is 3 (at least for Bachmann trains) |
mglmx | 1:0ab26889af9b | 73 | //Note: A DCC controller can reprogram the address whenever needed |
mglmx | 1:0ab26889af9b | 74 | unsigned int DCCaddress = 0x01; |
mglmx | 1:0ab26889af9b | 75 | //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf |
mglmx | 1:0ab26889af9b | 76 | //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB |
mglmx | 1:0ab26889af9b | 77 | unsigned int DCCinst_forward = 0x68; //forward half speed |
mglmx | 1:0ab26889af9b | 78 | unsigned int DCCinst_reverse = 0x48; //reverse half speed |
mglmx | 4:50879dfb82d5 | 79 | unsigned int DCCinst_stop = 0x50; |
mglmx | 1:0ab26889af9b | 80 | //100DDDDD for basic headlight functions |
mglmx | 1:0ab26889af9b | 81 | unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function |
mglmx | 1:0ab26889af9b | 82 | unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight |
mglmx | 1:0ab26889af9b | 83 | // |
mglmx | 1:0ab26889af9b | 84 | //Basic DCC Demo Commands |
mglmx | 1:0ab26889af9b | 85 | DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full |
mglmx | 1:0ab26889af9b | 86 | DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light |
mglmx | 1:0ab26889af9b | 87 | DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again |
mglmx | 2:f580707c44fa | 88 | led2 = 1; |
mglmx | 1:0ab26889af9b | 89 | while(1) { |
mglmx | 4:50879dfb82d5 | 90 | |
mglmx | 4:50879dfb82d5 | 91 | |
mglmx | 4:50879dfb82d5 | 92 | |
mglmx | 4:50879dfb82d5 | 93 | if(d21 == 1 || d22 == 1 || d23 == 1){ |
mglmx | 4:50879dfb82d5 | 94 | lcd.cls(); |
mglmx | 4:50879dfb82d5 | 95 | lcd.printf("Choo Choo station"); |
mglmx | 4:50879dfb82d5 | 96 | DCC_send_command(DCCaddress,DCCinst_stop,400); // forward half speed train address 3 |
mglmx | 4:50879dfb82d5 | 97 | |
mglmx | 4:50879dfb82d5 | 98 | }else{ |
mglmx | 4:50879dfb82d5 | 99 | DCC_send_command(DCCaddress,DCCinst_forward,1); // forward half speed train address 3 |
mglmx | 4:50879dfb82d5 | 100 | } |
mglmx | 4:50879dfb82d5 | 101 | |
mglmx | 4:50879dfb82d5 | 102 | |
mglmx | 4:50879dfb82d5 | 103 | /* |
mglmx | 3:fe7010b693a0 | 104 | float f = pot.read(); |
mglmx | 3:fe7010b693a0 | 105 | float vin = f * 3.3; |
mglmx | 3:fe7010b693a0 | 106 | |
mglmx | 2:f580707c44fa | 107 | led3 = 1; |
mglmx | 3:fe7010b693a0 | 108 | if(switch1 == 1){ |
mglmx | 3:fe7010b693a0 | 109 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 110 | lcd.printf("Going forward"); |
mglmx | 3:fe7010b693a0 | 111 | greenled = 0; |
mglmx | 3:fe7010b693a0 | 112 | redled = 1; |
mglmx | 3:fe7010b693a0 | 113 | DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3 |
mglmx | 3:fe7010b693a0 | 114 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 115 | |
mglmx | 3:fe7010b693a0 | 116 | lcd.printf("vin: %.4f",vin); |
mglmx | 3:fe7010b693a0 | 117 | wait(0.2); |
mglmx | 3:fe7010b693a0 | 118 | |
mglmx | 3:fe7010b693a0 | 119 | }else{ |
mglmx | 3:fe7010b693a0 | 120 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 121 | lcd.printf("Going backwards"); |
mglmx | 3:fe7010b693a0 | 122 | greenled = 1; |
mglmx | 3:fe7010b693a0 | 123 | redled = 0; |
mglmx | 3:fe7010b693a0 | 124 | DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3 |
mglmx | 3:fe7010b693a0 | 125 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 126 | |
mglmx | 3:fe7010b693a0 | 127 | lcd.printf("vin: %.4f",vin); |
mglmx | 3:fe7010b693a0 | 128 | wait(0.2); |
mglmx | 3:fe7010b693a0 | 129 | |
mglmx | 3:fe7010b693a0 | 130 | } |
mglmx | 4:50879dfb82d5 | 131 | */ |
mglmx | 1:0ab26889af9b | 132 | } |
mglmx | 3:fe7010b693a0 | 133 | } |