Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Committer:
mglmx
Date:
Thu Jun 07 12:35:18 2018 +0000
Revision:
18:aa43bb62e60f
Parent:
17:0a657e338356
Child:
20:608ab9bf340d
Interrupt with buzz now compiling

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