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.
Diff: main.cpp
- Revision:
- 14:a5fd98a957e6
- Parent:
- 13:78a2481fd65b
- Child:
- 15:8d96f7a06103
diff -r 78a2481fd65b -r a5fd98a957e6 main.cpp --- a/main.cpp Wed Jun 06 11:44:35 2018 +0000 +++ b/main.cpp Wed Jun 06 23:09:22 2018 +0000 @@ -2,8 +2,7 @@ #include "TextLCD.h" #include <string> #include <iostream> -#include "mcp23017.h" - +#include "mcp23017.h" //board 1 @@ -11,32 +10,58 @@ 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); // +/* LED Definitions */ +DigitalOut myled(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut myled4(LED4); + +TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd + AnalogIn Ain(p20); // pot. met. DigitalOut Track(p21); // train track -AnalogIn detect_21(p15); // detectors pin d2, d21, d22 +/* Train detectors d2, d21, d22 (trainstation) */ +AnalogIn detect_21(p15); AnalogIn detect_22(p16); AnalogIn detect_2(p17); -DigitalIn sw1(p5); // -DigitalIn sw2(p30); // switches -DigitalIn sw3(p29); // -DigitalIn sw4(p28); // +/* Switch Definitions */ +DigitalIn sw1(p5); +DigitalIn sw2(p30); +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); -void readVoltage(); -void readDetector(); - +/* Train Definitions */ unsigned int DCCaddress_darkRed = 0x01; unsigned int DCCaddress_lightRed = 0x03; + +/* Train movement */ + +/*---------------------------------------------------------------------------- +Train movement +*----------------------------------------------------------------------------*/ +//move forward unsigned int DCCinst_forward = 0x68; //forward half speed +//move backwards/reverse +unsigned int DCCinst_reverse = 01001000; //reverse half speed + +//speed dial +unsigned int DCCinst_step1 = 01100010; //step 1 +unsigned int DCCinst_step2 = 01110010; //step 2 +unsigned int DCCinst_step3 = 01100011; //step 3 +unsigned int DCCinst_step4 = 01110011; //step 4 +unsigned int DCCinst_step5 = 01100100; //step 5 +unsigned int DCCinst_step6 = 01110100; //step 6 1/4 speed + +unsigned int DCCinst_step13 = 01100000; //step 13 1/2 speed + +unsigned int DCCinst_step20 = 01110101; //step 20 3/4 speed + +unsigned int DCCinst_step28 = 01111111; //step 28 Full speed + +//stop unsigned int DCCinst_forward_stop = 0x60; //forward and stop 01100000 //move forward @@ -51,10 +76,18 @@ //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 +/*---------------------------------------------------------------------------- +Functions +*----------------------------------------------------------------------------*/ +/* Functions definition */ +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); // Initialization of MCP23017 with address bits 000 (track sensors) +void testMcp0(void); // Test of MCP23017 with address bits 000 +void readVoltage(); +void readDetector(); +void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command int main() { while(1){ @@ -100,11 +133,26 @@ return cmd[0]; // Return the read value } +/* Initialization of the MCP23017 for the track sensors. +We should enable the MCP23017 interrupts here because the sensors will only give a short pulse to the MCP23017*/ void initMcp0(void){ mcpWriteReg(addr0, MCP_IODIRA, 0xff); // All inputs mcpWriteReg(addr0, MCP_IODIRB, 0xff); // All inputs } +/* Test of the track sensors +* This does not use interrupts so the chance of actually detecting a train is very, very low +* With some patience it's good enough to test the hardware though. +*/ +void testMcp0(void) { + uint16_t sensors; // Put both ports in the sensor variable + sensors = (mcpReadReg(addr0, MCP_GPIOB) << 8) | mcpReadReg(addr0, MCP_GPIOA); + // Flip all bits, since the sensors are active low and it gives me headaches + // You could also set the IPOLA / IPOLB registers instead (see page 13 of the MCP23017 datasheet) + sensors = ~sensors; + lcd.printf("sensors: %04X\n", sensors); + } + 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 @@ -128,14 +176,12 @@ 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;