Jasmine Karlsson
/
train_rail
New project
main.cpp@17:b7b5f40cebff, 2018-06-08 (annotated)
- Committer:
- jasminealice
- Date:
- Fri Jun 08 14:12:49 2018 +0000
- Revision:
- 17:b7b5f40cebff
- Parent:
- 16:55c3c5727f14
- Child:
- 18:f5824ba95892
detect switches and turn LEDs on
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jasminealice | 0:57cbecaac770 | 1 | #include "mbed.h" |
Kwame |
2:9bb410612929 | 2 | #include "TextLCD.h" |
KwamsC | 14:a5fd98a957e6 | 3 | #include "mcp23017.h" |
jasminealice | 15:8d96f7a06103 | 4 | #include "train.h" |
KwamsC | 10:cda57976225f | 5 | |
jasminealice | 16:55c3c5727f14 | 6 | //Board 1 |
KwamsC | 10:cda57976225f | 7 | |
jasminealice | 16:55c3c5727f14 | 8 | /*---------------------------------------------------------------------------- |
jasminealice | 16:55c3c5727f14 | 9 | Pin definitions |
jasminealice | 16:55c3c5727f14 | 10 | *----------------------------------------------------------------------------*/ |
jasminealice | 17:b7b5f40cebff | 11 | DigitalOut Track(p20); // train track |
jasminealice | 17:b7b5f40cebff | 12 | |
jasminealice | 17:b7b5f40cebff | 13 | DigitalOut myled1(LED1); |
KwamsC | 14:a5fd98a957e6 | 14 | DigitalOut myled2(LED2); |
KwamsC | 14:a5fd98a957e6 | 15 | DigitalOut myled3(LED3); |
KwamsC | 14:a5fd98a957e6 | 16 | DigitalOut myled4(LED4); |
jasminealice | 17:b7b5f40cebff | 17 | DigitalOut externalLed1(p15); |
jasminealice | 17:b7b5f40cebff | 18 | DigitalOut externalLed2(p16); |
jasminealice | 17:b7b5f40cebff | 19 | DigitalOut externalLed3(p17); |
jasminealice | 17:b7b5f40cebff | 20 | DigitalOut externalLed4(p18); |
KwamsC | 14:a5fd98a957e6 | 21 | |
KwamsC | 14:a5fd98a957e6 | 22 | TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd |
KwamsC | 14:a5fd98a957e6 | 23 | |
jasminealice | 16:55c3c5727f14 | 24 | //AnalogIn Ain(p20); // pot. met. |
KwamsC | 14:a5fd98a957e6 | 25 | /* Train detectors d2, d21, d22 (trainstation) */ |
jasminealice | 16:55c3c5727f14 | 26 | // detect_21(p17); |
jasminealice | 16:55c3c5727f14 | 27 | //DigitalIn detect_22(p16); |
jasminealice | 16:55c3c5727f14 | 28 | //DigitalIn detect_2(p15); |
KwamsC | 10:cda57976225f | 29 | |
jasminealice | 16:55c3c5727f14 | 30 | DigitalIn sw1(p29); |
jasminealice | 16:55c3c5727f14 | 31 | DigitalIn sw2(p30); |
jasminealice | 16:55c3c5727f14 | 32 | DigitalIn sw3(p11); |
jasminealice | 16:55c3c5727f14 | 33 | DigitalIn sw4(p12); |
jasminealice | 16:55c3c5727f14 | 34 | //InterruptIn sw1(p5); |
jasminealice | 17:b7b5f40cebff | 35 | InterruptIn inter0(p13); |
jasminealice | 17:b7b5f40cebff | 36 | InterruptIn inter1(p14); |
jasminealice | 17:b7b5f40cebff | 37 | |
jasminealice | 17:b7b5f40cebff | 38 | /*---------------------------------------------------------------------------- |
jasminealice | 17:b7b5f40cebff | 39 | Addresses |
jasminealice | 17:b7b5f40cebff | 40 | *----------------------------------------------------------------------------*/ |
jasminealice | 17:b7b5f40cebff | 41 | const unsigned int DCCaddress_darkRed = 0x01; |
jasminealice | 17:b7b5f40cebff | 42 | const unsigned int DCCaddress_lightRed = 0x03; |
jasminealice | 17:b7b5f40cebff | 43 | const unsigned int DCCaddress_switch = 0x06; |
KwamsC | 14:a5fd98a957e6 | 44 | |
KwamsC | 14:a5fd98a957e6 | 45 | /*---------------------------------------------------------------------------- |
KwamsC | 14:a5fd98a957e6 | 46 | Train movement |
KwamsC | 14:a5fd98a957e6 | 47 | *----------------------------------------------------------------------------*/ |
KwamsC | 14:a5fd98a957e6 | 48 | //move backwards/reverse |
jasminealice | 16:55c3c5727f14 | 49 | const unsigned int DCCinst_reverse = 0x48; //reverse speed |
KwamsC | 14:a5fd98a957e6 | 50 | |
jasminealice | 15:8d96f7a06103 | 51 | //speed dial forward |
jasminealice | 16:55c3c5727f14 | 52 | const unsigned int DCCinst_step2 = 0x72; //step 2 |
jasminealice | 16:55c3c5727f14 | 53 | const unsigned int DCCinst_step4 = 0x73; //step 4 |
jasminealice | 17:b7b5f40cebff | 54 | const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed |
jasminealice | 16:55c3c5727f14 | 55 | const unsigned int DCCinst_step13 = 0x78; //step 13 1/2 speed |
jasminealice | 16:55c3c5727f14 | 56 | const unsigned int DCCinst_step20 = 0x75; //step 20 3/4 speed |
jasminealice | 16:55c3c5727f14 | 57 | const unsigned int DCCinst_step28 = 0x7F; //step 28 Full speed |
jasminealice | 17:b7b5f40cebff | 58 | const unsigned int DCCinst_switch1 = 0x81; //Activate switch1 |
jasminealice | 17:b7b5f40cebff | 59 | const unsigned int DCCinst_switch2 = 0x82; //Activate switch2 |
jasminealice | 17:b7b5f40cebff | 60 | const unsigned int DCCinst_switch3 = 0x84; //Activate switch3 |
jasminealice | 17:b7b5f40cebff | 61 | const unsigned int DCCinst_switch4 = 0x88; //Activate switch4 |
jasminealice | 17:b7b5f40cebff | 62 | const unsigned int DCCinst_deactive_switch = 0x80; //Deactivate switches |
KwamsC | 14:a5fd98a957e6 | 63 | //stop |
jasminealice | 16:55c3c5727f14 | 64 | const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000 |
KwamsC | 10:cda57976225f | 65 | |
jasminealice | 17:b7b5f40cebff | 66 | /*---------------------------------------------------------------------------- |
jasminealice | 17:b7b5f40cebff | 67 | Function definitions |
jasminealice | 17:b7b5f40cebff | 68 | *----------------------------------------------------------------------------*/ |
jasminealice | 16:55c3c5727f14 | 69 | void readVoltage(); |
jasminealice | 16:55c3c5727f14 | 70 | bool readDetector(); |
jasminealice | 17:b7b5f40cebff | 71 | bool readSwitch(DigitalIn theSwitch); |
jasminealice | 16:55c3c5727f14 | 72 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command |
jasminealice | 16:55c3c5727f14 | 73 | |
KwamsC | 10:cda57976225f | 74 | |
jasminealice | 16:55c3c5727f14 | 75 | /*---------------------------------------------------------------------------- |
jasminealice | 16:55c3c5727f14 | 76 | Main |
jasminealice | 16:55c3c5727f14 | 77 | *----------------------------------------------------------------------------*/ |
jasminealice | 16:55c3c5727f14 | 78 | int main() { |
jasminealice | 16:55c3c5727f14 | 79 | /*sw1.rise(&testInterupt2); |
jasminealice | 16:55c3c5727f14 | 80 | sw1.fall(&testInterupt); |
jasminealice | 16:55c3c5727f14 | 81 | inter0.rise(&riseFunction); |
jasminealice | 16:55c3c5727f14 | 82 | inter1.rise(&riseFunction);*/ |
jasminealice | 17:b7b5f40cebff | 83 | lcd.printf("Start the journey"); |
jasminealice | 16:55c3c5727f14 | 84 | while(1){ |
jasminealice | 16:55c3c5727f14 | 85 | |
jasminealice | 16:55c3c5727f14 | 86 | wait(0.2); |
jasminealice | 17:b7b5f40cebff | 87 | if(readSwitch(sw1)){ |
jasminealice | 16:55c3c5727f14 | 88 | lcd.cls(); |
jasminealice | 16:55c3c5727f14 | 89 | lcd.printf("Forward"); |
jasminealice | 16:55c3c5727f14 | 90 | DCC_send_command(DCCaddress_darkRed,DCCinst_step13,10); // forward half speed train address 3 |
jasminealice | 16:55c3c5727f14 | 91 | }else{ |
jasminealice | 16:55c3c5727f14 | 92 | lcd.cls(); |
jasminealice | 17:b7b5f40cebff | 93 | lcd.printf("Stop"); |
jasminealice | 17:b7b5f40cebff | 94 | DCC_send_command(DCCaddress_darkRed,DCCinst_stop,10); // forward half speed train address 3 |
jasminealice | 17:b7b5f40cebff | 95 | } |
jasminealice | 17:b7b5f40cebff | 96 | |
jasminealice | 17:b7b5f40cebff | 97 | if(readSwitch(sw2)){ |
jasminealice | 17:b7b5f40cebff | 98 | |
jasminealice | 17:b7b5f40cebff | 99 | DCC_send_command(DCCaddress_switch,DCCinst_switch1,10); |
jasminealice | 17:b7b5f40cebff | 100 | myled1 = 1; |
jasminealice | 17:b7b5f40cebff | 101 | externalLed1 = 1; |
jasminealice | 17:b7b5f40cebff | 102 | DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10); |
jasminealice | 17:b7b5f40cebff | 103 | myled1 = 0; |
jasminealice | 17:b7b5f40cebff | 104 | externalLed1 = 0; |
jasminealice | 17:b7b5f40cebff | 105 | |
jasminealice | 17:b7b5f40cebff | 106 | DCC_send_command(DCCaddress_switch,DCCinst_switch2,10); //Make sure to deactivate 2 right after activating it |
jasminealice | 17:b7b5f40cebff | 107 | myled2 = 1; |
jasminealice | 17:b7b5f40cebff | 108 | externalLed2 = 1; |
jasminealice | 17:b7b5f40cebff | 109 | DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10); |
jasminealice | 17:b7b5f40cebff | 110 | myled2 = 0; |
jasminealice | 17:b7b5f40cebff | 111 | externalLed2 = 0; |
jasminealice | 17:b7b5f40cebff | 112 | |
jasminealice | 17:b7b5f40cebff | 113 | DCC_send_command(DCCaddress_switch,DCCinst_switch3,10); |
jasminealice | 17:b7b5f40cebff | 114 | myled3 = 1; |
jasminealice | 17:b7b5f40cebff | 115 | externalLed3 = 1; |
jasminealice | 17:b7b5f40cebff | 116 | DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10); |
jasminealice | 17:b7b5f40cebff | 117 | myled3 = 0; |
jasminealice | 17:b7b5f40cebff | 118 | externalLed3 = 0; |
jasminealice | 17:b7b5f40cebff | 119 | |
jasminealice | 17:b7b5f40cebff | 120 | DCC_send_command(DCCaddress_switch,DCCinst_switch4,10); |
jasminealice | 17:b7b5f40cebff | 121 | myled4 = 1; |
jasminealice | 17:b7b5f40cebff | 122 | externalLed4 = 1; |
jasminealice | 17:b7b5f40cebff | 123 | DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10); |
jasminealice | 17:b7b5f40cebff | 124 | myled4 = 0; |
jasminealice | 17:b7b5f40cebff | 125 | externalLed4 = 0; |
jasminealice | 17:b7b5f40cebff | 126 | |
jasminealice | 16:55c3c5727f14 | 127 | } |
jasminealice | 16:55c3c5727f14 | 128 | |
jasminealice | 16:55c3c5727f14 | 129 | |
jasminealice | 16:55c3c5727f14 | 130 | } |
jasminealice | 16:55c3c5727f14 | 131 | } |
KwamsC | 10:cda57976225f | 132 | |
KwamsC | 10:cda57976225f | 133 | |
KwamsC | 14:a5fd98a957e6 | 134 | /*---------------------------------------------------------------------------- |
KwamsC | 14:a5fd98a957e6 | 135 | Functions |
KwamsC | 14:a5fd98a957e6 | 136 | *----------------------------------------------------------------------------*/ |
jasminealice | 4:4b62680f91b5 | 137 | |
jasminealice | 16:55c3c5727f14 | 138 | /* |
jasminealice | 15:8d96f7a06103 | 139 | void testInterupt(){ |
jasminealice | 15:8d96f7a06103 | 140 | lcd.cls(); |
jasminealice | 15:8d96f7a06103 | 141 | myled = 1; |
jasminealice | 15:8d96f7a06103 | 142 | lcd.printf("In interupt function"); |
jasminealice | 15:8d96f7a06103 | 143 | lcd.printf("%d", sw1.read()); |
jasminealice | 15:8d96f7a06103 | 144 | myled4 = 0; |
jasminealice | 15:8d96f7a06103 | 145 | } |
jasminealice | 15:8d96f7a06103 | 146 | |
jasminealice | 15:8d96f7a06103 | 147 | void testInterupt2(){ |
jasminealice | 15:8d96f7a06103 | 148 | lcd.cls(); |
jasminealice | 15:8d96f7a06103 | 149 | myled = 0; |
jasminealice | 15:8d96f7a06103 | 150 | lcd.printf("In interupt2 function"); |
jasminealice | 15:8d96f7a06103 | 151 | lcd.printf("%d", sw1.read()); |
jasminealice | 15:8d96f7a06103 | 152 | myled4 = 1; |
jasminealice | 15:8d96f7a06103 | 153 | } |
jasminealice | 15:8d96f7a06103 | 154 | |
jasminealice | 15:8d96f7a06103 | 155 | void riseFunction(){ |
jasminealice | 15:8d96f7a06103 | 156 | lcd.printf("In rise function"); |
jasminealice | 15:8d96f7a06103 | 157 | lcd.printf("%d", inter1.read()); |
jasminealice | 15:8d96f7a06103 | 158 | } |
jasminealice | 15:8d96f7a06103 | 159 | |
jasminealice | 15:8d96f7a06103 | 160 | void fallFunction(){ |
jasminealice | 15:8d96f7a06103 | 161 | lcd.printf("In fall function"); |
jasminealice | 15:8d96f7a06103 | 162 | lcd.printf("%d", inter1.read()); |
jasminealice | 16:55c3c5727f14 | 163 | }*/ |
jasminealice | 16:55c3c5727f14 | 164 | |
jasminealice | 16:55c3c5727f14 | 165 | |
jasminealice | 16:55c3c5727f14 | 166 | |
jasminealice | 15:8d96f7a06103 | 167 | |
jasminealice | 17:b7b5f40cebff | 168 | bool readSwitch(DigitalIn theSwitch){ |
jasminealice | 17:b7b5f40cebff | 169 | int val = theSwitch.read(); |
jasminealice | 16:55c3c5727f14 | 170 | lcd.printf("%d", val); |
jasminealice | 16:55c3c5727f14 | 171 | if(val == 1) |
jasminealice | 16:55c3c5727f14 | 172 | return true; |
jasminealice | 16:55c3c5727f14 | 173 | else |
jasminealice | 16:55c3c5727f14 | 174 | return false; |
tanjinamicky | 13:78a2481fd65b | 175 | } |
tanjinamicky | 13:78a2481fd65b | 176 | |
jasminealice | 15:8d96f7a06103 | 177 | bool readDetector(){ |
jasminealice | 16:55c3c5727f14 | 178 | /*int val1 = detect_2.read(); |
jasminealice | 15:8d96f7a06103 | 179 | if(val1 == 1) |
jasminealice | 15:8d96f7a06103 | 180 | { |
jasminealice | 15:8d96f7a06103 | 181 | lcd.cls(); |
jasminealice | 15:8d96f7a06103 | 182 | lcd.printf("Detect 2: "); |
jasminealice | 15:8d96f7a06103 | 183 | lcd.printf("%d", val1); |
jasminealice | 15:8d96f7a06103 | 184 | return true; |
tanjinamicky | 13:78a2481fd65b | 185 | } |
jasminealice | 15:8d96f7a06103 | 186 | |
jasminealice | 15:8d96f7a06103 | 187 | int val2 = detect_21.read(); |
jasminealice | 15:8d96f7a06103 | 188 | if(val2 == 1) |
jasminealice | 15:8d96f7a06103 | 189 | { |
jasminealice | 15:8d96f7a06103 | 190 | lcd.cls(); |
jasminealice | 15:8d96f7a06103 | 191 | lcd.printf("Detect 21: "); |
jasminealice | 15:8d96f7a06103 | 192 | lcd.printf("%d", val2); |
jasminealice | 15:8d96f7a06103 | 193 | return true; |
jasminealice | 15:8d96f7a06103 | 194 | } |
jasminealice | 16:55c3c5727f14 | 195 | |
jasminealice | 15:8d96f7a06103 | 196 | int val3 = detect_22.read(); |
jasminealice | 15:8d96f7a06103 | 197 | if(val3 == 1) |
jasminealice | 15:8d96f7a06103 | 198 | { |
jasminealice | 15:8d96f7a06103 | 199 | lcd.cls(); |
jasminealice | 15:8d96f7a06103 | 200 | lcd.printf("Detect 22: "); |
jasminealice | 15:8d96f7a06103 | 201 | lcd.printf("%d", val3); |
jasminealice | 15:8d96f7a06103 | 202 | return true; |
jasminealice | 16:55c3c5727f14 | 203 | }*/ |
jasminealice | 17:b7b5f40cebff | 204 | return false; |
jasminealice | 15:8d96f7a06103 | 205 | } |
tanjinamicky | 13:78a2481fd65b | 206 | |
tanjinamicky | 13:78a2481fd65b | 207 | void readVoltage(){ |
jasminealice | 16:55c3c5727f14 | 208 | /*float f = Ain.read(); //Read voltage value |
jasminealice | 5:49651a9f8ff7 | 209 | float Vin = f *3.3; |
jasminealice | 5:49651a9f8ff7 | 210 | lcd.printf("%.2f", Vin); |
tanjinamicky | 13:78a2481fd65b | 211 | wait(0.1); |
jasminealice | 16:55c3c5727f14 | 212 | lcd.printf("\n");*/ |
KwamsC | 10:cda57976225f | 213 | } |
KwamsC | 10:cda57976225f | 214 | |
KwamsC | 10:cda57976225f | 215 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
KwamsC | 10:cda57976225f | 216 | { |
KwamsC | 10:cda57976225f | 217 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
KwamsC | 10:cda57976225f | 218 | unsigned __int64 temp_command = 0x0000000000000000; |
KwamsC | 10:cda57976225f | 219 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
KwamsC | 10:cda57976225f | 220 | unsigned int error = 0x00; //error byte |
KwamsC | 10:cda57976225f | 221 | //calculate error detection byte with xor |
KwamsC | 10:cda57976225f | 222 | error = address ^ inst; |
KwamsC | 10:cda57976225f | 223 | //combine packet bits in basic DCC format |
KwamsC | 10:cda57976225f | 224 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
KwamsC | 10:cda57976225f | 225 | //printf("\n\r %llx \n\r",command); |
KwamsC | 10:cda57976225f | 226 | int i=0; |
KwamsC | 10:cda57976225f | 227 | //repeat DCC command lots of times |
KwamsC | 10:cda57976225f | 228 | while(i < repeat_count) { |
KwamsC | 10:cda57976225f | 229 | temp_command = command; |
KwamsC | 10:cda57976225f | 230 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
KwamsC | 10:cda57976225f | 231 | for (int j=0; j<64; j++) { |
KwamsC | 10:cda57976225f | 232 | if((temp_command&0x8000000000000000)==0) { //test packet bit |
KwamsC | 10:cda57976225f | 233 | //send data for a "0" bit |
KwamsC | 10:cda57976225f | 234 | Track=0; |
KwamsC | 10:cda57976225f | 235 | wait_us(100); |
KwamsC | 10:cda57976225f | 236 | Track=1; |
KwamsC | 10:cda57976225f | 237 | wait_us(100); |
KwamsC | 10:cda57976225f | 238 | } else { |
KwamsC | 10:cda57976225f | 239 | //send data for a "1"bit |
KwamsC | 10:cda57976225f | 240 | Track=0; |
KwamsC | 10:cda57976225f | 241 | wait_us(58); |
KwamsC | 10:cda57976225f | 242 | Track=1; |
KwamsC | 10:cda57976225f | 243 | wait_us(58); |
KwamsC | 10:cda57976225f | 244 | } |
KwamsC | 10:cda57976225f | 245 | // next bit in packet |
KwamsC | 10:cda57976225f | 246 | temp_command = temp_command<<1; |
KwamsC | 10:cda57976225f | 247 | } |
KwamsC | 10:cda57976225f | 248 | i++; |
KwamsC | 10:cda57976225f | 249 | } |
KwamsC | 10:cda57976225f | 250 | } |