Jasmine Karlsson
/
train_rail
New project
Diff: main.cpp
- Revision:
- 16:55c3c5727f14
- Parent:
- 15:8d96f7a06103
- Child:
- 17:b7b5f40cebff
--- a/main.cpp Thu Jun 07 15:09:53 2018 +0000 +++ b/main.cpp Fri Jun 08 11:43:36 2018 +0000 @@ -5,13 +5,11 @@ #include "mcp23017.h" #include "train.h" -//board 1 - -I2C i2c(p28, p27); // sda, scl pins (you can also use the other I2C port pins: 9,10) +//Board 1 -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) - -/* LED Definitions */ +/*---------------------------------------------------------------------------- +Pin definitions +*----------------------------------------------------------------------------*/ DigitalOut myled(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); @@ -19,69 +17,83 @@ TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd -AnalogIn Ain(p20); // pot. met. -DigitalOut Track(p29); // train track +//AnalogIn Ain(p20); // pot. met. +DigitalOut Track(p20); // train track /* Train detectors d2, d21, d22 (trainstation) */ -DigitalIn detect_21(p17); -DigitalIn detect_22(p16); -DigitalIn detect_2(p15); +// detect_21(p17); +//DigitalIn detect_22(p16); +//DigitalIn detect_2(p15); /* Switch Definitions */ -InterruptIn sw1(p5); -InterruptIn inter0(p12); -InterruptIn inter1(p13); -/*DigitalIn sw2(p30); -DigitalIn sw3(p29); -DigitalIn sw4(p28);*/ - -/* Train Definitions */ -unsigned int DCCaddress_darkRed = 0x01; -unsigned int DCCaddress_lightRed = 0x03; - -/* Train movement */ +DigitalIn sw1(p29); +DigitalIn sw2(p30); +DigitalIn sw3(p11); +DigitalIn sw4(p12); +//InterruptIn sw1(p5); +//InterruptIn inter0(p12); +//InterruptIn inter1(p13); /*---------------------------------------------------------------------------- Train movement *----------------------------------------------------------------------------*/ //move backwards/reverse -const unsigned int DCCinst_reverse = 01001000; //reverse speed +const unsigned int DCCinst_reverse = 0x48; //reverse speed //speed dial forward -const unsigned int DCCinst_step2 = 01110010; //step 2 -const unsigned int DCCinst_step4 = 01110011; //step 4 -const unsigned int DCCinst_step6 = 01110100; //step 6 1/4 speed - -const unsigned int DCCinst_step13 = 01111000; //step 13 1/2 speed - -const unsigned int DCCinst_step20 = 01110101; //step 20 3/4 speed - -const unsigned int DCCinst_step28 = 01111111; //step 28 Full speed +const unsigned int DCCinst_step2 = 0x72; //step 2 +const unsigned int DCCinst_step4 = 0x73; //step 4 +unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed +const unsigned int DCCinst_step13 = 0x78; //step 13 1/2 speed +const unsigned int DCCinst_step20 = 0x75; //step 20 3/4 speed +const unsigned int DCCinst_step28 = 0x7F; //step 28 Full speed //stop -const unsigned int DCCinst_stop = 01100000; //forward and stop 01100000 +const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000 -//read switch out +/* Functions definition */ +void readVoltage(); +bool readDetector(); +bool readSwitch(); +void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command + -//read signal - -//display message +/*---------------------------------------------------------------------------- +Main +*----------------------------------------------------------------------------*/ +int main() { + /*sw1.rise(&testInterupt2); + sw1.fall(&testInterupt); + inter0.rise(&riseFunction); + inter1.rise(&riseFunction);*/ + while(1){ + + /* Train Definitions */ + unsigned int DCCaddress_darkRed = 0x01; + //unsigned int DCCaddress_lightRed = 0x03; + lcd.printf("Start the journey"); + wait(0.2); + if(readSwitch()){ + lcd.cls(); + lcd.printf("Forward"); + DCC_send_command(DCCaddress_darkRed,DCCinst_step13,10); // forward half speed train address 3 + }else{ + lcd.cls(); + lcd.printf("Reverse"); + DCC_send_command(DCCaddress_darkRed,DCCinst_reverse,10); // forward half speed train address 3 + } + + + } +} /*---------------------------------------------------------------------------- Functions *----------------------------------------------------------------------------*/ -/* Functions definition */ -void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data); // Write an I2C register -uint8_t mcpReadReg(uint8_t address, uint8_t reg); // Read an I2C register -void initMcp0(void); // Initialization of MCP23017 with address bits 000 (track sensors) -void testMcp0(void); // Test of MCP23017 with address bits 000 -void readVoltage(); -bool readDetector(); -void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command - +/* void testInterupt(){ lcd.cls(); myled = 1; @@ -106,49 +118,22 @@ void fallFunction(){ lcd.printf("In fall function"); lcd.printf("%d", inter1.read()); -} +}*/ + + + -int main() { - /*sw1.rise(&testInterupt2); - sw1.fall(&testInterupt); - inter0.rise(&riseFunction); - inter1.rise(&riseFunction);*/ - while(1){ - - //wait(0.25); - unsigned int DCCaddress = 0x03; - unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed - - DCC_send_command(DCCaddress_darkRed,DCCinst_step6,400); // forward half speed train address 3 - lcd.printf("Forward"); - //unsigned int DCCaddress_darkRed = 0x01; - /*if(readDetector()) - { - DCC_send_command(DCCaddress_darkRed,DCCinst_stop,400); // forward half speed train address 3 - lcd.printf("Stop"); - wait(2); - } - else - lcd.printf("Go forward");*/ - //lcd.cls(); - //lcd.printf("Forward"); - //wait(2); - DCC_send_command(DCCaddress_darkRed,DCCinst_stop,400); // forward half speed train address 3 - lcd.printf("Stop"); - wait(2); - //wait(1.5); - //lcd.cls(); - //lcd.printf("Stop"); - //wait(1); - //readVoltage(); - //myled=1; - } +bool readSwitch(){ + int val = sw1.read(); + lcd.printf("%d", val); + if(val == 1) + return true; + else + return false; } bool readDetector(){ - int val1 = detect_2.read(); - - + /*int val1 = detect_2.read(); if(val1 == 1) { lcd.cls(); @@ -156,10 +141,8 @@ lcd.printf("%d", val1); return true; } - int val2 = detect_21.read(); - if(val2 == 1) { lcd.cls(); @@ -167,6 +150,7 @@ lcd.printf("%d", val2); return true; } + int val3 = detect_22.read(); if(val3 == 1) { @@ -174,53 +158,18 @@ lcd.printf("Detect 22: "); lcd.printf("%d", val3); return true; - } - return false; + }*/ + // return false; } void readVoltage(){ - float f = Ain.read(); //Read voltage value + /*float f = Ain.read(); //Read voltage value float Vin = f *3.3; lcd.printf("%.2f", Vin); wait(0.1); - lcd.printf("\n"); -} - -void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data){ - char cmd[2];cmd[0] = reg; - cmd[1] = data; - i2c.write(address, cmd, 2); // Write 2 bytes to device on specified address -} - -uint8_t mcpReadReg(uint8_t address, uint8_t reg){ - char cmd[1]; - cmd[0] = reg; - i2c.write(address, cmd, 1); // Write address - i2c.read(address, cmd, 1); // Read value (one byte) - - return cmd[0]; // Return the read value + lcd.printf("\n");*/ } -/* Initialization of the MCP23017 for the track sensors. -We should enable the MCP23017 interrupts here because the sensors will only give a short pulse to the MCP23017*/ -void initMcp0(void){ - mcpWriteReg(addr0, MCP_IODIRA, 0xff); // All inputs - mcpWriteReg(addr0, MCP_IODIRB, 0xff); // All inputs -} - -/* Test of the track sensors -* This does not use interrupts so the chance of actually detecting a train is very, very low -* With some patience it's good enough to test the hardware though. -*/ -void testMcp0(void) { - uint16_t sensors; // Put both ports in the sensor variable - sensors = (mcpReadReg(addr0, MCP_GPIOB) << 8) | mcpReadReg(addr0, MCP_GPIOA); - // Flip all bits, since the sensors are active low and it gives me headaches - // You could also set the IPOLA / IPOLB registers instead (see page 13 of the MCP23017 datasheet) - sensors = ~sensors; - lcd.printf("sensors: %04X\n", sensors); - } - void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) { unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type