DIY DCC question

ModelRailroadForums.com is a free Model Railroad Discussion Forum and photo gallery. We cover all scales and sizes of model railroads. Online since 2002, it's one of the oldest and largest model railroad forums on the web. Whether you're a master model railroader or just getting started, you'll find something of interest here.


Hello. I decided to build my own decoder based off the the MERG design. Now I need a litte help programing. I am not entirely new to ASM, but I am new to PIC programing. I understand how to turn my accessories on the decoder on and off, but I am not sure how to recieve and interpert DCC signals. Is there a good programing tutorial out there or can someone help me?

thanks
 
There may be but why would you want to build your own decoder when you can get good one already built and meeting NMRA standards for less tha $20? I guarantee you'll spend at least that in circuit boards and IC's alone. If you want to do this to try your hand at one, that's fine, but it makes no sense to figure out a way to equip a fleet of locomotives when the heavy lifting has already been done. I'd start at the NMRA web site in the decoder section and use the links from there.
 
The reason I built a decoder was price. For a simple decoder, you are right. Purchasing a new Digitrax decoder is much cheaper and easier. If you want sound, however, building a decoder is cheaper. Building is about $30 versus $100 for a good Tsunami. I have the sound code worked out, I just need a little help reading and understanding DCC signals.
 


I've got some experience programming PIC microcontrollers, and I'm reasonably familiar with the DCC standard. What is it that you want help with?
 
I am just wanting to make sure the idea I have in mind for reading and decoding the signals is correct.
Warning-- Long post ahead

I start off by setting up a counter in the PIC and check the state of Pin RA2 (the pin I have connected to the track). If the time of transition is between 52 and 64 microseconds, I store a 1 in a byte variable. I start checking again and check transition again and then OR that value with 02h (and then the 02h result with the first byte variable). I then repeat this process until I have a 8 bit long byte. If I AND the byte with 0FF,I should get a result of 0, representing a preamble. If not, I need to start reading data again until I find a preamble byte. If the byte was a preamble, I need to use the same method to read in the next byte. I need to AND that byte with my own address to see if the packet is for me. If it is, I need to read the next two bytes and store them. I will check the error packet first to make sure the data is good. If it is, I will use bit tests on the third packet to interpret what I am supposed to do. This whole time, I also need to be updating the function outputs, sound output, and motor output.

I think that is correct, but as I said, I have never really worked with PIC ASM before, and this is my first time working with this low of a level of serial work.

thanks
 
If I were doing this, I would definitely connect the track voltage (via a resistor!) to pin B.0. Then an interrupt could be generated when there's a transition there.

At a lowgoing tranition, I'd start a timer. At the highgoing transition, read the timer and see whether its contents indicated less than some threshold of time (say 80 microseconds??) or more. If less, it indicates a 1; if more, it's a 0. I don't see a reason to check for < 52 microseconds.

It seems easier to me to rotate a byte (the RLF instruction) instead of OR-ing successive masks into it. Note that the highest order bit arrives first! So if you do use the OR method, you'd have to set the mask first to 0x80, then 0x40, etc down to 0x01.
 
The reason I wasn't using interrupts to check times is the programs I am trying to base mine off of do not use the interrupts. Some of the first few instructions disable the interrupts.

http://www.merg.org.uk/resources/dcc.htm
http://www.tinet.cat/~fmco/download_en.html

However, looking through the MERG site (first link), I found an older version of the program that uses Interrupts. I am also thinking of switching to C instead of ASM because I know C a little better. I didn't think C would handle the low level work as well, but after talking with some people, I think I might do better. You are right on the masking. I forgot I could just roll the bits. I am too use to higher level conversions.

thanks
 
The idea of writing a complex program in assembler makes me shudder. Definitely, do it in C. I have a compiler for the PIC processor, though it's an old version that won't deal with newer components. But if I were starting out now, I'd probably use the AVR processor from Atmel. It's debatable which processor is better, but the main issue is that you can get a free compiler for the AVR, but if you use a PIC, there are only commercial ones.
 




Affiliate Disclosure: We may receive a commision from some of the links and ads shown on this website (Learn More Here)

Back
Top