![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
first draft
Dependencies: mbed
main.cpp@0:52e7cb6008d3, 2017-06-07 (annotated)
- Committer:
- rcwinder
- Date:
- Wed Jun 07 14:23:59 2017 +0000
- Revision:
- 0:52e7cb6008d3
Trein rijdt en stopt bij station
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rcwinder | 0:52e7cb6008d3 | 1 | #include "mbed.h" |
rcwinder | 0:52e7cb6008d3 | 2 | |
rcwinder | 0:52e7cb6008d3 | 3 | const unsigned __int64 FORWARD = 1; |
rcwinder | 0:52e7cb6008d3 | 4 | const unsigned __int64 BACKWARD = 0; |
rcwinder | 0:52e7cb6008d3 | 5 | const unsigned __int64 LIGHTON = 1; |
rcwinder | 0:52e7cb6008d3 | 6 | const unsigned __int64 LIGHTOFF = 0; |
rcwinder | 0:52e7cb6008d3 | 7 | const unsigned __int64 SLOW = 0x4; //0100 = step 5 |
rcwinder | 0:52e7cb6008d3 | 8 | const unsigned __int64 NORM = 0x9; //1001 = step 15 |
rcwinder | 0:52e7cb6008d3 | 9 | const unsigned __int64 FAST = 0xE; //1110 = step 25 |
rcwinder | 0:52e7cb6008d3 | 10 | const unsigned __int64 STOP = 0x1; //0001 = E-STOP |
rcwinder | 0:52e7cb6008d3 | 11 | |
rcwinder | 0:52e7cb6008d3 | 12 | DigitalOut Track(p20); |
rcwinder | 0:52e7cb6008d3 | 13 | DigitalIn sign2(p14); |
rcwinder | 0:52e7cb6008d3 | 14 | DigitalIn sign21(p15); |
rcwinder | 0:52e7cb6008d3 | 15 | DigitalIn sign22(p13); |
rcwinder | 0:52e7cb6008d3 | 16 | DigitalOut buzz(p21); |
rcwinder | 0:52e7cb6008d3 | 17 | |
rcwinder | 0:52e7cb6008d3 | 18 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count){ |
rcwinder | 0:52e7cb6008d3 | 19 | |
rcwinder | 0:52e7cb6008d3 | 20 | unsigned __int64 command = 0x0000000000000000; // this is a 64-bit integer type, due to the packet size |
rcwinder | 0:52e7cb6008d3 | 21 | unsigned __int64 temp_command = 0x0000000000000000; |
rcwinder | 0:52e7cb6008d3 | 22 | unsigned __int64 prefix = 0x3FFF; // 14 1 bits as in the preamble of the data packet |
rcwinder | 0:52e7cb6008d3 | 23 | unsigned int error = 0x00; // error byte |
rcwinder | 0:52e7cb6008d3 | 24 | //calculate error detection byte with xor |
rcwinder | 0:52e7cb6008d3 | 25 | error = address ^ inst; |
rcwinder | 0:52e7cb6008d3 | 26 | // combine packet bits in basic DCC format |
rcwinder | 0:52e7cb6008d3 | 27 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
rcwinder | 0:52e7cb6008d3 | 28 | |
rcwinder | 0:52e7cb6008d3 | 29 | int i = 0; |
rcwinder | 0:52e7cb6008d3 | 30 | |
rcwinder | 0:52e7cb6008d3 | 31 | // repeat DCC command lots of times |
rcwinder | 0:52e7cb6008d3 | 32 | while(i < repeat_count) { |
rcwinder | 0:52e7cb6008d3 | 33 | temp_command = command; |
rcwinder | 0:52e7cb6008d3 | 34 | |
rcwinder | 0:52e7cb6008d3 | 35 | //loops throught packet bits enconding and sending out digital pulses for a DCC command |
rcwinder | 0:52e7cb6008d3 | 36 | for (int j=0; j<64; j++) { |
rcwinder | 0:52e7cb6008d3 | 37 | if((temp_command&0x8000000000000000)==0) { //test packet bit |
rcwinder | 0:52e7cb6008d3 | 38 | |
rcwinder | 0:52e7cb6008d3 | 39 | Track=0; |
rcwinder | 0:52e7cb6008d3 | 40 | wait_us(100); |
rcwinder | 0:52e7cb6008d3 | 41 | Track=1; |
rcwinder | 0:52e7cb6008d3 | 42 | wait_us(100); |
rcwinder | 0:52e7cb6008d3 | 43 | |
rcwinder | 0:52e7cb6008d3 | 44 | } else { |
rcwinder | 0:52e7cb6008d3 | 45 | |
rcwinder | 0:52e7cb6008d3 | 46 | Track=0; |
rcwinder | 0:52e7cb6008d3 | 47 | wait_us(58); |
rcwinder | 0:52e7cb6008d3 | 48 | Track=1; |
rcwinder | 0:52e7cb6008d3 | 49 | wait_us(58); |
rcwinder | 0:52e7cb6008d3 | 50 | |
rcwinder | 0:52e7cb6008d3 | 51 | } |
rcwinder | 0:52e7cb6008d3 | 52 | |
rcwinder | 0:52e7cb6008d3 | 53 | temp_command = temp_command<<1; |
rcwinder | 0:52e7cb6008d3 | 54 | } |
rcwinder | 0:52e7cb6008d3 | 55 | i++; |
rcwinder | 0:52e7cb6008d3 | 56 | } |
rcwinder | 0:52e7cb6008d3 | 57 | } |
rcwinder | 0:52e7cb6008d3 | 58 | |
rcwinder | 0:52e7cb6008d3 | 59 | unsigned __int64 create_instruction(){ |
rcwinder | 0:52e7cb6008d3 | 60 | unsigned __int64 instruction = 0x0; |
rcwinder | 0:52e7cb6008d3 | 61 | unsigned __int64 direction = FORWARD; |
rcwinder | 0:52e7cb6008d3 | 62 | unsigned __int64 lights = LIGHTON; |
rcwinder | 0:52e7cb6008d3 | 63 | unsigned __int64 speed = NORM; |
rcwinder | 0:52e7cb6008d3 | 64 | |
rcwinder | 0:52e7cb6008d3 | 65 | return instruction = (0x01<<6)|(direction<<5)|(lights<<4)|speed; |
rcwinder | 0:52e7cb6008d3 | 66 | } |
rcwinder | 0:52e7cb6008d3 | 67 | |
rcwinder | 0:52e7cb6008d3 | 68 | int main(){ |
rcwinder | 0:52e7cb6008d3 | 69 | unsigned __int64 train1 = 0x1; |
rcwinder | 0:52e7cb6008d3 | 70 | unsigned __int64 train3 = 0x3; |
rcwinder | 0:52e7cb6008d3 | 71 | unsigned __int64 def_repeat_count = 2; |
rcwinder | 0:52e7cb6008d3 | 72 | unsigned __int64 instruction = 0x0; |
rcwinder | 0:52e7cb6008d3 | 73 | buzz = 0; |
rcwinder | 0:52e7cb6008d3 | 74 | |
rcwinder | 0:52e7cb6008d3 | 75 | while(1){ |
rcwinder | 0:52e7cb6008d3 | 76 | if(sign21){ |
rcwinder | 0:52e7cb6008d3 | 77 | instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTOFF<<4)|SLOW; |
rcwinder | 0:52e7cb6008d3 | 78 | buzz = 1; |
rcwinder | 0:52e7cb6008d3 | 79 | DCC_send_command(train3, instruction, def_repeat_count); |
rcwinder | 0:52e7cb6008d3 | 80 | } else if (sign2){ |
rcwinder | 0:52e7cb6008d3 | 81 | instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTOFF<<4)|STOP; |
rcwinder | 0:52e7cb6008d3 | 82 | buzz = 1; |
rcwinder | 0:52e7cb6008d3 | 83 | DCC_send_command(train3, instruction, def_repeat_count); |
rcwinder | 0:52e7cb6008d3 | 84 | wait(3); |
rcwinder | 0:52e7cb6008d3 | 85 | |
rcwinder | 0:52e7cb6008d3 | 86 | instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTOFF<<4)|SLOW; |
rcwinder | 0:52e7cb6008d3 | 87 | DCC_send_command(train3, instruction, def_repeat_count*20); |
rcwinder | 0:52e7cb6008d3 | 88 | |
rcwinder | 0:52e7cb6008d3 | 89 | } else if(sign22){ |
rcwinder | 0:52e7cb6008d3 | 90 | instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTOFF<<4)|SLOW; |
rcwinder | 0:52e7cb6008d3 | 91 | buzz = 1; |
rcwinder | 0:52e7cb6008d3 | 92 | DCC_send_command(train3, instruction, def_repeat_count); |
rcwinder | 0:52e7cb6008d3 | 93 | } else { |
rcwinder | 0:52e7cb6008d3 | 94 | instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|FAST; |
rcwinder | 0:52e7cb6008d3 | 95 | buzz = 0; |
rcwinder | 0:52e7cb6008d3 | 96 | DCC_send_command(train3, instruction, def_repeat_count); |
rcwinder | 0:52e7cb6008d3 | 97 | } |
rcwinder | 0:52e7cb6008d3 | 98 | } |
rcwinder | 0:52e7cb6008d3 | 99 | } |