Jasmine Karlsson
/
train_rail
New project
main.cpp@14:a5fd98a957e6, 2018-06-06 (annotated)
- Committer:
- KwamsC
- Date:
- Wed Jun 06 23:09:22 2018 +0000
- Revision:
- 14:a5fd98a957e6
- Parent:
- 13:78a2481fd65b
- Child:
- 15:8d96f7a06103
test sensors
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" |
jasminealice | 4:4b62680f91b5 | 3 | #include <string> |
jasminealice | 4:4b62680f91b5 | 4 | #include <iostream> |
KwamsC | 14:a5fd98a957e6 | 5 | #include "mcp23017.h" |
KwamsC | 10:cda57976225f | 6 | |
KwamsC | 10:cda57976225f | 7 | //board 1 |
KwamsC | 10:cda57976225f | 8 | |
KwamsC | 10:cda57976225f | 9 | I2C i2c(p28, p27); // sda, scl pins (you can also use the other I2C port pins: 9,10) |
KwamsC | 10:cda57976225f | 10 | |
KwamsC | 10:cda57976225f | 11 | 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 | 12 | |
KwamsC | 14:a5fd98a957e6 | 13 | /* LED Definitions */ |
KwamsC | 14:a5fd98a957e6 | 14 | DigitalOut myled(LED1); |
KwamsC | 14:a5fd98a957e6 | 15 | DigitalOut myled2(LED2); |
KwamsC | 14:a5fd98a957e6 | 16 | DigitalOut myled3(LED3); |
KwamsC | 14:a5fd98a957e6 | 17 | DigitalOut myled4(LED4); |
KwamsC | 14:a5fd98a957e6 | 18 | |
KwamsC | 14:a5fd98a957e6 | 19 | TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd |
KwamsC | 14:a5fd98a957e6 | 20 | |
KwamsC | 10:cda57976225f | 21 | AnalogIn Ain(p20); // pot. met. |
tanjinamicky | 13:78a2481fd65b | 22 | DigitalOut Track(p21); // train track |
KwamsC | 10:cda57976225f | 23 | |
KwamsC | 14:a5fd98a957e6 | 24 | /* Train detectors d2, d21, d22 (trainstation) */ |
KwamsC | 14:a5fd98a957e6 | 25 | AnalogIn detect_21(p15); |
tanjinamicky | 13:78a2481fd65b | 26 | AnalogIn detect_22(p16); |
tanjinamicky | 13:78a2481fd65b | 27 | AnalogIn detect_2(p17); |
KwamsC | 10:cda57976225f | 28 | |
KwamsC | 14:a5fd98a957e6 | 29 | /* Switch Definitions */ |
KwamsC | 14:a5fd98a957e6 | 30 | DigitalIn sw1(p5); |
KwamsC | 14:a5fd98a957e6 | 31 | DigitalIn sw2(p30); |
KwamsC | 14:a5fd98a957e6 | 32 | DigitalIn sw3(p29); |
KwamsC | 14:a5fd98a957e6 | 33 | DigitalIn sw4(p28); |
jasminealice | 0:57cbecaac770 | 34 | |
KwamsC | 14:a5fd98a957e6 | 35 | /* Train Definitions */ |
tanjinamicky | 13:78a2481fd65b | 36 | unsigned int DCCaddress_darkRed = 0x01; |
tanjinamicky | 13:78a2481fd65b | 37 | unsigned int DCCaddress_lightRed = 0x03; |
KwamsC | 14:a5fd98a957e6 | 38 | |
KwamsC | 14:a5fd98a957e6 | 39 | /* Train movement */ |
KwamsC | 14:a5fd98a957e6 | 40 | |
KwamsC | 14:a5fd98a957e6 | 41 | /*---------------------------------------------------------------------------- |
KwamsC | 14:a5fd98a957e6 | 42 | Train movement |
KwamsC | 14:a5fd98a957e6 | 43 | *----------------------------------------------------------------------------*/ |
KwamsC | 14:a5fd98a957e6 | 44 | //move forward |
KwamsC | 10:cda57976225f | 45 | unsigned int DCCinst_forward = 0x68; //forward half speed |
KwamsC | 10:cda57976225f | 46 | |
KwamsC | 14:a5fd98a957e6 | 47 | //move backwards/reverse |
KwamsC | 14:a5fd98a957e6 | 48 | unsigned int DCCinst_reverse = 01001000; //reverse half speed |
KwamsC | 14:a5fd98a957e6 | 49 | |
KwamsC | 14:a5fd98a957e6 | 50 | //speed dial |
KwamsC | 14:a5fd98a957e6 | 51 | unsigned int DCCinst_step1 = 01100010; //step 1 |
KwamsC | 14:a5fd98a957e6 | 52 | unsigned int DCCinst_step2 = 01110010; //step 2 |
KwamsC | 14:a5fd98a957e6 | 53 | unsigned int DCCinst_step3 = 01100011; //step 3 |
KwamsC | 14:a5fd98a957e6 | 54 | unsigned int DCCinst_step4 = 01110011; //step 4 |
KwamsC | 14:a5fd98a957e6 | 55 | unsigned int DCCinst_step5 = 01100100; //step 5 |
KwamsC | 14:a5fd98a957e6 | 56 | unsigned int DCCinst_step6 = 01110100; //step 6 1/4 speed |
KwamsC | 14:a5fd98a957e6 | 57 | |
KwamsC | 14:a5fd98a957e6 | 58 | unsigned int DCCinst_step13 = 01100000; //step 13 1/2 speed |
KwamsC | 14:a5fd98a957e6 | 59 | |
KwamsC | 14:a5fd98a957e6 | 60 | unsigned int DCCinst_step20 = 01110101; //step 20 3/4 speed |
KwamsC | 14:a5fd98a957e6 | 61 | |
KwamsC | 14:a5fd98a957e6 | 62 | unsigned int DCCinst_step28 = 01111111; //step 28 Full speed |
KwamsC | 14:a5fd98a957e6 | 63 | |
KwamsC | 14:a5fd98a957e6 | 64 | //stop |
tanjinamicky | 13:78a2481fd65b | 65 | unsigned int DCCinst_forward_stop = 0x60; //forward and stop 01100000 |
tanjinamicky | 13:78a2481fd65b | 66 | |
KwamsC | 10:cda57976225f | 67 | //move forward |
KwamsC | 10:cda57976225f | 68 | |
KwamsC | 10:cda57976225f | 69 | //speed dial |
KwamsC | 10:cda57976225f | 70 | |
KwamsC | 10:cda57976225f | 71 | //stop |
KwamsC | 10:cda57976225f | 72 | |
KwamsC | 10:cda57976225f | 73 | //read switch out |
KwamsC | 10:cda57976225f | 74 | |
KwamsC | 10:cda57976225f | 75 | //read signal |
KwamsC | 10:cda57976225f | 76 | |
KwamsC | 10:cda57976225f | 77 | //display message |
KwamsC | 10:cda57976225f | 78 | |
KwamsC | 10:cda57976225f | 79 | |
KwamsC | 14:a5fd98a957e6 | 80 | /*---------------------------------------------------------------------------- |
KwamsC | 14:a5fd98a957e6 | 81 | Functions |
KwamsC | 14:a5fd98a957e6 | 82 | *----------------------------------------------------------------------------*/ |
KwamsC | 14:a5fd98a957e6 | 83 | /* Functions definition */ |
KwamsC | 14:a5fd98a957e6 | 84 | void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data); // Write an I2C register |
KwamsC | 14:a5fd98a957e6 | 85 | uint8_t mcpReadReg(uint8_t address, uint8_t reg); // Read an I2C register |
KwamsC | 14:a5fd98a957e6 | 86 | void initMcp0(void); // Initialization of MCP23017 with address bits 000 (track sensors) |
KwamsC | 14:a5fd98a957e6 | 87 | void testMcp0(void); // Test of MCP23017 with address bits 000 |
KwamsC | 14:a5fd98a957e6 | 88 | void readVoltage(); |
KwamsC | 14:a5fd98a957e6 | 89 | void readDetector(); |
KwamsC | 14:a5fd98a957e6 | 90 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command |
jasminealice | 4:4b62680f91b5 | 91 | |
jasminealice | 5:49651a9f8ff7 | 92 | int main() { |
jasminealice | 5:49651a9f8ff7 | 93 | while(1){ |
tanjinamicky | 13:78a2481fd65b | 94 | //unsigned int DCCaddress = 0x03; |
tanjinamicky | 13:78a2481fd65b | 95 | //unsigned int DCCaddress_darkRed = 0x01; |
tanjinamicky | 13:78a2481fd65b | 96 | readDetector(); |
tanjinamicky | 13:78a2481fd65b | 97 | lcd.printf("Forward"); |
tanjinamicky | 13:78a2481fd65b | 98 | DCC_send_command(DCCaddress_lightRed,DCCinst_forward,400); // forward half speed train address 3 |
tanjinamicky | 13:78a2481fd65b | 99 | wait(0.5); |
tanjinamicky | 13:78a2481fd65b | 100 | lcd.printf("Stop"); |
tanjinamicky | 13:78a2481fd65b | 101 | DCC_send_command(DCCaddress_lightRed,DCCinst_forward_stop,400); // forward half speed train address 3 |
tanjinamicky | 13:78a2481fd65b | 102 | wait(1); |
tanjinamicky | 13:78a2481fd65b | 103 | //readVoltage(); |
tanjinamicky | 13:78a2481fd65b | 104 | //myled=1; |
tanjinamicky | 13:78a2481fd65b | 105 | } |
tanjinamicky | 13:78a2481fd65b | 106 | } |
tanjinamicky | 13:78a2481fd65b | 107 | |
tanjinamicky | 13:78a2481fd65b | 108 | void readDetector(){ |
tanjinamicky | 13:78a2481fd65b | 109 | float val = detect_21.read(); |
tanjinamicky | 13:78a2481fd65b | 110 | lcd.printf("%.2f", val); |
tanjinamicky | 13:78a2481fd65b | 111 | } |
tanjinamicky | 13:78a2481fd65b | 112 | |
tanjinamicky | 13:78a2481fd65b | 113 | void readVoltage(){ |
jasminealice | 5:49651a9f8ff7 | 114 | float f = Ain.read(); //Read voltage value |
jasminealice | 5:49651a9f8ff7 | 115 | float Vin = f *3.3; |
jasminealice | 5:49651a9f8ff7 | 116 | lcd.printf("%.2f", Vin); |
tanjinamicky | 13:78a2481fd65b | 117 | wait(0.1); |
jasminealice | 5:49651a9f8ff7 | 118 | lcd.printf("\n"); |
jasminealice | 5:49651a9f8ff7 | 119 | } |
KwamsC | 10:cda57976225f | 120 | |
KwamsC | 10:cda57976225f | 121 | void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data){ |
KwamsC | 10:cda57976225f | 122 | char cmd[2];cmd[0] = reg; |
KwamsC | 10:cda57976225f | 123 | cmd[1] = data; |
KwamsC | 10:cda57976225f | 124 | i2c.write(address, cmd, 2); // Write 2 bytes to device on specified address |
KwamsC | 10:cda57976225f | 125 | } |
KwamsC | 10:cda57976225f | 126 | |
KwamsC | 10:cda57976225f | 127 | uint8_t mcpReadReg(uint8_t address, uint8_t reg){ |
KwamsC | 10:cda57976225f | 128 | char cmd[1]; |
KwamsC | 10:cda57976225f | 129 | cmd[0] = reg; |
KwamsC | 10:cda57976225f | 130 | i2c.write(address, cmd, 1); // Write address |
KwamsC | 10:cda57976225f | 131 | i2c.read(address, cmd, 1); // Read value (one byte) |
KwamsC | 10:cda57976225f | 132 | |
KwamsC | 10:cda57976225f | 133 | return cmd[0]; // Return the read value |
KwamsC | 10:cda57976225f | 134 | } |
KwamsC | 10:cda57976225f | 135 | |
KwamsC | 14:a5fd98a957e6 | 136 | /* Initialization of the MCP23017 for the track sensors. |
KwamsC | 14:a5fd98a957e6 | 137 | We should enable the MCP23017 interrupts here because the sensors will only give a short pulse to the MCP23017*/ |
KwamsC | 10:cda57976225f | 138 | void initMcp0(void){ |
KwamsC | 10:cda57976225f | 139 | mcpWriteReg(addr0, MCP_IODIRA, 0xff); // All inputs |
KwamsC | 10:cda57976225f | 140 | mcpWriteReg(addr0, MCP_IODIRB, 0xff); // All inputs |
KwamsC | 10:cda57976225f | 141 | } |
KwamsC | 10:cda57976225f | 142 | |
KwamsC | 14:a5fd98a957e6 | 143 | /* Test of the track sensors |
KwamsC | 14:a5fd98a957e6 | 144 | * This does not use interrupts so the chance of actually detecting a train is very, very low |
KwamsC | 14:a5fd98a957e6 | 145 | * With some patience it's good enough to test the hardware though. |
KwamsC | 14:a5fd98a957e6 | 146 | */ |
KwamsC | 14:a5fd98a957e6 | 147 | void testMcp0(void) { |
KwamsC | 14:a5fd98a957e6 | 148 | uint16_t sensors; // Put both ports in the sensor variable |
KwamsC | 14:a5fd98a957e6 | 149 | sensors = (mcpReadReg(addr0, MCP_GPIOB) << 8) | mcpReadReg(addr0, MCP_GPIOA); |
KwamsC | 14:a5fd98a957e6 | 150 | // Flip all bits, since the sensors are active low and it gives me headaches |
KwamsC | 14:a5fd98a957e6 | 151 | // You could also set the IPOLA / IPOLB registers instead (see page 13 of the MCP23017 datasheet) |
KwamsC | 14:a5fd98a957e6 | 152 | sensors = ~sensors; |
KwamsC | 14:a5fd98a957e6 | 153 | lcd.printf("sensors: %04X\n", sensors); |
KwamsC | 14:a5fd98a957e6 | 154 | } |
KwamsC | 14:a5fd98a957e6 | 155 | |
KwamsC | 10:cda57976225f | 156 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
KwamsC | 10:cda57976225f | 157 | { |
KwamsC | 10:cda57976225f | 158 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
KwamsC | 10:cda57976225f | 159 | unsigned __int64 temp_command = 0x0000000000000000; |
KwamsC | 10:cda57976225f | 160 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
KwamsC | 10:cda57976225f | 161 | unsigned int error = 0x00; //error byte |
KwamsC | 10:cda57976225f | 162 | //calculate error detection byte with xor |
KwamsC | 10:cda57976225f | 163 | error = address ^ inst; |
KwamsC | 10:cda57976225f | 164 | //combine packet bits in basic DCC format |
KwamsC | 10:cda57976225f | 165 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
KwamsC | 10:cda57976225f | 166 | //printf("\n\r %llx \n\r",command); |
KwamsC | 10:cda57976225f | 167 | int i=0; |
KwamsC | 10:cda57976225f | 168 | //repeat DCC command lots of times |
KwamsC | 10:cda57976225f | 169 | while(i < repeat_count) { |
KwamsC | 10:cda57976225f | 170 | temp_command = command; |
KwamsC | 10:cda57976225f | 171 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
KwamsC | 10:cda57976225f | 172 | for (int j=0; j<64; j++) { |
KwamsC | 10:cda57976225f | 173 | if((temp_command&0x8000000000000000)==0) { //test packet bit |
KwamsC | 10:cda57976225f | 174 | //send data for a "0" bit |
KwamsC | 10:cda57976225f | 175 | Track=0; |
KwamsC | 10:cda57976225f | 176 | wait_us(100); |
KwamsC | 10:cda57976225f | 177 | Track=1; |
KwamsC | 10:cda57976225f | 178 | wait_us(100); |
KwamsC | 10:cda57976225f | 179 | } else { |
KwamsC | 10:cda57976225f | 180 | //send data for a "1"bit |
KwamsC | 10:cda57976225f | 181 | Track=0; |
KwamsC | 10:cda57976225f | 182 | wait_us(58); |
KwamsC | 10:cda57976225f | 183 | Track=1; |
KwamsC | 10:cda57976225f | 184 | wait_us(58); |
KwamsC | 10:cda57976225f | 185 | } |
KwamsC | 10:cda57976225f | 186 | // next bit in packet |
KwamsC | 10:cda57976225f | 187 | temp_command = temp_command<<1; |
KwamsC | 10:cda57976225f | 188 | } |
KwamsC | 10:cda57976225f | 189 | i++; |
KwamsC | 10:cda57976225f | 190 | } |
KwamsC | 10:cda57976225f | 191 | } |