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.
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 int interrupted = 0; 00018 00019 DigitalOut Track(p20); 00020 DigitalOut led(LED1); 00021 InterruptIn int0(p8); 00022 InterruptIn int1(p9); 00023 MCP23017 *mcp; 00024 00025 void initialize_mcp() { 00026 mcp = new MCP23017(p28, p27, 0x40); // Connect to mbed int pins 27 and 28, 00027 // MCP address is 0x40 00028 mcp->reset(); 00029 mcp->writeRegister(0x00, (unsigned char )0xff); 00030 mcp->writeRegister(0x01, (unsigned char )0xff); 00031 mcp->writeRegister(0x02, (unsigned char )0x00); 00032 mcp->writeRegister(0x03, (unsigned char )0x00); 00033 mcp->writeRegister(0x04, (unsigned char )0xff); 00034 mcp->writeRegister(0x05, (unsigned char )0xff); 00035 mcp->writeRegister(0x06, (unsigned char )0xff); 00036 mcp->writeRegister(0x07, (unsigned char )0xff); 00037 mcp->writeRegister(0x08, (unsigned char )0xff); 00038 mcp->writeRegister(0x09, (unsigned char )0xff); 00039 mcp->writeRegister(0x0a, (unsigned char )0x42); 00040 mcp->writeRegister(0x0b, (unsigned char )0x42); 00041 mcp->writeRegister(0x0c, (unsigned char )0x00); 00042 mcp->writeRegister(0x0d, (unsigned char )0x00); 00043 00044 wait_ms(100); 00045 mcp->readRegister(GPIO); 00046 } 00047 00048 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count){ 00049 unsigned __int64 command = 0x0000000000000000; // this is a 64-bit integer type, due to the packet size 00050 unsigned __int64 temp_command = 0x0000000000000000; 00051 unsigned __int64 prefix = 0x3FFF; // 14 1 bits as in the preamble of the data packet 00052 unsigned int error = 0x00; // error byte 00053 //calculate error detection byte with xor 00054 error = address ^ inst; 00055 // combine packet bits in basic DCC format 00056 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; 00057 00058 int i = 0; 00059 00060 // repeat DCC command lots of times 00061 while(i < repeat_count) { 00062 temp_command = command; 00063 00064 //loops throught packet bits enconding and sending out digital pulses for a DCC command 00065 for (int j=0; j<64; j++) { 00066 if((temp_command&0x8000000000000000)==0) { //test packet bit 00067 00068 Track=0; 00069 wait_us(100); 00070 Track=1; 00071 wait_us(100); 00072 00073 } else { 00074 00075 Track=0; 00076 wait_us(58); 00077 Track=1; 00078 wait_us(58); 00079 00080 } 00081 00082 temp_command = temp_command<<1; 00083 } 00084 i++; 00085 } 00086 } 00087 00088 unsigned __int64 create_instruction(){ 00089 unsigned __int64 instruction = 0x0; 00090 unsigned __int64 direction = FORWARD; 00091 unsigned __int64 lights = LIGHTON; 00092 unsigned __int64 speed = NORM; 00093 00094 return instruction = (0x01<<6)|(direction<<5)|(lights<<4)|speed; 00095 } 00096 00097 void interrupt_handler(){ 00098 interrupted = 1; 00099 } 00100 00101 int main(){ 00102 initialize_mcp(); 00103 int0.fall(&interrupt_handler); 00104 int1.fall(&interrupt_handler); 00105 unsigned __int64 instruction = 0x0; 00106 00107 while(1){ 00108 if(interrupted){ 00109 printf("interrupted\n"); 00110 mcp->readRegister(INTCAP); 00111 int data = mcp->readRegister(GPIO); // value of GPIO is 0x12 00112 //modify instructions 00113 printf("%d", data); 00114 led = 1; 00115 interrupted = 0; 00116 } else { 00117 led = 0; 00118 } 00119 instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|NORM; 00120 DCC_send_command(train3, instruction, def_repeat_count); 00121 } 00122 } 00123 00124 /* 00125 Interrupts 00126 INT0 - pin 8 00127 00128 clock en data pins for I2C communication - data sync - hier lezen na een interrupt 00129 SDA - pin 9 00130 SCL - pin 10 00131 00132 Sending command signals to train tracks 00133 DAT - pin 20 00134 EN - VOUT 00135 00136 00137 instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|STOP; 00138 DCC_send_command(train3, instruction, def_repeat_count); 00139 */
Generated on Thu Nov 17 2022 13:14:10 by
1.7.2