# PC for DCC control..



## John Hill (Nov 6, 2012)

Hi, this is my first post (after my short introduction) and my interest is DCC, specifically using a PC for DCC control.

I am well advanced on a project for DCC control via the sound output from a PC. I doubt I am the first to do this but it is suprising that I could not find any examples!

I have software running and a simple (just two chips) hardware interface to the tracks. My software, which is very crude in appearance, can handle up to 14 trains (about ten more than I will ever have running!) and is limited, so far, to basic speed (28 steps) and direction.

No doubt I will have a lot of queries as I get further into this project.

A few details, I am writing in Delphi and using a USB sound adapter. The two chips are a 741 op amp and a LMD18200 3 amp motor driver chip. The power supply is an old PC power supply. The track voltage is a little lower than ideal but than can be remedied if necessary.

John


----------



## Southern (Nov 17, 2008)

Are working on something like JMRI?

http://jmri.sourceforge.net/


----------



## John Hill (Nov 6, 2012)

Hi, not near as impressive as JMRI. However when I was searching around I found this on the site.....


> JMRI supports a wide range of DCC "command station"-style products from various model railroad automation vendors. We also support non-DCC layout If there is a way to make a computer communicate with a given piece of model railroad equipment, it is possible to connect it to JMRI.


My scheme does not require a command station as the PC produces the DCC signal.

Maybe my module will be able to be used with JMRI, but first I need to finish and fine tune it. Right now I have some trains that respond as expected and some that dont and I need to get that resolved.


----------



## waltr (Aug 15, 2011)

John,
That is a neat idea. Does the 741 clip and square up the output from the PC sound card?
A LMD18200 should drive a nice signal to the track.
You may be able to boost the voltage out of the PC power supply. There is info on the web on how to do this (basicly there is a resistive voltage divider for feedback. by adjusting one resistor value you can increase the regulated output voltage).
Are you using an O'scope to measure the timing of the DCC signals?


----------



## John Hill (Nov 6, 2012)

Hi Waltr, the 741 operates as a zero crossing detector and gives a nice signal to the LMD18200.

Re the PC power supply, thanks, I need to study up on that a little.

The timing is purely from calculations of the data I put into the sound buffers that are passed to the sound card. I assume the cards are reasonably well calibrated but I have not really checked the output waveform against anything reliable. 

My scope is a basic two channel analogue device which does not do a very good job of displaying the DCC waveform.


----------



## gunrunnerjohn (Nov 10, 2010)

Just curious, why not use a real digital output from the PC? Something like the Arduino Nano has a USB port you can use with serial emulation and simply interface with the board. You can do pretty much anything in the software in the chip.


----------



## John Hill (Nov 6, 2012)

I can program the sound driver and create the sound data well within the specifications for DCC so I dont see any advantage in introducing more hardware into the picture.

The sound card has a very limited upper frequency capability which makes it incapable of outputting a true square wave (which include very high frequencies) but really what we need for DCC is accurate zero crossings and having those we can very easily create the required DCC square wave with a simple op amp (i.e. the 741 chip).

Those are my theories which I am yet to prove.... stay tuned! Ha ha.


----------



## manchesterjim (Dec 30, 2011)

Call me old-fashioned......(or something) but I find myself asking the question: "Why re-invent the wheel"? 

I mean.....yes, you need a DCC command station and USB adaptor to use JMRI, but beyond that, you have very few limits for programming options. JMRI is open-source....you can pretty much tinker with it however you like.

You'll get TONS of time programming all the same. Its not just about train control. JMRI allows you to build complex layout controls....and the only real limitation is imagination......

....just my 2 cents.
Jim


----------



## John Hill (Nov 6, 2012)

Hi Jim, nothing wrong with your 2 cents at all.. but this enquiring mind wants to know why I have not yet been able to find a DCC system that uses a digital sound output from a PC. There must be a reason as it is surely the most simple interface between a PC and a DCC power supply and almost every PC ever made already has the sound output.

Right now, my system works for some decoders and not for others and that is what is challenging me right now.

BTW, I am not excluding JMRI et al as I can see there has been a lot of very good work done there.

John


----------



## waltr (Aug 15, 2011)

John,
Thanks for the explanation. Using the 741 as a zero crossing detector would work very well to square the output wave form.
I know about using an old analog scope on a DCC signal (I am spoiled by the high-end digital scopes at work). If you're in the market for a new scope check out the Tektronix TDS2000 series scopes. These work very well and the top end model (4 ch, 200MHz) is about $2400. They have 2 ch and slower models for much less.

I think that trying to use any sort of USB would not work due to MS Windows handling of the USB. Data to USB gets buffered then burst out so is horrible if you need precise timing. This is also an issue with any IO on newer MS OS's past NT.
The sound card has is own processor that does control timing very well so this is a good choice if not unique one.


----------



## John Hill (Nov 6, 2012)

Yes Waltr, that is why I choose the sound card as the output from the PC. I used to write Morse code decoder software but that all came to a halt when latency became an issue with Windows, I assume USB would be even worse.


Things did not go well with my testing last evening. Some sound cards work and some do not and some decoders refuse to take any notice of the control commands.

It seems I have been deceived by the sound driver software that is happy to accept sample rates outside those usually used but the sample does not change, the result is the basic bit interval is nominally 62.5 uSec instead of about 58 uSec. That is ever so slightly outside the NMRA spec for what a command station should produce and slightly inside what a decoder should accept. Some decoders accept this and some do not.

Although I have a few more ideas to try I fear the 30cent command station may not be possible!!!


----------



## gunrunnerjohn (Nov 10, 2010)

I'm at a loss why use the sound output from the PC, that seems pretty convoluted.

I guess I'll wait to see what you're trying to create, right now it's not making much sense to me.


----------



## John Hill (Nov 6, 2012)

John, the challenge I set myself was to get a DCC signal out of a PC. i.e. square wave frequency modulated, two frequencies about 8600Hz, for the '1' bit, and about half that for the '0' bit.

I cannot imagine a way of getting a good square wave out of a standard PC at those frequencies (except maybe from the serial port which I have not yet examined) from a standard PC. However I can get analogue from the sound card and using only a 30cent 741 op amp I can get from that a nice square wave.

That is the challenge, although the NMRA standard is fairly wide the frequency does fall between the stand frequencies of the PC sound card, I can get close, close enough that some decoders will accept the signal but not all.

That would give me signal level DCC but I would still need a 'power station'. I can make a basic power station using an LMD18200 chip ($10 or so) and a surplus PC power supply.

DCC command system for 30 cents, DCC power station for $10.....

That is the challenge, I am close, very close, but I am unsure if I can close the gap.


----------



## gunrunnerjohn (Nov 10, 2010)

As long as you don't mind a little heat, you can build yourself a DC amplifier using power Darlington transistors.

I guess I just never think of offering myself a challenge that makes the task harder. I suppose that's the curse of having both software and hardware expertise, I use the best solution at hand.


----------



## John Hill (Nov 6, 2012)

Well John if I really wanted the 'best solution' I should just reach for the VISA card!


----------



## gunrunnerjohn (Nov 10, 2010)

Well, I do consider the price in that equation. I figure that talking to an Arduino board that costs me $12 with a USB interface is a pretty cost effective solution. The Arduino makes it a piece of cake to generate the proper waveforms.


----------



## John Hill (Nov 6, 2012)

Yeah, the Arduino is an interesting little device and I do have one in the top drawer but the IDE I have for it is somewhat lacking as it requires me to learn a super cryptic version of "C", if I cant write the code in something human readable like Pascal, Delphi, Modula 2 then I tend to loose interest. 

Nonetheless I am working on setting up for developing for the Arduino using mikroPascal, but thats another project and I really can only work on 'heavy' stuff like that when I am alone in the house.

Maybe one day I will be able to post that I have cracked the challenge of getting a DCC signal form direct from a PC without additional hardware, once I have to add hardware I tend to loose the achievement value, dont you think?


----------



## gunrunnerjohn (Nov 10, 2010)

> it requires me to learn a super cryptic version of "C"


Well, it's standard C/C++, which has been the standard language for embedded systems for at least 20 years! Nothing cryptic about it AFAIK.

I've used Pascal, and personally I'd much rather use C. I've looked at Delphi, but I couldn't see the point with a much more standard and supported language available. Also, there is a huge Ardunio user community that provides tons of 3rd party libraries and code. Lots of time, something that you'd like to do is already figured out and you just have to adapt it to your application. 

C was one of the attractions for the Ardunio for me. Another bonus is they did all the chip initialization and setting up the environment, something that for a new chip normally takes some work. I did that many times when I worked at a real job when we'd select a new uP, being retired I'm getting lazy and want something easy to use.


----------



## John Hill (Nov 6, 2012)

John, I should tell you a story of something that happened a long time ago, back in the days of the IBM XT.

Our small company in New Zealand offered a solution to a government organisation who wanted terminals that could send and receive continous ASCII streams at 9600bps while operators composed messages on the screen and a 5 lines a second chain printer ran continuously on the parallel port.

We were offering the solution through one of America's major technical and defence corporations who were the prime contractors. The clients questioned the feasibility of our solution and the prime contractor told us that according to their experts our solution was unworkable and the deal required us to come up with another solution.

We told the prime contractor that it certainly worked and had been running for some weeks non-stop in my garage at home. They sent a high powered party to NZ and were convinced.

That little piece of software was the key part of a software product that kept our company (3 people) afloat for about 15 years and is still in use in airports around the world.

So although this DCC challenge I have set myself may be impossible I will not abandon it until I have exhausted myself on it.


----------



## John Hill (Nov 6, 2012)

John, I am not sure 'C' was very mature when I started writing in Pascal and when we began to develop more serious applications the principals of the company decided we did not want the sort of suprises that 'C' seemed capable of delivering so we standardised on Modula 2 for the company.

When we bought a Modula 2 toolkit for audio processing the vendor eventually told us we were the only people to have ever bought their toolkit and would we please understand if they unable to provide further support. Fortunately their product was very good and we were able to use it for several years.

Modula 2 eventually fell by the wayside and it was natural that we would move to Delphi (for its Pascal parentage) which by then was well supported on-line. We were still using Delphi when I retired two years ago.

I suppose I could still learn C/C++ but I am not enthusiastic about the idea.


----------



## gunrunnerjohn (Nov 10, 2010)

I went the other way. I worked for IBM in the 60's, and I was the Phila office software support specialist. My principal focus was communications support, but I supported Cobol, Fortran, and PL/1 issues as well. Truthfully, most of my work ended up being in Assembler back in those days. I left to go with a startup company for a few years, we sold out and I moved on. I started doing consulting at Univac and they sent the whole project to learn C, and I never looked back. 

I spent many years in the Aerospace field, and all our projects were either assembler or C, and the FAA pushed very hard for less assembler and more C. It's good enough to manage air data, cockpit instrumentation, and fire the weapons off an F18, so I suspect it's good enough for me.  

If you're not a C programmer now, I can see the reluctance to jump into that pit.


----------



## John Hill (Nov 6, 2012)

I had a bit of exposure to IBM comms protocols including 3270. One of the projects I worked on was a security and systems monitoring system that intercepted a 3270 poll and responded with the status data before returning the circuit to the terminal device.


----------



## gunrunnerjohn (Nov 10, 2010)

A very popular terminal in it's day.


----------



## John Hill (Nov 6, 2012)

OK, I seem to be defeated by the sound card by the way the sample rates are controlled. So now I am looking at the RS232 serial port and the 8250 UART......


----------



## gunrunnerjohn (Nov 10, 2010)

You're getting closer to my stand-alone uP solution. 

I've never thought about trying to generate such a waveform from the 8250, I'd have to think about that. I'd recommend the 16550 as a better choice, since the buffering may be useful to keep the stream coming with momentary delays.

Are you thinking of sending specific binary patterns to generate the waveforms? Did you take into account that the start bit is always zero and the stop bit is always one?


----------



## John Hill (Nov 6, 2012)

No John, I am still looking for minimum hardware!!!

The 8250 is the minimum UART and hopefully the buffered UARTs will be compatible with what I have in mind.

I am working on a scheme to send the DCC out the TXD line, it would be much simpler to just key one of the modem control lines but that requires a real time service from the PC which gets a bit out of my league in the Winderz world.

Most of the possible bit sequences have been solved but there are still a couple to find a way around. Yes, I am taking the stop and start bits into account. It is fortunate that the '0' DCC bit can be of varied length.

If the PC port is true V24 levels (not just 0 to 5 volt) I should be able to drive the LMD18200 direct!


----------



## gunrunnerjohn (Nov 10, 2010)

The buffered UART is the same pinout as the 8250, it's just a newer part. There's no downside to using the buffered part.

The PC serial port has standard RS-232 voltage levels, it's not just a 5V port. Some older laptops took shortcuts here, but I've never seen a desktop with anything but standard outputs.


----------



## John Hill (Nov 6, 2012)

Hi, I will be assuming an 8250 (though it is hard to imagine there are many XTs still around!).

I now have a scheme to get around the difficult bit sequences but I need to figure out the logic I can use when I write the code, plus I need to review the NMRA documents to ensure I have not overlooked something.

This whole idea might still prove to be impossible but I am confident right now. There is a list of higher priority tasks, and the occasional highest priority interrupt, to be attended to too. Retired= flat out doing nothing!


----------



## gunrunnerjohn (Nov 10, 2010)

I haven't seen an XT in a loooooong time. 

One issue with using the timing on the serial port is finding machines with a serial port! I've never looked at the USB<->Serial adapters output to see if they have any gaps in the data stream. The reason for the buffered UART is that Windows isn't a real-time system, so you may have stuff that interrupts the output if you don't have some characters queued up for the output.


----------



## John Hill (Nov 6, 2012)

The uncertainty I have with the USB serial adapters is that (as far as I know) USB is very 'bursty' and if I use too big a blocks there might be a lot of data in the adapter to be sent when the control is changed giving a noticeable delay with trains responding to user input. But if I make the blocks smaller that will put more demands on Winderz and might lead to breaks in the data.

I have been unable to get to the NMRA site today to read the specifications but I think there is a period that the train should continue 'as is' when there is a break in signal stream, so maybe gaps in data is not a real issue.

Time will tell..


----------



## gunrunnerjohn (Nov 10, 2010)

We look forward to your report.


----------



## John Hill (Nov 6, 2012)

gunrunnerjohn said:


> We look forward to your report.


The report makes dismal reading John, a big fat 'F' for FAIL!:laugh:

I cannot figure out a way to handle a few bit sequences and they can not be excluded the whole project is now officially dead, or at least dormant!

John


----------



## gunrunnerjohn (Nov 10, 2010)

Remember that Ardunio I mentioned?


----------



## waltr (Aug 15, 2011)

Thanks for the report and your effort on trying this.



gunrunnerjohn said:


> Remember that Ardunio I mentioned?


This would work and you already have the H-bridge to drive the track.


----------

