Jasmine Karlsson
/
train_rail
New project
Diff: main.cpp
- Revision:
- 10:cda57976225f
- Parent:
- 8:4573e1a4b4aa
- Child:
- 12:75aab2bdb3a1
--- a/main.cpp Tue Jun 05 13:08:20 2018 +0000 +++ b/main.cpp Tue Jun 05 14:18:13 2018 +0000 @@ -2,16 +2,56 @@ #include "TextLCD.h" #include <string> #include <iostream> +#include "mcp23017.h" -DigitalOut myled(LED1); -DigitalOut myled2(LED2); -DigitalOut myled3(LED3); -DigitalOut myled4(LED4); -AnalogIn Ain(p20); -TextLCD lcd(p22, p21, p23, p24, p25, p26); + +//board 1 + +I2C i2c(p28, p27); // sda, scl pins (you can also use the other I2C port pins: 9,10) + +const int addr0 = 0x20 << 1; // 7-bit base address of the MCP23017 + address bits (which are 000 for the first one), shifted 1 place to the right (see top header) + +DigitalOut myled(LED1); // +DigitalOut myled2(LED2); // led +DigitalOut myled3(LED3); // +DigitalOut myled4(LED4); // +AnalogIn Ain(p20); // pot. met. + +DigitalOut Track(p21); + +DigitalIn sw1(p5); // +DigitalIn sw2(p30); // switches +DigitalIn sw3(p29); // +DigitalIn sw4(p28); // + +void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data); // Write an I2C register +uint8_t mcpReadReg(uint8_t address, uint8_t reg); // Read an I2C register +void initMcp0(void); + +unsigned int DarkRedtrain = 0x01; +unsigned int DCCinst_forward = 0x68; //forward half speed + +//move forward + +//speed dial + +//stop + +//read switch out + +//read signal + +//display message + +//send command +void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); + +TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd int main() { while(1){ + unsigned int DCCaddress = 0x03; + DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3 float f = Ain.read(); //Read voltage value float Vin = f *3.3; lcd.printf("%.2f", Vin); @@ -19,3 +59,62 @@ //myled=1; } } + +void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data){ + char cmd[2];cmd[0] = reg; + cmd[1] = data; + i2c.write(address, cmd, 2); // Write 2 bytes to device on specified address +} + +uint8_t mcpReadReg(uint8_t address, uint8_t reg){ + char cmd[1]; + cmd[0] = reg; + i2c.write(address, cmd, 1); // Write address + i2c.read(address, cmd, 1); // Read value (one byte) + + return cmd[0]; // Return the read value +} + +void initMcp0(void){ + mcpWriteReg(addr0, MCP_IODIRA, 0xff); // All inputs + mcpWriteReg(addr0, MCP_IODIRB, 0xff); // All inputs +} + +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 + while(i < repeat_count) { + temp_command = 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 + //send data for a "0" bit + Track=0; + wait_us(100); + Track=1; + wait_us(100); + //printf("0011"); + } else { + //send data for a "1"bit + Track=0; + wait_us(58); + Track=1; + wait_us(58); + //printf("01"); + } + // next bit in packet + temp_command = temp_command<<1; + } + i++; + } +} \ No newline at end of file