connecting a Picaxe 28 X2 to a DCC system

sierrasmith71

New Member
I have designed a model railroad turntable controller using a 28X2 and a stepper motor. The user interface uses a 4 x 20 serial LCD, a 4 bit BCD rotary encoder - to select the track and end to be moved- two push button monentary switches and a two postion toggle switch.

I keep getting diverted --more functions or different motor drive and it is not finished yet. This past Friday I began wondering how I could use a model railroad DCC system to contol the turntable? This requires that the Picaxe reads the DCC commands from the tracks or command bus. Looking into this lead me to conclude this is not possible (directly) with the Picaxe as it is much to slow to process DCC commands on the fly. Well, what to do?

I had a flash of inspiration --" Since the motor drive output of a locomotive DCC decode is a PWM constant voltage, why not try to read the width of these pulses with a pulsin command? " If successful I could use the motor speed "steps" for track selection data for my controller and use the selectable DCC function outputs as inputs to replace the present switches.

After the required messing about and research I was able to successfully read and decode DCC 28 speed steps with the 28X2.

(This is using a loco DCC decoder as a stationary decoder --no motor is connected. )
The red and black wires are connected to the DCC tracks or bus.
I connected the orange (Motor+) and the grey (Motor-) with a 10K resistor, and a 33k res. from the orange (motor+) to pin c.0 on the Picaxe. The voltage of the pulse is about 13.5 volts and after searching the forums and the PIC reference info I concluded that it was safe to do it this way.

I used the following code to read the data and display it as a track # . There is a one to one relationship between the loco speed shown on the DCC system and what the LCD displays as Track #... Sweet.

I used an inexpensive Bachmann decoder ($12.00)and set it for NO acceleration or Decelaeration and a start voltage of 0.

The function outputs on the decoder are open collector type, so a resistor, 1 k or so, needs to connected between the function positive common and the function outputs. The logic of selection will be inverted --function "ON" will result in a low output. The Bachmannn decoder only has a single function out put so I will have to purchase a four function decoder ($17.00) to meet my needs.

The good news is I can control the turntable with a DCC system; I just have stop adding features and finish the job!

David G.

Maryland, US


Code:
PICAXE 28X2
'Note the pulse width readings are noisy and wander a bit so I use  averaging to calm them down a bit.
'The use of Select Case command makes it easy to assign the correct number to the read pulse width.  
'I tried using an optical coupler to connect to the DCC decoder but it did not lower the noise. I will look
'into the sourse of the noise --I have to borrow a friends O'scope.  

symbol pulse  = w0

symbol sum = w2
symbol average = b11
symbol number = b10



init:
	high c.7 							' Be sure Tx Pin is idle for some time
		   
   	Pause 1000							' wait for  LCD to boot
	

   
   	SerOut c.7, T9600, ("?f")				' clear the LCD and home the cursor

main:

	sum = 0							'clear sum
	
	
	for average  = 0 to 4						'set up for loop 
	
	 pulsin c.0, 1, pulse					'read pulse width and put in "pulse"
	 
	 sum = sum + pulse 					'build up average 
	 
	next 
	 
	 
	pulse = sum / 5						'calculate average
	 
	 
	  rem SerOut c.7, T9600, ("?f")			I use this code to read what the pulse width is for each speed step. 
	  rem SerOut c.7, T9600, ("pulse width  ")	
	  rem SerOut c.7, T9600, (#w0)
	  rem goto main
	 
	 select case w0						'set up select casr for read pulse widths
	 	
	 case 0 to 39
									'test for 0 -39 value of pulse width
	 number = 0							'if in this range set number to 0 and display result
	 SerOut c.7, T9600, ("?f")				'clear LCD and home cursor
	 SerOut c.7, T9600, ("Track Number  ")		'display Track Number
	 SerOut c.7, T9600, ("N/A")				'display N/A --No Track 0!
	  						
	 
	 case 40 to 65						'test for 40 -65 value of pulse width
	  number = 1:gosub lcd_out					'if in this range set number to 1 and display results
	  
	 
	 case 90 to 120
	  number = 2:gosub lcd_out					'Etc, etc....
	  
	 
	 case 145 to 169
	  number = 3:gosub lcd_out	
	  
	 case 195 to 220
	  number = 4:gosub lcd_out	
	  
	  
	 case 250 to 279
	  number = 5:gosub lcd_out	
	  
	 
	 case 320 to 360
	  number = 6:gosub lcd_out		  
	 
	 case 400 to 440
	  number =7:gosub lcd_out		 
	 
	 
	 case 490 to 515
	  number =8:gosub lcd_out		  
	 
	 
	 case 560 to 590
	  number =9:gosub lcd_out	
	  
	 
	 
	 case 640 to 670
	  number =10:gosub lcd_out		  
	 
	 case 740 to 780
	  number =11:gosub lcd_out		  
	 
	 
	 case 850 to 880
	  number =12:gosub lcd_out	
	  
	 
	 case 950 to 980
	  number =13:gosub lcd_out	
	  
	 
	 
	 case 1050 to 1080
	  number =14:gosub lcd_out	
	  
	 
	 case 1130 to 1200
	  number =15:gosub lcd_out	
	  
	 
	 case 1290 to 1340
	  number =16:gosub lcd_out	
	  
	 
	 case 1420 to 1440 
	  number =17:gosub lcd_out	

	 
	 
	 case 1550 to 1580
	  number =18:gosub lcd_out	
	  
	 
	 case 1680 to 1720
	  number =19:gosub lcd_out	
	  
	 
	 case 1800 to 1845
	  number =20:gosub lcd_out	
	  
	  
	 case 1950 to  1999
	  number =21:gosub lcd_out	
	  
	 
	 case 2120 to 2150
	  number =22:gosub lcd_out	
	  
	 
	 case 2240 to 2285
	  number =23:gosub lcd_out	
	  
	 
	 case 2410 to 2455
	  number=24:gosub lcd_out	
	  
	  
	 case 2580 to 2630
	  number =25:gosub lcd_out	
	  
	 
	 case 2670 to 2710
	  number =26:gosub lcd_out	
	  
	 
	 case 3050 to 3090
	  number =27:gosub lcd_out	
	  
	 
	 case 3300 to 3350
	  number =28:gosub lcd_out	
	  
	 
	 ENDSELECT
	 
	 goto main							'start over!
	 
lcd_out:

	 SerOut c.7, T9600, ("?f")				'clear LCD and home cursor
	 SerOut c.7, T9600, ("Track Number  ")		'display Track Number
	 SerOut c.7, T9600, (#number)				'display number value as a number
	 return
 

MPep

Senior Member
Hi David.
Congratulations. May I suggest you post this inthe Finished Projects part of the forum? Will be easier to locate later.

MPep.
 

westaust55

Moderator
Hello David,
congratulations on the turntable project.

I have visions of one day controlling my model railway with PICAXE chips.

Any chance you might add some photos of your project.

Maybe the Moderators can move this to the finished projects area
rather than sierrasmith71 having to repost.
 
Last edited:
Top