Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Committer:
carlosperales95
Date:
Thu Jun 07 10:09:15 2018 +0000
Revision:
6:9789ad5f8b7f
Parent:
3:fe7010b693a0
Tags for pins in declaration at the beginning - KEEP UPTODATE

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 1:0ab26889af9b 3 //mbed DCC Model Train Demo
carlosperales95 6:9789ad5f8b7f 4
carlosperales95 6:9789ad5f8b7f 5 ///p5
carlosperales95 6:9789ad5f8b7f 6 ///p6
carlosperales95 6:9789ad5f8b7f 7 ///p7
carlosperales95 6:9789ad5f8b7f 8 ///p8
carlosperales95 6:9789ad5f8b7f 9
carlosperales95 6:9789ad5f8b7f 10 //INT0 - p9
carlosperales95 6:9789ad5f8b7f 11 DigitalIn switch1(p9);
carlosperales95 6:9789ad5f8b7f 12 //INT1 - p10
carlosperales95 6:9789ad5f8b7f 13
carlosperales95 6:9789ad5f8b7f 14 ///p11
carlosperales95 6:9789ad5f8b7f 15 ///p12
carlosperales95 6:9789ad5f8b7f 16 //M0 - p13
carlosperales95 6:9789ad5f8b7f 17 //M1 - p14
carlosperales95 6:9789ad5f8b7f 18 //M2 - p15
carlosperales95 6:9789ad5f8b7f 19 //p16
carlosperales95 6:9789ad5f8b7f 20 ///p17
carlosperales95 6:9789ad5f8b7f 21 ///p18
carlosperales95 6:9789ad5f8b7f 22
carlosperales95 6:9789ad5f8b7f 23 //POTENTIOMETER - p19
carlosperales95 6:9789ad5f8b7f 24 AnalogIn pot(p19);
carlosperales95 6:9789ad5f8b7f 25
carlosperales95 6:9789ad5f8b7f 26 //DAT - p20
mglmx 3:fe7010b693a0 27 DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge
carlosperales95 6:9789ad5f8b7f 28
carlosperales95 6:9789ad5f8b7f 29 //LCD SCREEN - p21, p22, p23, p24, p25, p26
carlosperales95 6:9789ad5f8b7f 30 TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7
carlosperales95 6:9789ad5f8b7f 31
carlosperales95 6:9789ad5f8b7f 32 ///p27
carlosperales95 6:9789ad5f8b7f 33 ///p28
carlosperales95 6:9789ad5f8b7f 34
carlosperales95 6:9789ad5f8b7f 35 //LED1 - p29
carlosperales95 6:9789ad5f8b7f 36 DigitalOut redled(p29);
carlosperales95 6:9789ad5f8b7f 37 //LED2 - p30
carlosperales95 6:9789ad5f8b7f 38 DigitalOut greenled(p30);
carlosperales95 6:9789ad5f8b7f 39
carlosperales95 6:9789ad5f8b7f 40
mglmx 2:f580707c44fa 41 DigitalOut led1(LED1);
mglmx 2:f580707c44fa 42 DigitalOut led2(LED2);
mglmx 2:f580707c44fa 43 DigitalOut led3(LED3);
carlosperales95 6:9789ad5f8b7f 44
mglmx 1:0ab26889af9b 45
mglmx 1:0ab26889af9b 46 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
mglmx 1:0ab26889af9b 47 {
mglmx 1:0ab26889af9b 48 unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type
mglmx 1:0ab26889af9b 49 unsigned __int64 temp_command = 0x0000000000000000;
mglmx 1:0ab26889af9b 50 unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start
mglmx 1:0ab26889af9b 51 unsigned int error = 0x00; //error byte
mglmx 1:0ab26889af9b 52 //calculate error detection byte with xor
mglmx 1:0ab26889af9b 53 error = address ^ inst;
mglmx 1:0ab26889af9b 54 //combine packet bits in basic DCC format
mglmx 1:0ab26889af9b 55 command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01;
mglmx 1:0ab26889af9b 56 //printf("\n\r %llx \n\r",command);
mglmx 1:0ab26889af9b 57 int i=0;
mglmx 1:0ab26889af9b 58 //repeat DCC command lots of times
mglmx 1:0ab26889af9b 59 while(i < repeat_count) {
mglmx 1:0ab26889af9b 60 temp_command = command;
mglmx 1:0ab26889af9b 61 //loops through packet bits encoding and sending out digital pulses for a DCC command
mglmx 1:0ab26889af9b 62 for (int j=0; j<64; j++) {
mglmx 1:0ab26889af9b 63 if((temp_command&0x8000000000000000)==0) { //test packet bit
mglmx 1:0ab26889af9b 64 //send data for a "0" bit
mglmx 1:0ab26889af9b 65 Track=0;
mglmx 1:0ab26889af9b 66 wait_us(100);
mglmx 1:0ab26889af9b 67 Track=1;
mglmx 1:0ab26889af9b 68 wait_us(100);
mglmx 1:0ab26889af9b 69 //printf("0011");
mglmx 1:0ab26889af9b 70 } else {
mglmx 1:0ab26889af9b 71 //send data for a "1"bit
mglmx 1:0ab26889af9b 72 Track=0;
mglmx 1:0ab26889af9b 73 wait_us(58);
mglmx 1:0ab26889af9b 74 Track=1;
mglmx 1:0ab26889af9b 75 wait_us(58);
mglmx 1:0ab26889af9b 76 //printf("01");
mglmx 1:0ab26889af9b 77 }
mglmx 1:0ab26889af9b 78 // next bit in packet
mglmx 1:0ab26889af9b 79 temp_command = temp_command<<1;
mglmx 1:0ab26889af9b 80 }
mglmx 1:0ab26889af9b 81 i++;
mglmx 0:4d06a6a8e785 82 }
mglmx 0:4d06a6a8e785 83 }
mglmx 1:0ab26889af9b 84 //DCC train demo turns on headlight, dims headlight, and moves back and forth at half speed forever
mglmx 1:0ab26889af9b 85 int main()
mglmx 1:0ab26889af9b 86 {
mglmx 2:f580707c44fa 87 led1 = 1;
mglmx 2:f580707c44fa 88 wait(0.5);
mglmx 2:f580707c44fa 89 led1 = 0;
mglmx 2:f580707c44fa 90 wait(0.5);
mglmx 2:f580707c44fa 91 led1 = 1;
mglmx 1:0ab26889af9b 92 //typical out of box default engine DCC address is 3 (at least for Bachmann trains)
mglmx 1:0ab26889af9b 93 //Note: A DCC controller can reprogram the address whenever needed
mglmx 1:0ab26889af9b 94 unsigned int DCCaddress = 0x01;
mglmx 1:0ab26889af9b 95 //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf
mglmx 1:0ab26889af9b 96 //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB
mglmx 1:0ab26889af9b 97 unsigned int DCCinst_forward = 0x68; //forward half speed
mglmx 1:0ab26889af9b 98 unsigned int DCCinst_reverse = 0x48; //reverse half speed
mglmx 1:0ab26889af9b 99 //100DDDDD for basic headlight functions
mglmx 1:0ab26889af9b 100 unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function
mglmx 1:0ab26889af9b 101 unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight
mglmx 1:0ab26889af9b 102 //
mglmx 1:0ab26889af9b 103 //Basic DCC Demo Commands
mglmx 1:0ab26889af9b 104 DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full
mglmx 1:0ab26889af9b 105 DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light
mglmx 1:0ab26889af9b 106 DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again
mglmx 2:f580707c44fa 107 led2 = 1;
mglmx 1:0ab26889af9b 108 while(1) {
mglmx 3:fe7010b693a0 109 float f = pot.read();
mglmx 3:fe7010b693a0 110 float vin = f * 3.3;
mglmx 3:fe7010b693a0 111
mglmx 2:f580707c44fa 112 led3 = 1;
mglmx 3:fe7010b693a0 113 if(switch1 == 1){
mglmx 3:fe7010b693a0 114 lcd.cls();
mglmx 3:fe7010b693a0 115 lcd.printf("Going forward");
mglmx 3:fe7010b693a0 116 greenled = 0;
mglmx 3:fe7010b693a0 117 redled = 1;
mglmx 3:fe7010b693a0 118 DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3
mglmx 3:fe7010b693a0 119 lcd.cls();
mglmx 3:fe7010b693a0 120
mglmx 3:fe7010b693a0 121 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 122 wait(0.2);
mglmx 3:fe7010b693a0 123
mglmx 3:fe7010b693a0 124 }else{
mglmx 3:fe7010b693a0 125 lcd.cls();
mglmx 3:fe7010b693a0 126 lcd.printf("Going backwards");
mglmx 3:fe7010b693a0 127 greenled = 1;
mglmx 3:fe7010b693a0 128 redled = 0;
mglmx 3:fe7010b693a0 129 DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3
mglmx 3:fe7010b693a0 130 lcd.cls();
mglmx 3:fe7010b693a0 131
mglmx 3:fe7010b693a0 132 lcd.printf("vin: %.4f",vin);
mglmx 3:fe7010b693a0 133 wait(0.2);
mglmx 3:fe7010b693a0 134
mglmx 3:fe7010b693a0 135 }
mglmx 3:fe7010b693a0 136
mglmx 1:0ab26889af9b 137 }
mglmx 3:fe7010b693a0 138 }