Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Committer:
mglmx
Date:
Thu Jun 07 12:14:03 2018 +0000
Revision:
15:700cfb65da5f
Parent:
7:e2b8461d4f05
Interrupt implemented

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mglmx 0:4d06a6a8e785 1 #include "mbed.h"
mglmx 3:fe7010b693a0 2 #include "TextLCD.h"
mglmx 15:700cfb65da5f 3 #include "MCP23017"
mglmx 15:700cfb65da5f 4
mglmx 1:0ab26889af9b 5 //mbed DCC Model Train Demo
mglmx 1:0ab26889af9b 6
carlosperales95 7:e2b8461d4f05 7 /******PINS AND DECLARATIONS*******/
carlosperales95 7:e2b8461d4f05 8
carlosperales95 7:e2b8461d4f05 9 ///p5
carlosperales95 7:e2b8461d4f05 10 ///p6
carlosperales95 7:e2b8461d4f05 11 ///p7
carlosperales95 7:e2b8461d4f05 12 ///p8
carlosperales95 7:e2b8461d4f05 13
carlosperales95 7:e2b8461d4f05 14 //INT0 - p9
carlosperales95 7:e2b8461d4f05 15 DigitalIn switch1(p9);
carlosperales95 7:e2b8461d4f05 16 //INT1 - p10
carlosperales95 7:e2b8461d4f05 17
carlosperales95 7:e2b8461d4f05 18 ///p11
carlosperales95 7:e2b8461d4f05 19 ///p12
carlosperales95 7:e2b8461d4f05 20 //M0 - p13
carlosperales95 7:e2b8461d4f05 21 DigitalIn d21(p13);
carlosperales95 7:e2b8461d4f05 22 //M1 - p14
carlosperales95 7:e2b8461d4f05 23 DigitalIn d22(p14);
carlosperales95 7:e2b8461d4f05 24 //M2 - p15
carlosperales95 7:e2b8461d4f05 25 DigitalIn d23(p15);
carlosperales95 7:e2b8461d4f05 26 //p16
carlosperales95 7:e2b8461d4f05 27 ///p17
carlosperales95 7:e2b8461d4f05 28 ///p18
carlosperales95 7:e2b8461d4f05 29
carlosperales95 7:e2b8461d4f05 30 //POTENTIOMETER - p19
carlosperales95 7:e2b8461d4f05 31 AnalogIn pot(p19);
carlosperales95 7:e2b8461d4f05 32
carlosperales95 7:e2b8461d4f05 33 //DAT - p20
mglmx 3:fe7010b693a0 34 DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge
carlosperales95 7:e2b8461d4f05 35
carlosperales95 7:e2b8461d4f05 36 //LCD SCREEN - p21, p22, p23, p24, p25, p26
carlosperales95 7:e2b8461d4f05 37 TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7
carlosperales95 7:e2b8461d4f05 38
carlosperales95 7:e2b8461d4f05 39 ///p27
carlosperales95 7:e2b8461d4f05 40 ///p28
carlosperales95 7:e2b8461d4f05 41
carlosperales95 7:e2b8461d4f05 42 //LED1 - p29
mglmx 3:fe7010b693a0 43 DigitalOut redled(p29);
carlosperales95 7:e2b8461d4f05 44 //LED2 - p30
mglmx 3:fe7010b693a0 45 DigitalOut greenled(p30);
carlosperales95 7:e2b8461d4f05 46
mglmx 5:ce0f66ea12c5 47
mglmx 5:ce0f66ea12c5 48 //Rail sensors
mglmx 15:700cfb65da5f 49 InterruptIn int0(p9);
mglmx 15:700cfb65da5f 50 InterruptIn int1(p10);
mglmx 15:700cfb65da5f 51
mglmx 15:700cfb65da5f 52 //SDA - p28
mglmx 15:700cfb65da5f 53 //SCL - p27
mglmx 15:700cfb65da5f 54
mglmx 15:700cfb65da5f 55 MCP23017 *mcp;
mglmx 15:700cfb65da5f 56
mglmx 15:700cfb65da5f 57 void interrupt0(){
mglmx 15:700cfb65da5f 58 int data = mcp->readRegister(GPIO);
mglmx 15:700cfb65da5f 59 lcd.printf("int0 %d",data);
mglmx 15:700cfb65da5f 60 }
mglmx 15:700cfb65da5f 61
mglmx 15:700cfb65da5f 62 void interrupt1(){
mglmx 15:700cfb65da5f 63 int data = mcp->readRegister(GPIO);
mglmx 15:700cfb65da5f 64 lcd.printf("int1 %d",data);
mglmx 15:700cfb65da5f 65 }
mglmx 5:ce0f66ea12c5 66
mglmx 15:700cfb65da5f 67 void initalize_mcp(){
mglmx 15:700cfb65da5f 68 mcp = new MPC23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40
mglmx 15:700cfb65da5f 69
mglmx 15:700cfb65da5f 70 mcp->reset();
mglmx 15:700cfb65da5f 71 mcp->writeRegister(0x00, (unsigned char )0xff);
mglmx 15:700cfb65da5f 72 mcp->writeRegister(0x01, (unsigned char )0xff);
mglmx 15:700cfb65da5f 73 mcp->writeRegister(0x02, (unsigned char )0x00);
mglmx 15:700cfb65da5f 74 mcp->writeRegister(0x03, (unsigned char )0x00);
mglmx 15:700cfb65da5f 75 mcp->writeRegister(0x04, (unsigned char )0xff);
mglmx 15:700cfb65da5f 76 mcp->writeRegister(0x05, (unsigned char )0xff);
mglmx 15:700cfb65da5f 77 mcp->writeRegister(0x06, (unsigned char )0xff);
mglmx 15:700cfb65da5f 78 mcp->writeRegister(0x07, (unsigned char )0xff);
mglmx 15:700cfb65da5f 79 mcp->writeRegister(0x08, (unsigned char )0xff);
mglmx 15:700cfb65da5f 80 mcp->writeRegister(0x09, (unsigned char )0xff);
mglmx 15:700cfb65da5f 81 mcp->writeRegister(0x0a, (unsigned char )0x42);
mglmx 15:700cfb65da5f 82 mcp->writeRegister(0x0b, (unsigned char )0x42);
mglmx 15:700cfb65da5f 83 mcp->writeRegister(0x0c, (unsigned char )0x00);
mglmx 15:700cfb65da5f 84 mcp->writeRegister(0x0d, (unsigned char )0x00);
mglmx 15:700cfb65da5f 85 }
mglmx 1:0ab26889af9b 86
mglmx 1:0ab26889af9b 87 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
mglmx 1:0ab26889af9b 88 {
mglmx 1:0ab26889af9b 89 unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
mglmx 1:0ab26889af9b 90 unsigned __int64 temp_command = 0x0000000000000000;
mglmx 1:0ab26889af9b 91 unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
mglmx 1:0ab26889af9b 92 unsigned int error = 0x00; //error byte
mglmx 1:0ab26889af9b 93 //calculate error detection byte with xor
mglmx 1:0ab26889af9b 94 error = address ^ inst;
mglmx 1:0ab26889af9b 95 //combine packet bits in basic DCC format
mglmx 1:0ab26889af9b 96 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
mglmx 1:0ab26889af9b 97 //printf("\n\r %llx \n\r",command);
mglmx 1:0ab26889af9b 98 int i=0;
mglmx 1:0ab26889af9b 99 //repeat DCC command lots of times
mglmx 1:0ab26889af9b 100 while(i < repeat_count) {
mglmx 1:0ab26889af9b 101 temp_command = command;
mglmx 1:0ab26889af9b 102 //loops through packet bits encoding and sending out digital pulses for a DCC command
mglmx 1:0ab26889af9b 103 for (int j=0; j<64; j++) {
mglmx 1:0ab26889af9b 104 if((temp_command&0x8000000000000000)==0) { //test packet bit
mglmx 1:0ab26889af9b 105 //send data for a "0" bit
mglmx 1:0ab26889af9b 106 Track=0;
mglmx 1:0ab26889af9b 107 wait_us(100);
mglmx 1:0ab26889af9b 108 Track=1;
mglmx 1:0ab26889af9b 109 wait_us(100);
mglmx 1:0ab26889af9b 110 //printf("0011");
mglmx 1:0ab26889af9b 111 } else {
mglmx 1:0ab26889af9b 112 //send data for a "1"bit
mglmx 1:0ab26889af9b 113 Track=0;
mglmx 1:0ab26889af9b 114 wait_us(58);
mglmx 1:0ab26889af9b 115 Track=1;
mglmx 1:0ab26889af9b 116 wait_us(58);
mglmx 1:0ab26889af9b 117 //printf("01");
mglmx 1:0ab26889af9b 118 }
mglmx 1:0ab26889af9b 119 // next bit in packet
mglmx 1:0ab26889af9b 120 temp_command = temp_command<<1;
mglmx 1:0ab26889af9b 121 }
mglmx 1:0ab26889af9b 122 i++;
mglmx 0:4d06a6a8e785 123 }
mglmx 0:4d06a6a8e785 124 }
mglmx 1:0ab26889af9b 125 //DCC train demo turns on headlight, dims headlight, and moves back and forth at half speed forever
mglmx 1:0ab26889af9b 126 int main()
mglmx 1:0ab26889af9b 127 {
mglmx 2:f580707c44fa 128 led1 = 1;
mglmx 2:f580707c44fa 129 wait(0.5);
mglmx 2:f580707c44fa 130 led1 = 0;
mglmx 2:f580707c44fa 131 wait(0.5);
mglmx 2:f580707c44fa 132 led1 = 1;
mglmx 15:700cfb65da5f 133
mglmx 15:700cfb65da5f 134 initialize_mcp();
mglmx 15:700cfb65da5f 135
mglmx 15:700cfb65da5f 136 int0.rise($interrupt0);
mglmx 15:700cfb65da5f 137 int1.rise($interrupt1);
mglmx 15:700cfb65da5f 138
mglmx 15:700cfb65da5f 139
mglmx 1:0ab26889af9b 140 //typical out of box default engine DCC address is 3 (at least for Bachmann trains)
mglmx 1:0ab26889af9b 141 //Note: A DCC controller can reprogram the address whenever needed
mglmx 1:0ab26889af9b 142 unsigned int DCCaddress = 0x01;
mglmx 1:0ab26889af9b 143 //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf
mglmx 1:0ab26889af9b 144 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
mglmx 1:0ab26889af9b 145 unsigned int DCCinst_forward = 0x68; //forward half speed
mglmx 1:0ab26889af9b 146 unsigned int DCCinst_reverse = 0x48; //reverse half speed
mglmx 4:50879dfb82d5 147 unsigned int DCCinst_stop = 0x50;
mglmx 1:0ab26889af9b 148 //100DDDDD for basic headlight functions
mglmx 1:0ab26889af9b 149 unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function
mglmx 1:0ab26889af9b 150 unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight
mglmx 1:0ab26889af9b 151 //
mglmx 1:0ab26889af9b 152 //Basic DCC Demo Commands
mglmx 1:0ab26889af9b 153 DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full
mglmx 1:0ab26889af9b 154 DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light
mglmx 1:0ab26889af9b 155 DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again
mglmx 2:f580707c44fa 156 led2 = 1;
mglmx 1:0ab26889af9b 157 while(1) {
mglmx 4:50879dfb82d5 158
mglmx 4:50879dfb82d5 159
mglmx 4:50879dfb82d5 160
mglmx 4:50879dfb82d5 161 if(d21 == 1 || d22 == 1 || d23 == 1){
mglmx 4:50879dfb82d5 162 lcd.cls();
mglmx 4:50879dfb82d5 163 lcd.printf("Choo Choo station");
mglmx 4:50879dfb82d5 164 DCC_send_command(DCCaddress,DCCinst_stop,400); // forward half speed train address 3
mglmx 4:50879dfb82d5 165
mglmx 4:50879dfb82d5 166 }else{
mglmx 4:50879dfb82d5 167 DCC_send_command(DCCaddress,DCCinst_forward,1); // forward half speed train address 3
mglmx 4:50879dfb82d5 168 }
mglmx 4:50879dfb82d5 169
mglmx 4:50879dfb82d5 170
mglmx 4:50879dfb82d5 171 /*
mglmx 3:fe7010b693a0 172 float f = pot.read();
mglmx 3:fe7010b693a0 173 float vin = f * 3.3;
mglmx 3:fe7010b693a0 174
mglmx 2:f580707c44fa 175 led3 = 1;
mglmx 3:fe7010b693a0 176 if(switch1 == 1){
mglmx 3:fe7010b693a0 177 lcd.cls();
mglmx 3:fe7010b693a0 178 lcd.printf("Going forward");
mglmx 3:fe7010b693a0 179 greenled = 0;
mglmx 3:fe7010b693a0 180 redled = 1;
mglmx 3:fe7010b693a0 181 DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3
mglmx 3:fe7010b693a0 182 lcd.cls();
mglmx 3:fe7010b693a0 183
mglmx 3:fe7010b693a0 184 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 185 wait(0.2);
mglmx 3:fe7010b693a0 186
mglmx 3:fe7010b693a0 187 }else{
mglmx 3:fe7010b693a0 188 lcd.cls();
mglmx 3:fe7010b693a0 189 lcd.printf("Going backwards");
mglmx 3:fe7010b693a0 190 greenled = 1;
mglmx 3:fe7010b693a0 191 redled = 0;
mglmx 3:fe7010b693a0 192 DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3
mglmx 3:fe7010b693a0 193 lcd.cls();
mglmx 3:fe7010b693a0 194
mglmx 3:fe7010b693a0 195 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 196 wait(0.2);
mglmx 3:fe7010b693a0 197
mglmx 3:fe7010b693a0 198 }
mglmx 4:50879dfb82d5 199 */
mglmx 1:0ab26889af9b 200 }
mglmx 3:fe7010b693a0 201 }