![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
initialisatie met niels zn functies
Dependencies: mbed
main.cpp@0:e206ee5c4d91, 2017-06-08 (annotated)
- Committer:
- rcwinder
- Date:
- Thu Jun 08 15:19:14 2017 +0000
- Revision:
- 0:e206ee5c4d91
initialisatie met niels zn functies
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rcwinder | 0:e206ee5c4d91 | 1 | #include "mbed.h" |
rcwinder | 0:e206ee5c4d91 | 2 | #include "MCP23017.h" |
rcwinder | 0:e206ee5c4d91 | 3 | |
rcwinder | 0:e206ee5c4d91 | 4 | const unsigned __int64 FORWARD = 1; |
rcwinder | 0:e206ee5c4d91 | 5 | const unsigned __int64 BACKWARD = 0; |
rcwinder | 0:e206ee5c4d91 | 6 | const unsigned __int64 LIGHTON = 1; |
rcwinder | 0:e206ee5c4d91 | 7 | const unsigned __int64 LIGHTOFF = 0; |
rcwinder | 0:e206ee5c4d91 | 8 | const unsigned __int64 SLOW = 0x4; //0100 = step 5 |
rcwinder | 0:e206ee5c4d91 | 9 | const unsigned __int64 NORM = 0x9; //1001 = step 15 |
rcwinder | 0:e206ee5c4d91 | 10 | const unsigned __int64 FAST = 0xE; //1110 = step 25 |
rcwinder | 0:e206ee5c4d91 | 11 | const unsigned __int64 STOP = 0x1; //0001 = E-STOP |
rcwinder | 0:e206ee5c4d91 | 12 | |
rcwinder | 0:e206ee5c4d91 | 13 | const unsigned __int64 train1 = 0x1; |
rcwinder | 0:e206ee5c4d91 | 14 | const unsigned __int64 train3 = 0x3; |
rcwinder | 0:e206ee5c4d91 | 15 | const unsigned __int64 def_repeat_count = 5; |
rcwinder | 0:e206ee5c4d91 | 16 | |
rcwinder | 0:e206ee5c4d91 | 17 | const int addr0 = 0x20 << 1; |
rcwinder | 0:e206ee5c4d91 | 18 | |
rcwinder | 0:e206ee5c4d91 | 19 | int interrupted = 0; |
rcwinder | 0:e206ee5c4d91 | 20 | |
rcwinder | 0:e206ee5c4d91 | 21 | I2C i2c(p28, p27); |
rcwinder | 0:e206ee5c4d91 | 22 | |
rcwinder | 0:e206ee5c4d91 | 23 | DigitalOut Track(p20); |
rcwinder | 0:e206ee5c4d91 | 24 | DigitalOut led(LED1); |
rcwinder | 0:e206ee5c4d91 | 25 | InterruptIn int0(p25); |
rcwinder | 0:e206ee5c4d91 | 26 | //InterruptIn int1(p9); |
rcwinder | 0:e206ee5c4d91 | 27 | |
rcwinder | 0:e206ee5c4d91 | 28 | /* Write a register on an I2C device */ |
rcwinder | 0:e206ee5c4d91 | 29 | void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data){ |
rcwinder | 0:e206ee5c4d91 | 30 | char cmd[2]; |
rcwinder | 0:e206ee5c4d91 | 31 | cmd[0] = reg; |
rcwinder | 0:e206ee5c4d91 | 32 | cmd[1] = data; |
rcwinder | 0:e206ee5c4d91 | 33 | i2c.write(address, cmd, 2); // Write 2 bytes to device on specified address |
rcwinder | 0:e206ee5c4d91 | 34 | } |
rcwinder | 0:e206ee5c4d91 | 35 | |
rcwinder | 0:e206ee5c4d91 | 36 | /* Read a register on a I2C device */ |
rcwinder | 0:e206ee5c4d91 | 37 | uint8_t mcpReadReg(uint8_t address, uint8_t reg){ |
rcwinder | 0:e206ee5c4d91 | 38 | char cmd[1]; |
rcwinder | 0:e206ee5c4d91 | 39 | cmd[0] = reg; |
rcwinder | 0:e206ee5c4d91 | 40 | i2c.write(address, cmd, 1); // Write address |
rcwinder | 0:e206ee5c4d91 | 41 | i2c.read(address, cmd, 1); // Read value (one byte) |
rcwinder | 0:e206ee5c4d91 | 42 | return cmd[0]; // Return the read value |
rcwinder | 0:e206ee5c4d91 | 43 | } |
rcwinder | 0:e206ee5c4d91 | 44 | |
rcwinder | 0:e206ee5c4d91 | 45 | void interrupt_handler(){ |
rcwinder | 0:e206ee5c4d91 | 46 | interrupted = 1; |
rcwinder | 0:e206ee5c4d91 | 47 | } |
rcwinder | 0:e206ee5c4d91 | 48 | |
rcwinder | 0:e206ee5c4d91 | 49 | void initialize_mcp() { |
rcwinder | 0:e206ee5c4d91 | 50 | /* |
rcwinder | 0:e206ee5c4d91 | 51 | printf("%x\n", mcpReadReg(addr0, MCP_IODIRA)); |
rcwinder | 0:e206ee5c4d91 | 52 | printf("%x\n", mcpReadReg(addr0, MCP_IODIRB)); |
rcwinder | 0:e206ee5c4d91 | 53 | printf("%x\n", mcpReadReg(addr0, MCP_IPOLA)); |
rcwinder | 0:e206ee5c4d91 | 54 | printf("%x\n", mcpReadReg(addr0, MCP_IPOLB)); |
rcwinder | 0:e206ee5c4d91 | 55 | printf("%x\n", mcpReadReg(addr0, MCP_GPINTENA)); |
rcwinder | 0:e206ee5c4d91 | 56 | printf("%x\n", mcpReadReg(addr0, MCP_GPINTENB)); |
rcwinder | 0:e206ee5c4d91 | 57 | printf("%x\n", mcpReadReg(addr0, MCP_DEFVALA)); |
rcwinder | 0:e206ee5c4d91 | 58 | printf("%x\n", mcpReadReg(addr0, MCP_DEFVALB)); |
rcwinder | 0:e206ee5c4d91 | 59 | printf("%x\n", mcpReadReg(addr0, MCP_INTCONA)); |
rcwinder | 0:e206ee5c4d91 | 60 | printf("%x\n", mcpReadReg(addr0, MCP_INTCONB)); |
rcwinder | 0:e206ee5c4d91 | 61 | printf("%x\n", mcpReadReg(addr0, MCP_IOCON)); |
rcwinder | 0:e206ee5c4d91 | 62 | printf("%x\n", mcpReadReg(addr0, MCP_IOCON_MIRROR)); |
rcwinder | 0:e206ee5c4d91 | 63 | printf("%x\n", mcpReadReg(addr0, MCP_GPPUA)); |
rcwinder | 0:e206ee5c4d91 | 64 | printf("%x\n", mcpReadReg(addr0, MCP_GPPUB)); |
rcwinder | 0:e206ee5c4d91 | 65 | printf("%x\n", mcpReadReg(addr0, MCP_INTFA)); |
rcwinder | 0:e206ee5c4d91 | 66 | printf("%x\n", mcpReadReg(addr0, MCP_INTFB)); |
rcwinder | 0:e206ee5c4d91 | 67 | printf("%x\n", mcpReadReg(addr0, MCP_INTCAPA)); |
rcwinder | 0:e206ee5c4d91 | 68 | printf("%x\n", mcpReadReg(addr0, MCP_INTCAPB)); |
rcwinder | 0:e206ee5c4d91 | 69 | printf("%x\n", mcpReadReg(addr0, MCP_GPIOA)); |
rcwinder | 0:e206ee5c4d91 | 70 | printf("%x\n", mcpReadReg(addr0, MCP_GPIOB)); |
rcwinder | 0:e206ee5c4d91 | 71 | printf("%x\n", mcpReadReg(addr0, MCP_OLATA)); |
rcwinder | 0:e206ee5c4d91 | 72 | printf("%x\n", mcpReadReg(addr0, MCP_OLATB)); |
rcwinder | 0:e206ee5c4d91 | 73 | */ |
rcwinder | 0:e206ee5c4d91 | 74 | |
rcwinder | 0:e206ee5c4d91 | 75 | mcpWriteReg(addr0, MCP_IODIRA, 0xff); // All inputs |
rcwinder | 0:e206ee5c4d91 | 76 | mcpWriteReg(addr0, MCP_IODIRB, 0xff); // All inputs |
rcwinder | 0:e206ee5c4d91 | 77 | mcpWriteReg(addr0, MCP_GPINTENA, 0xff); |
rcwinder | 0:e206ee5c4d91 | 78 | mcpWriteReg(addr0, MCP_GPINTENB, 0xff); |
rcwinder | 0:e206ee5c4d91 | 79 | mcpWriteReg(addr0, MCP_INTCONA, 0xff); |
rcwinder | 0:e206ee5c4d91 | 80 | mcpWriteReg(addr0, MCP_INTCONB, 0xff); |
rcwinder | 0:e206ee5c4d91 | 81 | mcpWriteReg(addr0, MCP_DEFVALA, 0xff); |
rcwinder | 0:e206ee5c4d91 | 82 | mcpWriteReg(addr0, MCP_DEFVALB, 0xff); |
rcwinder | 0:e206ee5c4d91 | 83 | mcpWriteReg(addr0, MCP_GPIOA, 0x00); |
rcwinder | 0:e206ee5c4d91 | 84 | mcpWriteReg(addr0, MCP_GPIOB, 0x00); |
rcwinder | 0:e206ee5c4d91 | 85 | |
rcwinder | 0:e206ee5c4d91 | 86 | int0.fall(&interrupt_handler); |
rcwinder | 0:e206ee5c4d91 | 87 | //int1.fall(&interrupt_handler); |
rcwinder | 0:e206ee5c4d91 | 88 | |
rcwinder | 0:e206ee5c4d91 | 89 | } |
rcwinder | 0:e206ee5c4d91 | 90 | |
rcwinder | 0:e206ee5c4d91 | 91 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count){ |
rcwinder | 0:e206ee5c4d91 | 92 | unsigned __int64 command = 0x0000000000000000; // this is a 64-bit integer type, due to the packet size |
rcwinder | 0:e206ee5c4d91 | 93 | unsigned __int64 temp_command = 0x0000000000000000; |
rcwinder | 0:e206ee5c4d91 | 94 | unsigned __int64 prefix = 0x3FFF; // 14 1 bits as in the preamble of the data packet |
rcwinder | 0:e206ee5c4d91 | 95 | unsigned int error = 0x00; // error byte |
rcwinder | 0:e206ee5c4d91 | 96 | //calculate error detection byte with xor |
rcwinder | 0:e206ee5c4d91 | 97 | error = address ^ inst; |
rcwinder | 0:e206ee5c4d91 | 98 | // combine packet bits in basic DCC format |
rcwinder | 0:e206ee5c4d91 | 99 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
rcwinder | 0:e206ee5c4d91 | 100 | |
rcwinder | 0:e206ee5c4d91 | 101 | int i = 0; |
rcwinder | 0:e206ee5c4d91 | 102 | |
rcwinder | 0:e206ee5c4d91 | 103 | // repeat DCC command lots of times |
rcwinder | 0:e206ee5c4d91 | 104 | while(i < repeat_count) { |
rcwinder | 0:e206ee5c4d91 | 105 | temp_command = command; |
rcwinder | 0:e206ee5c4d91 | 106 | |
rcwinder | 0:e206ee5c4d91 | 107 | //loops throught packet bits enconding and sending out digital pulses for a DCC command |
rcwinder | 0:e206ee5c4d91 | 108 | for (int j=0; j<64; j++) { |
rcwinder | 0:e206ee5c4d91 | 109 | if((temp_command&0x8000000000000000)==0) { //test packet bit |
rcwinder | 0:e206ee5c4d91 | 110 | |
rcwinder | 0:e206ee5c4d91 | 111 | Track=0; |
rcwinder | 0:e206ee5c4d91 | 112 | wait_us(100); |
rcwinder | 0:e206ee5c4d91 | 113 | Track=1; |
rcwinder | 0:e206ee5c4d91 | 114 | wait_us(100); |
rcwinder | 0:e206ee5c4d91 | 115 | |
rcwinder | 0:e206ee5c4d91 | 116 | } else { |
rcwinder | 0:e206ee5c4d91 | 117 | |
rcwinder | 0:e206ee5c4d91 | 118 | Track=0; |
rcwinder | 0:e206ee5c4d91 | 119 | wait_us(58); |
rcwinder | 0:e206ee5c4d91 | 120 | Track=1; |
rcwinder | 0:e206ee5c4d91 | 121 | wait_us(58); |
rcwinder | 0:e206ee5c4d91 | 122 | |
rcwinder | 0:e206ee5c4d91 | 123 | } |
rcwinder | 0:e206ee5c4d91 | 124 | |
rcwinder | 0:e206ee5c4d91 | 125 | temp_command = temp_command<<1; |
rcwinder | 0:e206ee5c4d91 | 126 | } |
rcwinder | 0:e206ee5c4d91 | 127 | i++; |
rcwinder | 0:e206ee5c4d91 | 128 | } |
rcwinder | 0:e206ee5c4d91 | 129 | } |
rcwinder | 0:e206ee5c4d91 | 130 | |
rcwinder | 0:e206ee5c4d91 | 131 | unsigned __int64 create_instruction(){ |
rcwinder | 0:e206ee5c4d91 | 132 | unsigned __int64 instruction = 0x0; |
rcwinder | 0:e206ee5c4d91 | 133 | unsigned __int64 direction = FORWARD; |
rcwinder | 0:e206ee5c4d91 | 134 | unsigned __int64 lights = LIGHTON; |
rcwinder | 0:e206ee5c4d91 | 135 | unsigned __int64 speed = NORM; |
rcwinder | 0:e206ee5c4d91 | 136 | |
rcwinder | 0:e206ee5c4d91 | 137 | return instruction = (0x01<<6)|(direction<<5)|(lights<<4)|speed; |
rcwinder | 0:e206ee5c4d91 | 138 | } |
rcwinder | 0:e206ee5c4d91 | 139 | |
rcwinder | 0:e206ee5c4d91 | 140 | int main(){ |
rcwinder | 0:e206ee5c4d91 | 141 | initialize_mcp(); |
rcwinder | 0:e206ee5c4d91 | 142 | unsigned __int64 instruction = 0x0; |
rcwinder | 0:e206ee5c4d91 | 143 | |
rcwinder | 0:e206ee5c4d91 | 144 | while(1){ |
rcwinder | 0:e206ee5c4d91 | 145 | if(interrupted){ |
rcwinder | 0:e206ee5c4d91 | 146 | uint16_t data = (mcpReadReg(addr0, MCP_GPIOB) << 8) | mcpReadReg(addr0, MCP_GPIOA); |
rcwinder | 0:e206ee5c4d91 | 147 | //mcpReadReg(addr0, MCP_INTCAPA); |
rcwinder | 0:e206ee5c4d91 | 148 | //mcpReadReg(addr0, MCP_INTCAPB); |
rcwinder | 0:e206ee5c4d91 | 149 | //modify instructions |
rcwinder | 0:e206ee5c4d91 | 150 | printf("%x interrupted\n", data); |
rcwinder | 0:e206ee5c4d91 | 151 | interrupted = 0; |
rcwinder | 0:e206ee5c4d91 | 152 | } |
rcwinder | 0:e206ee5c4d91 | 153 | |
rcwinder | 0:e206ee5c4d91 | 154 | instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|FAST; |
rcwinder | 0:e206ee5c4d91 | 155 | DCC_send_command(train3, instruction, def_repeat_count); |
rcwinder | 0:e206ee5c4d91 | 156 | } |
rcwinder | 0:e206ee5c4d91 | 157 | } |
rcwinder | 0:e206ee5c4d91 | 158 | |
rcwinder | 0:e206ee5c4d91 | 159 | /* |
rcwinder | 0:e206ee5c4d91 | 160 | Interrupts |
rcwinder | 0:e206ee5c4d91 | 161 | INT0 - pin 8 |
rcwinder | 0:e206ee5c4d91 | 162 | |
rcwinder | 0:e206ee5c4d91 | 163 | clock en data pins for I2C communication - data sync - hier lezen na een interrupt |
rcwinder | 0:e206ee5c4d91 | 164 | SDA - pin 9 |
rcwinder | 0:e206ee5c4d91 | 165 | SCL - pin 10 |
rcwinder | 0:e206ee5c4d91 | 166 | |
rcwinder | 0:e206ee5c4d91 | 167 | Sending command signals to train tracks |
rcwinder | 0:e206ee5c4d91 | 168 | DAT - pin 20 |
rcwinder | 0:e206ee5c4d91 | 169 | EN - VOUT |
rcwinder | 0:e206ee5c4d91 | 170 | |
rcwinder | 0:e206ee5c4d91 | 171 | |
rcwinder | 0:e206ee5c4d91 | 172 | instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|STOP; |
rcwinder | 0:e206ee5c4d91 | 173 | DCC_send_command(train3, instruction, def_repeat_count); |
rcwinder | 0:e206ee5c4d91 | 174 | */ |