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@3:fe7010b693a0, 2018-06-06 (annotated)
- Committer:
- mglmx
- Date:
- Wed Jun 06 14:06:57 2018 +0000
- Revision:
- 3:fe7010b693a0
- Parent:
- 2:f580707c44fa
- Child:
- 4:50879dfb82d5
- Child:
- 6:9789ad5f8b7f
LCD and potentiometer working.
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 | 1:0ab26889af9b | 14 | |
mglmx | 1:0ab26889af9b | 15 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
mglmx | 1:0ab26889af9b | 16 | { |
mglmx | 1:0ab26889af9b | 17 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 18 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 19 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 20 | unsigned int error = 0x00; //error byte |
mglmx | 1:0ab26889af9b | 21 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 22 | error = address ^ inst; |
mglmx | 1:0ab26889af9b | 23 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 24 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 25 | //printf("\n\r %llx \n\r",command); |
mglmx | 1:0ab26889af9b | 26 | int i=0; |
mglmx | 1:0ab26889af9b | 27 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 28 | while(i < repeat_count) { |
mglmx | 1:0ab26889af9b | 29 | temp_command = command; |
mglmx | 1:0ab26889af9b | 30 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 31 | for (int j=0; j<64; j++) { |
mglmx | 1:0ab26889af9b | 32 | if((temp_command&0x8000000000000000)==0) { //test packet bit |
mglmx | 1:0ab26889af9b | 33 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 34 | Track=0; |
mglmx | 1:0ab26889af9b | 35 | wait_us(100); |
mglmx | 1:0ab26889af9b | 36 | Track=1; |
mglmx | 1:0ab26889af9b | 37 | wait_us(100); |
mglmx | 1:0ab26889af9b | 38 | //printf("0011"); |
mglmx | 1:0ab26889af9b | 39 | } else { |
mglmx | 1:0ab26889af9b | 40 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 41 | Track=0; |
mglmx | 1:0ab26889af9b | 42 | wait_us(58); |
mglmx | 1:0ab26889af9b | 43 | Track=1; |
mglmx | 1:0ab26889af9b | 44 | wait_us(58); |
mglmx | 1:0ab26889af9b | 45 | //printf("01"); |
mglmx | 1:0ab26889af9b | 46 | } |
mglmx | 1:0ab26889af9b | 47 | // next bit in packet |
mglmx | 1:0ab26889af9b | 48 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 49 | } |
mglmx | 1:0ab26889af9b | 50 | i++; |
mglmx | 0:4d06a6a8e785 | 51 | } |
mglmx | 0:4d06a6a8e785 | 52 | } |
mglmx | 1:0ab26889af9b | 53 | //DCC train demo turns on headlight, dims headlight, and moves back and forth at half speed forever |
mglmx | 1:0ab26889af9b | 54 | int main() |
mglmx | 1:0ab26889af9b | 55 | { |
mglmx | 2:f580707c44fa | 56 | led1 = 1; |
mglmx | 2:f580707c44fa | 57 | wait(0.5); |
mglmx | 2:f580707c44fa | 58 | led1 = 0; |
mglmx | 2:f580707c44fa | 59 | wait(0.5); |
mglmx | 2:f580707c44fa | 60 | led1 = 1; |
mglmx | 1:0ab26889af9b | 61 | //typical out of box default engine DCC address is 3 (at least for Bachmann trains) |
mglmx | 1:0ab26889af9b | 62 | //Note: A DCC controller can reprogram the address whenever needed |
mglmx | 1:0ab26889af9b | 63 | unsigned int DCCaddress = 0x01; |
mglmx | 1:0ab26889af9b | 64 | //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf |
mglmx | 1:0ab26889af9b | 65 | //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB |
mglmx | 1:0ab26889af9b | 66 | unsigned int DCCinst_forward = 0x68; //forward half speed |
mglmx | 1:0ab26889af9b | 67 | unsigned int DCCinst_reverse = 0x48; //reverse half speed |
mglmx | 1:0ab26889af9b | 68 | //100DDDDD for basic headlight functions |
mglmx | 1:0ab26889af9b | 69 | unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function |
mglmx | 1:0ab26889af9b | 70 | unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight |
mglmx | 1:0ab26889af9b | 71 | // |
mglmx | 1:0ab26889af9b | 72 | //Basic DCC Demo Commands |
mglmx | 1:0ab26889af9b | 73 | DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full |
mglmx | 1:0ab26889af9b | 74 | DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light |
mglmx | 1:0ab26889af9b | 75 | DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again |
mglmx | 2:f580707c44fa | 76 | led2 = 1; |
mglmx | 1:0ab26889af9b | 77 | while(1) { |
mglmx | 3:fe7010b693a0 | 78 | float f = pot.read(); |
mglmx | 3:fe7010b693a0 | 79 | float vin = f * 3.3; |
mglmx | 3:fe7010b693a0 | 80 | |
mglmx | 2:f580707c44fa | 81 | led3 = 1; |
mglmx | 3:fe7010b693a0 | 82 | if(switch1 == 1){ |
mglmx | 3:fe7010b693a0 | 83 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 84 | lcd.printf("Going forward"); |
mglmx | 3:fe7010b693a0 | 85 | greenled = 0; |
mglmx | 3:fe7010b693a0 | 86 | redled = 1; |
mglmx | 3:fe7010b693a0 | 87 | DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3 |
mglmx | 3:fe7010b693a0 | 88 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 89 | |
mglmx | 3:fe7010b693a0 | 90 | lcd.printf("vin: %.4f",vin); |
mglmx | 3:fe7010b693a0 | 91 | wait(0.2); |
mglmx | 3:fe7010b693a0 | 92 | |
mglmx | 3:fe7010b693a0 | 93 | }else{ |
mglmx | 3:fe7010b693a0 | 94 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 95 | lcd.printf("Going backwards"); |
mglmx | 3:fe7010b693a0 | 96 | greenled = 1; |
mglmx | 3:fe7010b693a0 | 97 | redled = 0; |
mglmx | 3:fe7010b693a0 | 98 | DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3 |
mglmx | 3:fe7010b693a0 | 99 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 100 | |
mglmx | 3:fe7010b693a0 | 101 | lcd.printf("vin: %.4f",vin); |
mglmx | 3:fe7010b693a0 | 102 | wait(0.2); |
mglmx | 3:fe7010b693a0 | 103 | |
mglmx | 3:fe7010b693a0 | 104 | } |
mglmx | 3:fe7010b693a0 | 105 | |
mglmx | 1:0ab26889af9b | 106 | } |
mglmx | 3:fe7010b693a0 | 107 | } |