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.
main.cpp
- Committer:
- mglmx
- Date:
- 2018-06-07
- Revision:
- 4:50879dfb82d5
- Parent:
- 3:fe7010b693a0
- Child:
- 5:ce0f66ea12c5
File content as of revision 4:50879dfb82d5:
#include "mbed.h" #include "TextLCD.h" //mbed DCC Model Train Demo DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut redled(p29); DigitalOut greenled(p30); DigitalIn switch1(p9); TextLCD lcd(p22,p21,p23,p24,p25,p26); AnalogIn pot(p19); DigitalIn d21(p13); DigitalIn d22(p14); DigitalIn d23(p15); 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++; } } //DCC train demo turns on headlight, dims headlight, and moves back and forth at half speed forever int main() { led1 = 1; wait(0.5); led1 = 0; wait(0.5); led1 = 1; //typical out of box default engine DCC address is 3 (at least for Bachmann trains) //Note: A DCC controller can reprogram the address whenever needed unsigned int DCCaddress = 0x01; //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB unsigned int DCCinst_forward = 0x68; //forward half speed unsigned int DCCinst_reverse = 0x48; //reverse half speed unsigned int DCCinst_stop = 0x50; //100DDDDD for basic headlight functions unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight // //Basic DCC Demo Commands DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again led2 = 1; while(1) { if(d21 == 1 || d22 == 1 || d23 == 1){ lcd.cls(); lcd.printf("Choo Choo station"); DCC_send_command(DCCaddress,DCCinst_stop,400); // forward half speed train address 3 }else{ DCC_send_command(DCCaddress,DCCinst_forward,1); // forward half speed train address 3 } /* float f = pot.read(); float vin = f * 3.3; led3 = 1; if(switch1 == 1){ lcd.cls(); lcd.printf("Going forward"); greenled = 0; redled = 1; DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3 lcd.cls(); lcd.printf("vin: %.4f",vin); wait(0.2); }else{ lcd.cls(); lcd.printf("Going backwards"); greenled = 1; redled = 0; DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3 lcd.cls(); lcd.printf("vin: %.4f",vin); wait(0.2); } */ } }