SWTP / Mbed 2 deprecated trains3

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MCP23017.h"
00003 
00004 const unsigned __int64 FORWARD = 1;
00005 const unsigned __int64 BACKWARD = 0;
00006 const unsigned __int64 LIGHTON = 1;
00007 const unsigned __int64 LIGHTOFF = 0;
00008 const unsigned __int64 SLOW = 0x4; //0100 = step 5
00009 const unsigned __int64 NORM = 0x9; //1001 = step 15
00010 const unsigned __int64 FAST = 0xE; //1110 = step 25
00011 const unsigned __int64 STOP = 0x1; //0001 = E-STOP
00012 
00013 const unsigned __int64 train1 = 0x1; 
00014 const unsigned __int64 train3 = 0x3;
00015 const unsigned __int64 def_repeat_count = 5;
00016 
00017 const int addr0 = 0x20 << 1;
00018 
00019 int interrupted = 0;
00020 
00021 I2C i2c(p28, p27);
00022 
00023 DigitalOut Track(p20);
00024 DigitalOut led(LED1);
00025 InterruptIn int0(p25);
00026 //InterruptIn int1(p9);
00027 
00028 /* Write a register on an I2C device */
00029 void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data){
00030     char cmd[2];
00031     cmd[0] = reg;
00032     cmd[1] = data;
00033     i2c.write(address, cmd, 2);   // Write 2 bytes to device on specified address
00034 }
00035 
00036 /* Read a register on a I2C device */
00037 uint8_t mcpReadReg(uint8_t address, uint8_t reg){
00038     char cmd[1];
00039     cmd[0] = reg;
00040     i2c.write(address, cmd, 1);     // Write address 
00041     i2c.read(address, cmd, 1);      // Read value (one byte)
00042     return cmd[0];                  // Return the read value
00043 }
00044 
00045 void interrupt_handler(){
00046      interrupted = 1;
00047 }
00048 
00049 void initialize_mcp() {
00050     /*
00051     printf("%x\n", mcpReadReg(addr0, MCP_IODIRA));
00052     printf("%x\n", mcpReadReg(addr0, MCP_IODIRB));
00053     printf("%x\n", mcpReadReg(addr0, MCP_IPOLA));
00054     printf("%x\n", mcpReadReg(addr0, MCP_IPOLB));
00055     printf("%x\n", mcpReadReg(addr0, MCP_GPINTENA));
00056     printf("%x\n", mcpReadReg(addr0, MCP_GPINTENB));
00057     printf("%x\n", mcpReadReg(addr0, MCP_DEFVALA));
00058     printf("%x\n", mcpReadReg(addr0, MCP_DEFVALB));
00059     printf("%x\n", mcpReadReg(addr0, MCP_INTCONA));
00060     printf("%x\n", mcpReadReg(addr0, MCP_INTCONB));
00061     printf("%x\n", mcpReadReg(addr0, MCP_IOCON));
00062     printf("%x\n", mcpReadReg(addr0, MCP_IOCON_MIRROR));
00063     printf("%x\n", mcpReadReg(addr0, MCP_GPPUA));
00064     printf("%x\n", mcpReadReg(addr0, MCP_GPPUB));
00065     printf("%x\n", mcpReadReg(addr0, MCP_INTFA));
00066     printf("%x\n", mcpReadReg(addr0, MCP_INTFB));
00067     printf("%x\n", mcpReadReg(addr0, MCP_INTCAPA));
00068     printf("%x\n", mcpReadReg(addr0, MCP_INTCAPB));
00069     printf("%x\n", mcpReadReg(addr0, MCP_GPIOA));
00070     printf("%x\n", mcpReadReg(addr0, MCP_GPIOB));
00071     printf("%x\n", mcpReadReg(addr0, MCP_OLATA));
00072     printf("%x\n", mcpReadReg(addr0, MCP_OLATB));
00073     */
00074     
00075     mcpWriteReg(addr0, MCP_IODIRA, 0xff);     // All inputs 
00076     mcpWriteReg(addr0, MCP_IODIRB, 0xff);     // All inputs
00077     mcpWriteReg(addr0, MCP_GPINTENA, 0xff);
00078     mcpWriteReg(addr0, MCP_GPINTENB, 0xff);
00079     mcpWriteReg(addr0, MCP_INTCONA, 0xff);
00080     mcpWriteReg(addr0, MCP_INTCONB, 0xff);
00081     mcpWriteReg(addr0, MCP_DEFVALA, 0xff);
00082     mcpWriteReg(addr0, MCP_DEFVALB, 0xff);
00083     mcpWriteReg(addr0, MCP_GPIOA, 0x00);
00084     mcpWriteReg(addr0, MCP_GPIOB, 0x00);
00085     
00086     int0.fall(&interrupt_handler);
00087     //int1.fall(&interrupt_handler);
00088     
00089 }
00090 
00091 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count){
00092     unsigned __int64 command = 0x0000000000000000; // this is a 64-bit integer type, due to the packet size
00093     unsigned __int64 temp_command = 0x0000000000000000;
00094     unsigned __int64 prefix = 0x3FFF; // 14 1 bits as in the preamble of the data packet
00095     unsigned int error = 0x00; // error byte
00096     //calculate error detection byte with xor
00097     error = address ^ inst;
00098     // combine packet bits in basic DCC format
00099     command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
00100 
00101     int i = 0;
00102 
00103     // repeat DCC command lots of times
00104      while(i < repeat_count) {
00105         temp_command = command;
00106 
00107     //loops throught packet bits enconding and sending out digital pulses for a DCC command
00108         for (int j=0; j<64; j++) {
00109             if((temp_command&0x8000000000000000)==0) { //test packet bit
00110 
00111                 Track=0;
00112                 wait_us(100);
00113                 Track=1;
00114                 wait_us(100);
00115 
00116             } else {
00117 
00118                 Track=0;
00119                 wait_us(58);
00120                 Track=1;
00121                 wait_us(58);
00122 
00123             }
00124 
00125             temp_command = temp_command<<1;
00126         }
00127         i++;
00128     }
00129 }
00130 
00131 unsigned __int64 create_instruction(){
00132     unsigned __int64 instruction = 0x0;
00133     unsigned __int64 direction = FORWARD;
00134     unsigned __int64 lights = LIGHTON;
00135     unsigned __int64 speed = NORM;   
00136     
00137     return instruction = (0x01<<6)|(direction<<5)|(lights<<4)|speed;
00138 }
00139 
00140 int main(){
00141     initialize_mcp();
00142     unsigned __int64 instruction = 0x0;  
00143      
00144     while(1){  
00145         if(interrupted){
00146             uint16_t data = (mcpReadReg(addr0, MCP_GPIOB) << 8) | mcpReadReg(addr0, MCP_GPIOA); 
00147             //mcpReadReg(addr0, MCP_INTCAPA);
00148             //mcpReadReg(addr0, MCP_INTCAPB);
00149             //modify instructions
00150             printf("%x interrupted\n", data);
00151             interrupted = 0; 
00152         }
00153         
00154         instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|FAST;
00155         DCC_send_command(train3, instruction, def_repeat_count);
00156     }
00157 }
00158 
00159 /*
00160 Interrupts
00161 INT0 - pin 8
00162 
00163 clock en data pins for I2C communication - data sync - hier lezen na een interrupt
00164 SDA - pin 9
00165 SCL - pin 10
00166 
00167 Sending command signals to train tracks
00168 DAT - pin 20
00169 EN - VOUT
00170 
00171 
00172         instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|STOP;
00173         DCC_send_command(train3, instruction, def_repeat_count);
00174 */