Denver trai project

Dependencies:   mbed TextLCD

Revision:
33:24ce12dec157
Parent:
32:e5b732fb8e65
Child:
34:c9ab2a987734
--- a/main.cpp	Tue Jun 12 12:15:05 2018 +0000
+++ b/main.cpp	Wed Jun 13 12:02:48 2018 +0000
@@ -49,6 +49,7 @@
 
 ///p27
 ///p28
+I2C i2c(p28,p27);
 
 //LED1 - p29
 DigitalOut redled(p29);
@@ -76,6 +77,7 @@
 
 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
 const unsigned int DCCinst_forward = 0x68; //forward half speed
+const unsigned int DCCinst_forward_slow = 0x66; //forward half speed
 const unsigned int DCCinst_reverse = 0x48; //reverse half speed
 const unsigned int DCCinst_stop = 0x50;    //stop the train
 
@@ -129,39 +131,34 @@
 *
 **/
 void initialize_mcp(){
-    mcp = new MCP23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 
+    mcp = new MCP23017(i2c,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 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);
+    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);    
+    
 }
 
-
 /**
 *
 *Method to catch interrupts 0
 *
 **/
-void interrupt0(){
+void on_int0_change(){
      
-     int data = mcp->readRegister(GPIO);
+     wait_us(2000);
+     int sensor_data = mcp->_read(INTCAPA);
      lcd.cls();
-     lcd.printf("int0 %x",data);
-     redled = 1;
-     wait(0.2);
-     redled = 0;
+     lcd.printf("int0 0x%x",sensor_data);
      
 }
 
@@ -171,17 +168,27 @@
 *Method to catch interrupts 1
 *
 **/
-void interrupt1(){
+void on_int1_change(){
     
-    int data = mcp->readRegister(GPIO);
-    lcd.cls();
-    lcd.printf("int1 %x",data);
-    greenled = 1;
-    wait(0.2);
-    greenled = 0;
+    wait_us(2000);
+     int sensor_data = mcp->_read(INTCAPB);
+     lcd.cls();
+     lcd.printf("int1 0x%x",sensor_data);
 }
 
 
+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! 
+  }
+
+
+
+
 /**
 *
 *Print the positions where there is a 0
@@ -366,6 +373,7 @@
     led1 = 1;
     
     initialize_mcp();   //mcp initialization for interrupts before train running
+    init();
     
     //Train light routine to start running
     DCC_send_command(DCCaddressDR,DCC_func_lighton,200); // turn light on full
@@ -382,7 +390,8 @@
         
         checkSwitch();  //Checks for switch commands everytime.  
         
-        if(station == 1){      //If train is on the sensor at the middle of the station it stops and displays LCD text.
+        if(1==0){
+        //if(station == 1){      //If train is on the sensor at the middle of the station it stops and displays LCD text.
             
             lcd.cls();
             lcd.printf("All aboard\n mind the gap");
@@ -390,7 +399,7 @@
             lcd.cls();
             
         }else{
-            DCC_send_command(DCCaddressDR,DCCinst_forward,1); // Forward half speed train addres DARK-RED             
+            DCC_send_command(DCCaddressDR,DCCinst_forward_slow,1); // Forward half speed train addres DARK-RED             
             DCC_send_command(DCCaddressLR,DCCinst_forward,1); // Forward half speed train address LIGHT-RED
         } 
     }