New project

Dependencies:   mbed TextLCD

Committer:
jasminealice
Date:
Thu Jun 07 15:09:53 2018 +0000
Revision:
15:8d96f7a06103
Parent:
14:a5fd98a957e6
Child:
16:55c3c5727f14
Day work june 7th

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 4:4b62680f91b5 3 #include <string>
jasminealice 4:4b62680f91b5 4 #include <iostream>
KwamsC 14:a5fd98a957e6 5 #include "mcp23017.h"
jasminealice 15:8d96f7a06103 6 #include "train.h"
KwamsC 10:cda57976225f 7
KwamsC 10:cda57976225f 8 //board 1
KwamsC 10:cda57976225f 9
KwamsC 10:cda57976225f 10 I2C i2c(p28, p27); // sda, scl pins (you can also use the other I2C port pins: 9,10)
KwamsC 10:cda57976225f 11
KwamsC 10:cda57976225f 12 const int addr0 = 0x20 << 1; // 7-bit base address of the MCP23017 + address bits (which are 000 for the first one), shifted 1 place to the right (see top header)
KwamsC 10:cda57976225f 13
KwamsC 14:a5fd98a957e6 14 /* LED Definitions */
KwamsC 14:a5fd98a957e6 15 DigitalOut myled(LED1);
KwamsC 14:a5fd98a957e6 16 DigitalOut myled2(LED2);
KwamsC 14:a5fd98a957e6 17 DigitalOut myled3(LED3);
KwamsC 14:a5fd98a957e6 18 DigitalOut myled4(LED4);
KwamsC 14:a5fd98a957e6 19
KwamsC 14:a5fd98a957e6 20 TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd
KwamsC 14:a5fd98a957e6 21
KwamsC 10:cda57976225f 22 AnalogIn Ain(p20); // pot. met.
jasminealice 15:8d96f7a06103 23 DigitalOut Track(p29); // train track
KwamsC 10:cda57976225f 24
KwamsC 14:a5fd98a957e6 25 /* Train detectors d2, d21, d22 (trainstation) */
jasminealice 15:8d96f7a06103 26 DigitalIn detect_21(p17);
jasminealice 15:8d96f7a06103 27 DigitalIn detect_22(p16);
jasminealice 15:8d96f7a06103 28 DigitalIn detect_2(p15);
KwamsC 10:cda57976225f 29
KwamsC 14:a5fd98a957e6 30 /* Switch Definitions */
jasminealice 15:8d96f7a06103 31 InterruptIn sw1(p5);
jasminealice 15:8d96f7a06103 32 InterruptIn inter0(p12);
jasminealice 15:8d96f7a06103 33 InterruptIn inter1(p13);
jasminealice 15:8d96f7a06103 34 /*DigitalIn sw2(p30);
KwamsC 14:a5fd98a957e6 35 DigitalIn sw3(p29);
jasminealice 15:8d96f7a06103 36 DigitalIn sw4(p28);*/
jasminealice 0:57cbecaac770 37
KwamsC 14:a5fd98a957e6 38 /* Train Definitions */
tanjinamicky 13:78a2481fd65b 39 unsigned int DCCaddress_darkRed = 0x01;
tanjinamicky 13:78a2481fd65b 40 unsigned int DCCaddress_lightRed = 0x03;
KwamsC 14:a5fd98a957e6 41
KwamsC 14:a5fd98a957e6 42 /* Train movement */
KwamsC 14:a5fd98a957e6 43
KwamsC 14:a5fd98a957e6 44 /*----------------------------------------------------------------------------
KwamsC 14:a5fd98a957e6 45 Train movement
KwamsC 14:a5fd98a957e6 46 *----------------------------------------------------------------------------*/
KwamsC 10:cda57976225f 47
KwamsC 14:a5fd98a957e6 48 //move backwards/reverse
jasminealice 15:8d96f7a06103 49 const unsigned int DCCinst_reverse = 01001000; //reverse speed
KwamsC 14:a5fd98a957e6 50
jasminealice 15:8d96f7a06103 51 //speed dial forward
jasminealice 15:8d96f7a06103 52 const unsigned int DCCinst_step2 = 01110010; //step 2
jasminealice 15:8d96f7a06103 53 const unsigned int DCCinst_step4 = 01110011; //step 4
jasminealice 15:8d96f7a06103 54 const unsigned int DCCinst_step6 = 01110100; //step 6 1/4 speed
KwamsC 14:a5fd98a957e6 55
jasminealice 15:8d96f7a06103 56 const unsigned int DCCinst_step13 = 01111000; //step 13 1/2 speed
KwamsC 14:a5fd98a957e6 57
jasminealice 15:8d96f7a06103 58 const unsigned int DCCinst_step20 = 01110101; //step 20 3/4 speed
KwamsC 14:a5fd98a957e6 59
jasminealice 15:8d96f7a06103 60 const unsigned int DCCinst_step28 = 01111111; //step 28 Full speed
KwamsC 14:a5fd98a957e6 61
KwamsC 14:a5fd98a957e6 62 //stop
jasminealice 15:8d96f7a06103 63 const unsigned int DCCinst_stop = 01100000; //forward and stop 01100000
KwamsC 10:cda57976225f 64
KwamsC 10:cda57976225f 65 //read switch out
KwamsC 10:cda57976225f 66
KwamsC 10:cda57976225f 67 //read signal
KwamsC 10:cda57976225f 68
KwamsC 10:cda57976225f 69 //display message
KwamsC 10:cda57976225f 70
KwamsC 10:cda57976225f 71
KwamsC 14:a5fd98a957e6 72 /*----------------------------------------------------------------------------
KwamsC 14:a5fd98a957e6 73 Functions
KwamsC 14:a5fd98a957e6 74 *----------------------------------------------------------------------------*/
KwamsC 14:a5fd98a957e6 75 /* Functions definition */
KwamsC 14:a5fd98a957e6 76 void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data); // Write an I2C register
KwamsC 14:a5fd98a957e6 77 uint8_t mcpReadReg(uint8_t address, uint8_t reg); // Read an I2C register
KwamsC 14:a5fd98a957e6 78 void initMcp0(void); // Initialization of MCP23017 with address bits 000 (track sensors)
KwamsC 14:a5fd98a957e6 79 void testMcp0(void); // Test of MCP23017 with address bits 000
KwamsC 14:a5fd98a957e6 80 void readVoltage();
jasminealice 15:8d96f7a06103 81 bool readDetector();
KwamsC 14:a5fd98a957e6 82 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command
jasminealice 4:4b62680f91b5 83
jasminealice 15:8d96f7a06103 84
jasminealice 15:8d96f7a06103 85 void testInterupt(){
jasminealice 15:8d96f7a06103 86 lcd.cls();
jasminealice 15:8d96f7a06103 87 myled = 1;
jasminealice 15:8d96f7a06103 88 lcd.printf("In interupt function");
jasminealice 15:8d96f7a06103 89 lcd.printf("%d", sw1.read());
jasminealice 15:8d96f7a06103 90 myled4 = 0;
jasminealice 15:8d96f7a06103 91 }
jasminealice 15:8d96f7a06103 92
jasminealice 15:8d96f7a06103 93 void testInterupt2(){
jasminealice 15:8d96f7a06103 94 lcd.cls();
jasminealice 15:8d96f7a06103 95 myled = 0;
jasminealice 15:8d96f7a06103 96 lcd.printf("In interupt2 function");
jasminealice 15:8d96f7a06103 97 lcd.printf("%d", sw1.read());
jasminealice 15:8d96f7a06103 98 myled4 = 1;
jasminealice 15:8d96f7a06103 99 }
jasminealice 15:8d96f7a06103 100
jasminealice 15:8d96f7a06103 101 void riseFunction(){
jasminealice 15:8d96f7a06103 102 lcd.printf("In rise function");
jasminealice 15:8d96f7a06103 103 lcd.printf("%d", inter1.read());
jasminealice 15:8d96f7a06103 104 }
jasminealice 15:8d96f7a06103 105
jasminealice 15:8d96f7a06103 106 void fallFunction(){
jasminealice 15:8d96f7a06103 107 lcd.printf("In fall function");
jasminealice 15:8d96f7a06103 108 lcd.printf("%d", inter1.read());
jasminealice 15:8d96f7a06103 109 }
jasminealice 15:8d96f7a06103 110
jasminealice 5:49651a9f8ff7 111 int main() {
jasminealice 15:8d96f7a06103 112 /*sw1.rise(&testInterupt2);
jasminealice 15:8d96f7a06103 113 sw1.fall(&testInterupt);
jasminealice 15:8d96f7a06103 114 inter0.rise(&riseFunction);
jasminealice 15:8d96f7a06103 115 inter1.rise(&riseFunction);*/
jasminealice 5:49651a9f8ff7 116 while(1){
jasminealice 15:8d96f7a06103 117
jasminealice 15:8d96f7a06103 118 //wait(0.25);
jasminealice 15:8d96f7a06103 119 unsigned int DCCaddress = 0x03;
jasminealice 15:8d96f7a06103 120 unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
jasminealice 15:8d96f7a06103 121
jasminealice 15:8d96f7a06103 122 DCC_send_command(DCCaddress_darkRed,DCCinst_step6,400); // forward half speed train address 3
tanjinamicky 13:78a2481fd65b 123 lcd.printf("Forward");
jasminealice 15:8d96f7a06103 124 //unsigned int DCCaddress_darkRed = 0x01;
jasminealice 15:8d96f7a06103 125 /*if(readDetector())
jasminealice 15:8d96f7a06103 126 {
jasminealice 15:8d96f7a06103 127 DCC_send_command(DCCaddress_darkRed,DCCinst_stop,400); // forward half speed train address 3
jasminealice 15:8d96f7a06103 128 lcd.printf("Stop");
jasminealice 15:8d96f7a06103 129 wait(2);
jasminealice 15:8d96f7a06103 130 }
jasminealice 15:8d96f7a06103 131 else
jasminealice 15:8d96f7a06103 132 lcd.printf("Go forward");*/
jasminealice 15:8d96f7a06103 133 //lcd.cls();
jasminealice 15:8d96f7a06103 134 //lcd.printf("Forward");
jasminealice 15:8d96f7a06103 135 //wait(2);
jasminealice 15:8d96f7a06103 136 DCC_send_command(DCCaddress_darkRed,DCCinst_stop,400); // forward half speed train address 3
tanjinamicky 13:78a2481fd65b 137 lcd.printf("Stop");
jasminealice 15:8d96f7a06103 138 wait(2);
jasminealice 15:8d96f7a06103 139 //wait(1.5);
jasminealice 15:8d96f7a06103 140 //lcd.cls();
jasminealice 15:8d96f7a06103 141 //lcd.printf("Stop");
jasminealice 15:8d96f7a06103 142 //wait(1);
tanjinamicky 13:78a2481fd65b 143 //readVoltage();
tanjinamicky 13:78a2481fd65b 144 //myled=1;
tanjinamicky 13:78a2481fd65b 145 }
tanjinamicky 13:78a2481fd65b 146 }
tanjinamicky 13:78a2481fd65b 147
jasminealice 15:8d96f7a06103 148 bool readDetector(){
jasminealice 15:8d96f7a06103 149 int val1 = detect_2.read();
jasminealice 15:8d96f7a06103 150
jasminealice 15:8d96f7a06103 151
jasminealice 15:8d96f7a06103 152 if(val1 == 1)
jasminealice 15:8d96f7a06103 153 {
jasminealice 15:8d96f7a06103 154 lcd.cls();
jasminealice 15:8d96f7a06103 155 lcd.printf("Detect 2: ");
jasminealice 15:8d96f7a06103 156 lcd.printf("%d", val1);
jasminealice 15:8d96f7a06103 157 return true;
tanjinamicky 13:78a2481fd65b 158 }
jasminealice 15:8d96f7a06103 159
jasminealice 15:8d96f7a06103 160
jasminealice 15:8d96f7a06103 161 int val2 = detect_21.read();
jasminealice 15:8d96f7a06103 162
jasminealice 15:8d96f7a06103 163 if(val2 == 1)
jasminealice 15:8d96f7a06103 164 {
jasminealice 15:8d96f7a06103 165 lcd.cls();
jasminealice 15:8d96f7a06103 166 lcd.printf("Detect 21: ");
jasminealice 15:8d96f7a06103 167 lcd.printf("%d", val2);
jasminealice 15:8d96f7a06103 168 return true;
jasminealice 15:8d96f7a06103 169 }
jasminealice 15:8d96f7a06103 170 int val3 = detect_22.read();
jasminealice 15:8d96f7a06103 171 if(val3 == 1)
jasminealice 15:8d96f7a06103 172 {
jasminealice 15:8d96f7a06103 173 lcd.cls();
jasminealice 15:8d96f7a06103 174 lcd.printf("Detect 22: ");
jasminealice 15:8d96f7a06103 175 lcd.printf("%d", val3);
jasminealice 15:8d96f7a06103 176 return true;
jasminealice 15:8d96f7a06103 177 }
jasminealice 15:8d96f7a06103 178 return false;
jasminealice 15:8d96f7a06103 179 }
tanjinamicky 13:78a2481fd65b 180
tanjinamicky 13:78a2481fd65b 181 void readVoltage(){
jasminealice 5:49651a9f8ff7 182 float f = Ain.read(); //Read voltage value
jasminealice 5:49651a9f8ff7 183 float Vin = f *3.3;
jasminealice 5:49651a9f8ff7 184 lcd.printf("%.2f", Vin);
tanjinamicky 13:78a2481fd65b 185 wait(0.1);
jasminealice 5:49651a9f8ff7 186 lcd.printf("\n");
jasminealice 15:8d96f7a06103 187 }
KwamsC 10:cda57976225f 188
KwamsC 10:cda57976225f 189 void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data){
KwamsC 10:cda57976225f 190 char cmd[2];cmd[0] = reg;
KwamsC 10:cda57976225f 191 cmd[1] = data;
KwamsC 10:cda57976225f 192 i2c.write(address, cmd, 2); // Write 2 bytes to device on specified address
KwamsC 10:cda57976225f 193 }
KwamsC 10:cda57976225f 194
KwamsC 10:cda57976225f 195 uint8_t mcpReadReg(uint8_t address, uint8_t reg){
KwamsC 10:cda57976225f 196 char cmd[1];
KwamsC 10:cda57976225f 197 cmd[0] = reg;
KwamsC 10:cda57976225f 198 i2c.write(address, cmd, 1); // Write address
KwamsC 10:cda57976225f 199 i2c.read(address, cmd, 1); // Read value (one byte)
KwamsC 10:cda57976225f 200
KwamsC 10:cda57976225f 201 return cmd[0]; // Return the read value
KwamsC 10:cda57976225f 202 }
KwamsC 10:cda57976225f 203
KwamsC 14:a5fd98a957e6 204 /* Initialization of the MCP23017 for the track sensors.
KwamsC 14:a5fd98a957e6 205 We should enable the MCP23017 interrupts here because the sensors will only give a short pulse to the MCP23017*/
KwamsC 10:cda57976225f 206 void initMcp0(void){
KwamsC 10:cda57976225f 207 mcpWriteReg(addr0, MCP_IODIRA, 0xff); // All inputs
KwamsC 10:cda57976225f 208 mcpWriteReg(addr0, MCP_IODIRB, 0xff); // All inputs
KwamsC 10:cda57976225f 209 }
KwamsC 10:cda57976225f 210
KwamsC 14:a5fd98a957e6 211 /* Test of the track sensors
KwamsC 14:a5fd98a957e6 212 * This does not use interrupts so the chance of actually detecting a train is very, very low
KwamsC 14:a5fd98a957e6 213 * With some patience it's good enough to test the hardware though.
KwamsC 14:a5fd98a957e6 214 */
KwamsC 14:a5fd98a957e6 215 void testMcp0(void) {
KwamsC 14:a5fd98a957e6 216 uint16_t sensors; // Put both ports in the sensor variable
KwamsC 14:a5fd98a957e6 217 sensors = (mcpReadReg(addr0, MCP_GPIOB) << 8) | mcpReadReg(addr0, MCP_GPIOA);
KwamsC 14:a5fd98a957e6 218 // Flip all bits, since the sensors are active low and it gives me headaches
KwamsC 14:a5fd98a957e6 219 // You could also set the IPOLA / IPOLB registers instead (see page 13 of the MCP23017 datasheet)
KwamsC 14:a5fd98a957e6 220 sensors = ~sensors;
KwamsC 14:a5fd98a957e6 221 lcd.printf("sensors: %04X\n", sensors);
KwamsC 14:a5fd98a957e6 222 }
KwamsC 14:a5fd98a957e6 223
KwamsC 10:cda57976225f 224 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
KwamsC 10:cda57976225f 225 {
KwamsC 10:cda57976225f 226 unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
KwamsC 10:cda57976225f 227 unsigned __int64 temp_command = 0x0000000000000000;
KwamsC 10:cda57976225f 228 unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
KwamsC 10:cda57976225f 229 unsigned int error = 0x00; //error byte
KwamsC 10:cda57976225f 230 //calculate error detection byte with xor
KwamsC 10:cda57976225f 231 error = address ^ inst;
KwamsC 10:cda57976225f 232 //combine packet bits in basic DCC format
KwamsC 10:cda57976225f 233 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
KwamsC 10:cda57976225f 234 //printf("\n\r %llx \n\r",command);
KwamsC 10:cda57976225f 235 int i=0;
KwamsC 10:cda57976225f 236 //repeat DCC command lots of times
KwamsC 10:cda57976225f 237 while(i < repeat_count) {
KwamsC 10:cda57976225f 238 temp_command = command;
KwamsC 10:cda57976225f 239 //loops through packet bits encoding and sending out digital pulses for a DCC command
KwamsC 10:cda57976225f 240 for (int j=0; j<64; j++) {
KwamsC 10:cda57976225f 241 if((temp_command&0x8000000000000000)==0) { //test packet bit
KwamsC 10:cda57976225f 242 //send data for a "0" bit
KwamsC 10:cda57976225f 243 Track=0;
KwamsC 10:cda57976225f 244 wait_us(100);
KwamsC 10:cda57976225f 245 Track=1;
KwamsC 10:cda57976225f 246 wait_us(100);
KwamsC 10:cda57976225f 247 } else {
KwamsC 10:cda57976225f 248 //send data for a "1"bit
KwamsC 10:cda57976225f 249 Track=0;
KwamsC 10:cda57976225f 250 wait_us(58);
KwamsC 10:cda57976225f 251 Track=1;
KwamsC 10:cda57976225f 252 wait_us(58);
KwamsC 10:cda57976225f 253 }
KwamsC 10:cda57976225f 254 // next bit in packet
KwamsC 10:cda57976225f 255 temp_command = temp_command<<1;
KwamsC 10:cda57976225f 256 }
KwamsC 10:cda57976225f 257 i++;
KwamsC 10:cda57976225f 258 }
KwamsC 10:cda57976225f 259 }