New project

Dependencies:   mbed TextLCD

Committer:
jasminealice
Date:
Mon Jun 11 10:53:14 2018 +0000
Revision:
18:f5824ba95892
Parent:
17:b7b5f40cebff
Child:
20:32ba0a5f2d02
Latest changes

Who changed what in which revision?

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