Jasmine Karlsson
/
train_rail
New project
Diff: main.cpp
- Revision:
- 22:c024b20a0e2d
- Parent:
- 21:31647d80614f
- Child:
- 23:bb57966cb776
--- a/main.cpp Tue Jun 12 15:02:44 2018 +0000 +++ b/main.cpp Wed Jun 13 09:06:55 2018 +0000 @@ -5,6 +5,7 @@ #include "Switch.h" #include "Track.h" #include "Detector.h" +#include "MCP23017.h" //Board 1 /*---------------------------------------------------------------------------- @@ -35,8 +36,9 @@ DigitalIn sw4(p12); //InterruptIn sw1(p5); -InterruptIn inter0(p13); -InterruptIn inter1(p14); +InterruptIn int0(p13); +InterruptIn int1(p14); +I2C i2c(p28, p27); MCP23017 *mcp; /*---------------------------------------------------------------------------- @@ -77,7 +79,12 @@ *----------------------------------------------------------------------------*/ bool readDetector(DigitalIn detector); bool readSwitch(DigitalIn theSwitch); -void initialize_mcp(); +//void initialize_mcp(); +void on_int0_change(); +void on_int1_change(); +void init_mcp(); +void init(); + //Trains @@ -99,23 +106,32 @@ sw1.fall(&testInterupt); inter0.rise(&riseFunction); inter1.rise(&riseFunction);*/ - - initialize_mcp(); - int res[2] = {2,4}; + lcd.printf("Start journey"); + init_mcp(); + init(); + /*int res[2] = {2,4}; dect.reserveDetector(res); //Run trains counterclockwise and reserve the int res2[2] = {12,13}; dect.reserveDetector(res2);//detectors in front - + */ while(1){ + if(readSwitch(sw1)){ //Control switches - switch1.switchOn(); + /*switch1.switchOn(); lcd.printf("Switch 1 on"); //switch2.switchOn(); // Need to turn it off immediately!!! switch3.switchOn(); - switch4.switchOn(); + switch4.switchOn();*/ + + // Clear current interrupts + mcp->_read(GPIOA); + mcp->_read(GPIOB); + // Register callbacks + int0.fall(&on_int0_change); + int1.fall(&on_int1_change); } - else{ + /*else{ //DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10); switch1.switchOff(); //switch2.switchOff(); @@ -140,8 +156,8 @@ else silverTrain.changeSpeed(DCCinst_step2); - } -} + } + } */ /*lcd.printf("Start the journey"); time_t tstart, tend; @@ -150,6 +166,7 @@ lcd.printf("%d", data); wait(2); */ + } } /*---------------------------------------------------------------------------- @@ -158,7 +175,7 @@ bool testInterupt(){ - int pos = mcp->readRegister(0x12); + int pos = 0;// mcp->readRegister(0x12); lcd.cls(); //Check which train got the interupt Train *temptrain; @@ -179,6 +196,7 @@ switch(pos){ case 0: case 1: + //Slow down case 2: //IF Clock 2->1 //Disable Switch2 @@ -335,7 +353,7 @@ bool readSwitch(DigitalIn theSwitch){ int val = theSwitch.read(); - lcd.printf("%d", val); + //lcd.printf("%d", val); if(val == 1) return true; else @@ -354,21 +372,53 @@ return false; } -void initialize_mcp(){ - mcp = new MCP23017(p28, p27, 0x40); - mcp->reset(); - mcp->writeRegister(0x00, (unsigned char )0xff); - mcp->writeRegister(0x01, (unsigned char )0xff); - mcp->writeRegister(0x02, (unsigned char )0x00); - mcp->writeRegister(0x03, (unsigned char )0x00); - mcp->writeRegister(0x04, (unsigned char )0xff); - mcp->writeRegister(0x05, (unsigned char )0xff); - mcp->writeRegister(0x06, (unsigned char )0xff); - mcp->writeRegister(0x07, (unsigned char )0xff); - mcp->writeRegister(0x08, (unsigned char )0xff); - mcp->writeRegister(0x09, (unsigned char )0xff); - mcp->writeRegister(0x0a, (unsigned char )0x42); - mcp->writeRegister(0x0b, (unsigned char )0x42); - mcp->writeRegister(0x0c, (unsigned char )0x00); - mcp->writeRegister(0x0d, (unsigned char )0x00); +void init_mcp() { + // Initialisation of MCP registers,documentation on registers is availableatNiels/Jacco/Natalia + mcp = new MCP23017(i2c, 0x40); + mcp->_write(IODIRA, (unsigned char )0xff); + mcp->_write(IODIRB, (unsigned char )0xff); + mcp->_write(IPOLA, (unsigned char )0x00); + mcp->_write(IPOLB, (unsigned char )0x00); + mcp->_write(DEFVALA, (unsigned char )0xff); + mcp->_write(DEFVALB, (unsigned char )0xff); + mcp->_write(INTCONA, (unsigned char )0xff); + mcp->_write(INTCONB, (unsigned char )0xff); + mcp->_write(IOCONA, (unsigned char )0x2); + mcp->_write(IOCONB, (unsigned char )0x2); + mcp->_write(GPPUA, (unsigned char )0xff); + mcp->_write(GPPUB, (unsigned char )0xff); } + +void init() { + // Clear current interrupts + mcp->_read(GPIOA); + mcp->_read(GPIOB); + // Register callbacks + int0.fall(&on_int0_change); + int1.fall(&on_int1_change); + // Enable interrupts on MCP + mcp->_write(GPINTENA, (unsigned char )0xff); + mcp->_write(GPINTENB, (unsigned char )0xff);// Ready to go! +} + +void on_int0_change() { + lcd.printf("In int0)"); + // In your code, you might want to move this logic out of the interrupt routine + // (for instance, by setting a flag and checking this flagin your main-loop) + wait_us(200000); + int sensor_data = mcp->_read(INTCAPA); + // The only thing we do with the interrupt signal is printing it + lcd.cls(); + lcd.printf("GOT INT0 (0x%x)\n", sensor_data); +} + +void on_int1_change() { + lcd.printf("In int1)"); + // In your code, you might want to move this logic out of the interrupt routine + // (for instance, by setting a flag and checking this flagin your main-loop) + wait_us(200000); + int sensor_data = mcp->_read(INTCAPB); + // The only thing we do with the interrupt signal is printing it + lcd.cls(); + lcd.printf("GOT INT0 (1) (0x%x)\n", sensor_data); +} \ No newline at end of file