Sunday, December 23, 2012

The LaunchPad for Model Railroading



No, I'm not talking about some cheesy "action" accessory made by Lionel in the sixties.  What I am talking about is a microcontroller development board from Texas Instruments that has the potential for scores of uses on a model railroad.  The Texas Instruments MSP430 Launch Pad (MSP-EXP430G)

A microcontroller is, essentially, a computer system on a single chip, including some hardware that ordinarily would be external to and would have to be connected to a conventional microcomputer.  Today's microcontrollers can be essentially as fast or faster than the '486 generation of microcomputers. Whether you know it or not microcontrollers are embedded into many of the systems model railroaders use today.  They are in each DCC decoder, in the command station and handhelds.  Accessory decoders and the products from Tam Valley Depot are built around them as well.

What's unique about the Launch Pad is that you can buy one from TI for $4.30 - that's not a misprint.  For less than $5 you get a development board, two target microcontrollers and some accessories.  This makes the TI board much cheaper that similar products from the competing microcontroller lines and it's cheap enough that the Launch Pad can be incorporated right into your projects.

What can you do with one?  Among other things, microcontrollers can directly light LEDs therefore they can be used for all manner of things like crossing flashers, traffic light sequencers and signaling systems.  In addition to illuminating the lights, the microcontroller can incorporate the logic to determine which lights to turn on.  You can program one to sequence the lights inside of a building or factory to imply activity inside. The logic for a bi-directional, three color signaling system is created using a surprisingly few lines of code.  In addition, with a few external components the same microcontroller can also detect the presence of the trains.

Turnout control using either stall motor (Tortoise) machines or servos is possible.  There are accessory boards available for the Launch Pad that enable it to store and playback sounds, opening the possibility to add sound sequences to your scenes.  Because the Launch Pad has a USB port on board that is accessible from the target microcontroller, the potential exists for tying many Launch Pads together into a network so that they can be controlled from a central location, or tied into a system like JMRI.

The microcontroller has to be programmed to function. The programming language (C or assembler) and environment is available free from TI.  If you cannot program, don't despair  I'll address that shortly.  Because these can be programmed, you can customize one for whatever functions you need.  Need B&O four-color signalling with all six marker indications (about 37 different signal aspects including flashing)?  You can program that complex signal system into one of these - and in many cases you can do it without additional hardware.

So what if you cannot program?  Well you could learn.  I hadn't programmed anything in almost 25 years before I took to programming the Launch Pad; and I had a couple of projects up and running in two days - including the time it took me to learn the "C" programming language.  I will be trying to cajole some of my buddies at Model Rail Radio to create some tutorials on how to learn and program in C.

For those of you who don't want to program, but want to give this technology a try anyway, I'll be putting up the programs that I create, and hopefully the creations of others, onto a website where people can come to share and use the code created by all.  In this way I hope to foster a community where we can share applications for the TI Lanuch Pad (and other microcontrollers) for model railroad use.

The code below, for an alternate crossing flasher, is the first such submission.  This code is provided under a Creative Commons license, which means that you can use it, modify it and redistribute it with attribution to the original author.  And your modifications must be shared freely. You cannot use it, even if modified, for commercial purposes.

This program will flash the on-board LEDs in an alternate pattern, as used in a RR crossing flasher.  In addition to the on-board LEDs, two of the microcontroller's pins (pins 6 and 7) are toggled so that if you connected a LED with series resistor between the pin and circuit ground, the LEDs will flash as well.

Here's a short video of the program in operation. BTW that's a small travel mouse along side the Launch Pad to give you some sense of it's size.




The program is almost as simple as any program can get. After some set-up formalities, the program is comprised of  two loops inside of an infinite loop.  In the first loop one LED and a pin is turned on and the other LED and a pin is turned off, the loop counts for the duration of the flash.  Then the second loop executes and turns off the first LED and pin while turning on the other LED and pin.  The second loop counts for a similar time to keep these LEDs on.  Finally the outer loop takes over and starts at the beginning; and it will do this indefinitely so long as the circuit is powered up.  It's brute force and inelegant, but it is simple to understand.

A comment about reading the program.  Anything after a // is a comment and is not read by compiler (the compiler converts the statements that are written in the code into a numerical sequence that the microcontroller reads and acts upon).  It's there only for you to read for understanding. 

To use this program you will have to download TI's Code Composer Studio (CCS) and install it on to your computer. You can download the free version - which has some limitations that are not likely to effect the programming that you will do - from this link.

Code Composer Studio

Most likely you'll want the windows version.  Installing this in your machine may require that you sign up for an account with TI; don't worry, no credit card or anything like that is required.

Once you have CCS installed (it takes rather a lot of time to install and wherever you start it), download the code file from my Microsoft Skydrive at this link:

Flasher code

Go to the "File" menu in the upper left, pull it down and choose "Save As";  from save as you will be given an option to download the file.  Open the MS Word file that you download, cut and paste the program into the "main.c" window of CCS.  Do not cut and paste from this blog post, because of embedded HTML characters it will not work.     These instructions will resume below, after the code listing.


//
//      
//
//
//             ALTERNATE CROSSING FLASHER
//
//            COPYRIGHT 2012 T. TERRANCE
//
// Provided  under a Creative Commons, Attribution,  
//       Non-Commercial, Share Alike, 3.0 Unported License
//
//
//      TARGETED TO MSP430 LANUCHPAD          
//      W/MSP430G2553 PROCESSOR
//              
//

/*
 * main.c
 */
#include <msp430g2553.h>


volatile long t=0;            //Define loop counter t and set it to 0
volatile long p=15000;  //Define flash period variable P and set it to 15000



void main(void){



WDTCTL = WDTPW + WDTHOLD;  //Stop Watchdog Timer



P1DIR |= BIT0;  //sets pin 1.0 RED on-board LED for output and turns it on
P1DIR |= BIT6;  //sets pin 1.6 Green on-board LED for output and turns it on
P1DIR |= BIT4;  //sets pin 6 (port 1.4) for output and turns it on
P1DIR |= BIT5;  //sets pin 7 (port 1.5) for output and turns it on


for (;;)  //start infinite loop
{


for (t;  t<p; t++)  // loop 1
{


P1OUT &= BIT0;  //Turn off on-board red LED 0 FOR DURATION OF TIMER 1
P1OUT &= BIT4;  //Turn off pin 6 (port 1.4) FOR DURATION OF TIMER 1
P1OUT |= BIT6;  // TURN ON on-board green LED FOR DURATION OF TIMER 1
P1OUT |= BIT5;  // TURN ON pin 7 (port 1.5) FOR DURATION OF TIMER 1
}


t=0;  //set t to zero


for (t;  t<p; t++)  // loop 2
{
P1OUT &= ~BIT6;  //Turn off on-board green LED for duration of timer 2
P1OUT &= ~BIT5;  //TURN OFF Pin 7 (port 1.5) FOR DURATION OF TIMER 2
P1OUT |= BIT0;   //TURN on on-board red LED FOR DURATION OF TIMER 2
P1OUT |= BIT4;   //TURN ON pin 6 (port 1.4) FOR DURATION OF TIMER 2
}


t=0;  //set t to zero



}  //end of infinite loop



}  //end of main




Creative Commons License
Alternate Flasher by Terry Terrance is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.



The convention for writing C code includes indenting the lines of code to highlight the nested loops.  My original code was so indented, but Blogger took out the tab characters. 
 
Connect your Launch Pad to your computer via the USB cable included with it.  A demonstration program is loaded into the Launch Pad that, coincidentally, causes the on-board LEDs to flash.  You can stop the flashing by pushing the push button nearest the LEDs.

Now in CCS pull down the "Project" menu and click on "Build Project".  The build process will take a few seconds during which a pop up window will appear and then disappear.  After the build has finished go to the "Run" menu and click on "Debug".  A pop-up window will appear complaining about the lack of code to support TI ultra low power mode.  Since we are not developing this project for battery power you can just click on "Proceed" to ignore this warning.  A few more pop-ups will come and go of their own accord.

The code will be loaded onto your Launch Pad automatically.  Once it's loaded the Debug window will appear as the topmost window on your screen.  In the top bar of the debug window at the far right you'll see some controls that look like those on a DVD player: a play button, a pause button and a stop button.  When the play button is green and yellow, you can click on it and the LEDs should start flashing.  Congrats, you've successfully compiled and loaded a program.

Pushing the pause button will pause the program's execution.  Depending on when you hit the pause button, the red or the green LED may stay on; I even once paused the program during the tiny amount of time that both LEDs were off.  Pushing the Stop button will disconnect your computer from the running program.  The program is still running however, and the LEDs will continue to flash until you disconnect the USB cable, thereby removing power.

The program is now loaded into the micro controller and will stay there until replaced by another program.  You can prove this to yourself by disconnecting the Launch Pad, shutting down CCS, then reconnecting the Launch Pad.  The LEDs will begin to flash and continue until disconnected.

For your first foray into modifying a program, change the value of the variable "p" which controls the timing loops.   Make p larger or smaller (in increments of 1000) to change the flash rate.  Rebuild the project as above, load it and see the results.

There are many resources for the TI Launch Pad on line.  There are videos on both the TI website and You Tube; although the ones at TI are hard to find.  There are user groups for the Launch Pad as well as products made to interface to the Launch Pad, they're called "Booster Packs".

I'll be putting up more information on the Launch Pad for model railroads as well as a dedicated website and/or blog.  But for now this entry is running long so I'll end it here.

.


No comments:

Post a Comment