initialisatie zoals vorig jaar (pins moeten nog aangepast)

Dependencies:   MCP23017 mbed

Committer:
rcwinder
Date:
Thu Jun 08 15:17:47 2017 +0000
Revision:
0:af56c309135b
Initialisatie zoals vorig jaar (pins nog fout)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rcwinder 0:af56c309135b 1 #include "mbed.h"
rcwinder 0:af56c309135b 2 #include "MCP23017.h"
rcwinder 0:af56c309135b 3
rcwinder 0:af56c309135b 4 const unsigned __int64 FORWARD = 1;
rcwinder 0:af56c309135b 5 const unsigned __int64 BACKWARD = 0;
rcwinder 0:af56c309135b 6 const unsigned __int64 LIGHTON = 1;
rcwinder 0:af56c309135b 7 const unsigned __int64 LIGHTOFF = 0;
rcwinder 0:af56c309135b 8 const unsigned __int64 SLOW = 0x4; //0100 = step 5
rcwinder 0:af56c309135b 9 const unsigned __int64 NORM = 0x9; //1001 = step 15
rcwinder 0:af56c309135b 10 const unsigned __int64 FAST = 0xE; //1110 = step 25
rcwinder 0:af56c309135b 11 const unsigned __int64 STOP = 0x1; //0001 = E-STOP
rcwinder 0:af56c309135b 12
rcwinder 0:af56c309135b 13 const unsigned __int64 train1 = 0x1;
rcwinder 0:af56c309135b 14 const unsigned __int64 train3 = 0x3;
rcwinder 0:af56c309135b 15 const unsigned __int64 def_repeat_count = 5;
rcwinder 0:af56c309135b 16
rcwinder 0:af56c309135b 17 int interrupted = 0;
rcwinder 0:af56c309135b 18
rcwinder 0:af56c309135b 19 DigitalOut Track(p20);
rcwinder 0:af56c309135b 20 DigitalOut led(LED1);
rcwinder 0:af56c309135b 21 InterruptIn int0(p8);
rcwinder 0:af56c309135b 22 InterruptIn int1(p9);
rcwinder 0:af56c309135b 23 MCP23017 *mcp;
rcwinder 0:af56c309135b 24
rcwinder 0:af56c309135b 25 void initialize_mcp() {
rcwinder 0:af56c309135b 26 mcp = new MCP23017(p28, p27, 0x40); // Connect to mbed int pins 27 and 28,
rcwinder 0:af56c309135b 27 // MCP address is 0x40
rcwinder 0:af56c309135b 28 mcp->reset();
rcwinder 0:af56c309135b 29 mcp->writeRegister(0x00, (unsigned char )0xff);
rcwinder 0:af56c309135b 30 mcp->writeRegister(0x01, (unsigned char )0xff);
rcwinder 0:af56c309135b 31 mcp->writeRegister(0x02, (unsigned char )0x00);
rcwinder 0:af56c309135b 32 mcp->writeRegister(0x03, (unsigned char )0x00);
rcwinder 0:af56c309135b 33 mcp->writeRegister(0x04, (unsigned char )0xff);
rcwinder 0:af56c309135b 34 mcp->writeRegister(0x05, (unsigned char )0xff);
rcwinder 0:af56c309135b 35 mcp->writeRegister(0x06, (unsigned char )0xff);
rcwinder 0:af56c309135b 36 mcp->writeRegister(0x07, (unsigned char )0xff);
rcwinder 0:af56c309135b 37 mcp->writeRegister(0x08, (unsigned char )0xff);
rcwinder 0:af56c309135b 38 mcp->writeRegister(0x09, (unsigned char )0xff);
rcwinder 0:af56c309135b 39 mcp->writeRegister(0x0a, (unsigned char )0x42);
rcwinder 0:af56c309135b 40 mcp->writeRegister(0x0b, (unsigned char )0x42);
rcwinder 0:af56c309135b 41 mcp->writeRegister(0x0c, (unsigned char )0x00);
rcwinder 0:af56c309135b 42 mcp->writeRegister(0x0d, (unsigned char )0x00);
rcwinder 0:af56c309135b 43
rcwinder 0:af56c309135b 44 wait_ms(100);
rcwinder 0:af56c309135b 45 mcp->readRegister(GPIO);
rcwinder 0:af56c309135b 46 }
rcwinder 0:af56c309135b 47
rcwinder 0:af56c309135b 48 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count){
rcwinder 0:af56c309135b 49 unsigned __int64 command = 0x0000000000000000; // this is a 64-bit integer type, due to the packet size
rcwinder 0:af56c309135b 50 unsigned __int64 temp_command = 0x0000000000000000;
rcwinder 0:af56c309135b 51 unsigned __int64 prefix = 0x3FFF; // 14 1 bits as in the preamble of the data packet
rcwinder 0:af56c309135b 52 unsigned int error = 0x00; // error byte
rcwinder 0:af56c309135b 53 //calculate error detection byte with xor
rcwinder 0:af56c309135b 54 error = address ^ inst;
rcwinder 0:af56c309135b 55 // combine packet bits in basic DCC format
rcwinder 0:af56c309135b 56 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
rcwinder 0:af56c309135b 57
rcwinder 0:af56c309135b 58 int i = 0;
rcwinder 0:af56c309135b 59
rcwinder 0:af56c309135b 60 // repeat DCC command lots of times
rcwinder 0:af56c309135b 61 while(i < repeat_count) {
rcwinder 0:af56c309135b 62 temp_command = command;
rcwinder 0:af56c309135b 63
rcwinder 0:af56c309135b 64 //loops throught packet bits enconding and sending out digital pulses for a DCC command
rcwinder 0:af56c309135b 65 for (int j=0; j<64; j++) {
rcwinder 0:af56c309135b 66 if((temp_command&0x8000000000000000)==0) { //test packet bit
rcwinder 0:af56c309135b 67
rcwinder 0:af56c309135b 68 Track=0;
rcwinder 0:af56c309135b 69 wait_us(100);
rcwinder 0:af56c309135b 70 Track=1;
rcwinder 0:af56c309135b 71 wait_us(100);
rcwinder 0:af56c309135b 72
rcwinder 0:af56c309135b 73 } else {
rcwinder 0:af56c309135b 74
rcwinder 0:af56c309135b 75 Track=0;
rcwinder 0:af56c309135b 76 wait_us(58);
rcwinder 0:af56c309135b 77 Track=1;
rcwinder 0:af56c309135b 78 wait_us(58);
rcwinder 0:af56c309135b 79
rcwinder 0:af56c309135b 80 }
rcwinder 0:af56c309135b 81
rcwinder 0:af56c309135b 82 temp_command = temp_command<<1;
rcwinder 0:af56c309135b 83 }
rcwinder 0:af56c309135b 84 i++;
rcwinder 0:af56c309135b 85 }
rcwinder 0:af56c309135b 86 }
rcwinder 0:af56c309135b 87
rcwinder 0:af56c309135b 88 unsigned __int64 create_instruction(){
rcwinder 0:af56c309135b 89 unsigned __int64 instruction = 0x0;
rcwinder 0:af56c309135b 90 unsigned __int64 direction = FORWARD;
rcwinder 0:af56c309135b 91 unsigned __int64 lights = LIGHTON;
rcwinder 0:af56c309135b 92 unsigned __int64 speed = NORM;
rcwinder 0:af56c309135b 93
rcwinder 0:af56c309135b 94 return instruction = (0x01<<6)|(direction<<5)|(lights<<4)|speed;
rcwinder 0:af56c309135b 95 }
rcwinder 0:af56c309135b 96
rcwinder 0:af56c309135b 97 void interrupt_handler(){
rcwinder 0:af56c309135b 98 interrupted = 1;
rcwinder 0:af56c309135b 99 }
rcwinder 0:af56c309135b 100
rcwinder 0:af56c309135b 101 int main(){
rcwinder 0:af56c309135b 102 initialize_mcp();
rcwinder 0:af56c309135b 103 int0.fall(&interrupt_handler);
rcwinder 0:af56c309135b 104 int1.fall(&interrupt_handler);
rcwinder 0:af56c309135b 105 unsigned __int64 instruction = 0x0;
rcwinder 0:af56c309135b 106
rcwinder 0:af56c309135b 107 while(1){
rcwinder 0:af56c309135b 108 if(interrupted){
rcwinder 0:af56c309135b 109 printf("interrupted\n");
rcwinder 0:af56c309135b 110 mcp->readRegister(INTCAP);
rcwinder 0:af56c309135b 111 int data = mcp->readRegister(GPIO); // value of GPIO is 0x12
rcwinder 0:af56c309135b 112 //modify instructions
rcwinder 0:af56c309135b 113 printf("%d", data);
rcwinder 0:af56c309135b 114 led = 1;
rcwinder 0:af56c309135b 115 interrupted = 0;
rcwinder 0:af56c309135b 116 } else {
rcwinder 0:af56c309135b 117 led = 0;
rcwinder 0:af56c309135b 118 }
rcwinder 0:af56c309135b 119 instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|NORM;
rcwinder 0:af56c309135b 120 DCC_send_command(train3, instruction, def_repeat_count);
rcwinder 0:af56c309135b 121 }
rcwinder 0:af56c309135b 122 }
rcwinder 0:af56c309135b 123
rcwinder 0:af56c309135b 124 /*
rcwinder 0:af56c309135b 125 Interrupts
rcwinder 0:af56c309135b 126 INT0 - pin 8
rcwinder 0:af56c309135b 127
rcwinder 0:af56c309135b 128 clock en data pins for I2C communication - data sync - hier lezen na een interrupt
rcwinder 0:af56c309135b 129 SDA - pin 9
rcwinder 0:af56c309135b 130 SCL - pin 10
rcwinder 0:af56c309135b 131
rcwinder 0:af56c309135b 132 Sending command signals to train tracks
rcwinder 0:af56c309135b 133 DAT - pin 20
rcwinder 0:af56c309135b 134 EN - VOUT
rcwinder 0:af56c309135b 135
rcwinder 0:af56c309135b 136
rcwinder 0:af56c309135b 137 instruction = (0x01<<6)|(FORWARD<<5)|(LIGHTON<<4)|STOP;
rcwinder 0:af56c309135b 138 DCC_send_command(train3, instruction, def_repeat_count);
rcwinder 0:af56c309135b 139 */