Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 24:1d71dd8778c4
- Parent:
- 23:3fe9e76e06f2
- Child:
- 25:a42a1ed4d8e9
--- a/main.cpp Mon Jun 11 12:23:24 2018 +0000 +++ b/main.cpp Mon Jun 11 12:31:56 2018 +0000 @@ -54,6 +54,7 @@ //LED2 - p30 DigitalOut greenled(p30); +//MBED LEDS DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); @@ -61,6 +62,8 @@ //MCP MCP23017 *mcp; + + //**************** FUNCTIONS FOR DENVER TRAIN ****************// @@ -73,9 +76,10 @@ buzz = 0; } + /** - -*/ +* +**/ void initialize_mcp(){ mcp = new MCP23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 @@ -96,6 +100,10 @@ mcp->writeRegister(0x0d, (unsigned char )0x00); } + +/** +* +**/ void interrupt0(){ int data = mcp->readRegister(GPIO); lcd.cls(); @@ -106,6 +114,10 @@ } + +/** +* +**/ void interrupt1(){ int data = mcp->readRegister(GPIO); lcd.cls(); @@ -116,31 +128,42 @@ } +/** +* +**/ 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 unsigned __int64 temp_command = 0x0000000000000000; unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start unsigned int error = 0x00; //error byte + //calculate error detection byte with xor error = address ^ inst; + //combine packet bits in basic DCC format command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; //printf("\n\r %llx \n\r",command); + int i=0; -//repeat DCC command lots of times + //repeat DCC command lots of times while(i < repeat_count) { + temp_command = command; -//loops through packet bits encoding and sending out digital pulses for a DCC command + //loops through packet bits encoding and sending out digital pulses for a DCC command for (int j=0; j<64; j++) { - if((temp_command&0x8000000000000000)==0) { //test packet bit + + if((temp_command&0x8000000000000000)==0) { + //test packet bit //send data for a "0" bit Track=0; wait_us(100); Track=1; wait_us(100); //printf("0011"); - } else { + + }else{ + //send data for a "1"bit Track=0; wait_us(58); @@ -155,6 +178,10 @@ } } + +/** +* +**/ void flipSwitch(int switchId, int times){ //Command variables for Switches using DCC @@ -166,9 +193,7 @@ ///00100 to flip the third switch SW3 (F3 active) ///01000 to flip the fourth switch SW4 (F4 active) - /// 00011 maybe flips 1 and 2 - - //example - 111111 0 00000101 0 10000000 0 10000101 1 - idleç + //example - 111111 0 00000101 0 10000000 0 10000101 1 - idle unsigned int SWBflip = 0x80; //No switches -- idle @@ -186,8 +211,7 @@ SWBflip = 0x88; break; default: - break; - + break; } //Security measure not to burn the switch. @@ -195,15 +219,13 @@ DCC_send_command(SWBaddress,SWBflip,times); } - } -/* -Check the swithces of the box. If they have 1 value the railSiwtches flip. -*/ - +/** +* Check the swithces of the box. If they have 1 value the railSiwtches flip. +**/ void switching(string sw1,string sw2, string sw3, string sw4){ string strInst = "1000" + sw1 + sw2 + sw3 + sw4; @@ -211,30 +233,42 @@ //DCC_send_command(0x06,inst,5); - - + } + +/** +* +**/ void checkSwitch(){ + if(switch1 == 1){ + lcd.cls(); lcd.printf("Switch 1 ON - SW1"); flipSwitch(1,5); //IDLE + }else if(switch2 == 1){ - lcd.cls(); - lcd.printf("Switch 2 ON - SW2"); - flipSwitch(2,5); - } else if(switch3 == 0){ - lcd.cls(); - lcd.printf("Switch 3 ON - SW3"); - flipSwitch(3,5); - } else if(switch4 == 0){ - lcd.cls(); - lcd.printf("Switch 4 ON - IDLE"); - flipSwitch(0,5); + + lcd.cls(); + lcd.printf("Switch 2 ON - SW2"); + flipSwitch(2,5); + + }else if(switch3 == 0){ + + lcd.cls(); + lcd.printf("Switch 3 ON - SW3"); + flipSwitch(3,5); + + }else if(switch4 == 0){ + + lcd.cls(); + lcd.printf("Switch 4 ON - IDLE"); + flipSwitch(0,5); + } } -} + /* void checkSwitch(){ int switches[] = [0,0,0,0] @@ -266,6 +300,8 @@ */ + + //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// int main()