The Rusty Spike

A Railroad Fan's Website

By

Testing out the Launchpad

Well the two launchpads i ordered arrived via FEDEX 2nd Day shipping. Not bad for just under $9 USD! After the kids were in bed, I sat down for about two hours to play with the code and figure some things out.

The very first thing to jump out at me was the opportunity to make the code much easier to write and read by way of abstracting the super technical stuff into a library of sorts. I’m no where near done, but here’s the initial header and source files for the ‘library’ I’m creating.

lp4mrr.h

//
// Launchpad for Model Railroaders (LP4MRR) Library
// COPYRIGHT 2013 Dave Loman
//                                
// Provided  under a Creative Commons Attribution, Non-Commercial  
// Share-Alike,3.0 Unported License                          
//
// TARGETED TO MSP430 LANUCHPAD W/MSP430G2553 PROCESSOR
//
//
 
 
// ensure this library description is only included once
#ifndef LP4MRR_H
#define LP4MRR_H
 
 
#define INPUT  0x00
#define OUTPUT 0x01
 
#define ON     0x00
#define OFF    0x01
#define TOGGLE 0x02
 
 
void setP1PinMode(int pin, int mode);
void setP2PinMode(int pin, int mode);
void setP1PinState(int pin, int state);
void setP2PinState(int pin, int state);
void waitabit(volatile unsigned int cycles);
 
 
#endif

 

lp4mrr.c

//
// Launchpad for Model Railroaders (LP4MRR) Library
// COPYRIGHT 2013 Dave Loman
//                                
// Provided  under a Creative Commons Attribution, Non-Commercial  
// Share-Alike,3.0 Unported License                          
//
// TARGETED TO MSP430 LANUCHPAD W/MSP430G2553 PROCESSOR
//
//
 
#include "lp4mrr.h"
#include <msp430.h>
 
void setP1PinMode(int pin, int mode) {
	if (mode == INPUT )
		P1DIR &= ~(0x01 << pin);
	else
		P1DIR |= (0x01 << pin);
}
 
void setP2PinMode(int pin, int mode) {
	if (mode == INPUT )
		P2DIR &= ~(0x01 << pin);
	else
		P2DIR |= (0x01 << pin);
}
 
 
void setP1PinState(int pin, int state) {
	if (state == OFF )
		P1OUT &= ~(0x01 << pin);
	else if (state == ON )
		P1OUT |= (0x01 << pin);
	else
		P1OUT ^= (0x01 << pin);
}
 
void setP2PinState(int pin, int state) {
	if (state == OFF )
		P2OUT &= ~(0x01 << pin);
	else if (state == ON )
		P2OUT |= (0x01 << pin);
	else
		P2OUT ^= (0x01 << pin);
}
 
// delay utility
void waitabit(volatile unsigned int cycles) {
	do cycles--;
	while(cycles != 0);
}
 
//application entrypoint
int main(void) {
	setup();
 
	int msElapsed = 0;
	for(;;) {
		//TODO: calc elapsed time and pass to loop
		loop(msElapsed);
	}
 
	return 0;
}

 

Now, the reason why I’m trying to isolate all the hard to read bit-level manipulation in a library is because it will make the actual application much easier to write. My initial application was reduced to this code:

blink.c

//
// COPYRIGHT 2013 Dave Loman
//                                
// Provided  under a Creative Commons Attribution, Non-Commercial  
// Share-Alike,3.0 Unported License                          
//
// TARGETED TO MSP430 LANUCHPAD W/MSP430G2553 PROCESSOR
//
 
#include <msp430.h> //Import support for talking to/about the MSP430 chip
#include "lp4mrr.h"  //Import our library functions, defines and constants
 
void setup() {
	WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
                                  // TODO: Make this a standard library call.
 
	// Loop across pins P1.0-P1.4 
        // setting direction to OUTPUT and pin state to OFF
	int a;
	for (a = 0; a <= 5 ; ++a) {
		setP1PinMode(a, OUTPUT);
		setP1PinState(a, OFF);
	}
}
 
void loop(int msElapsed) {
	int a;
	for (a = 0; a <= 5 ; ++a) { // Loop across pins 0 - 5.
		setP1PinState(a, TOGGLE); // Toggle the current pin's state
                                          // by using TOGGLE.  Need not 
                                          // know what state it is in.
		waitabit(0x0FFF); // 0x0FFF is the hexadecimal 
                                  // equivalent of the number 4095
	}
}

 

And produced this result:

 

The massive reduction of the amount of code that does the ‘actual’ work we want the launchpad to do is well worth the effort in making a library, in my opinion! Now on to actually USEFUL model railroad applications of the Launchpad…. Next step, I think, will be to implement an accurate Real Time Clock so we can pass in milliseconds elapsed since last loop pass.

2 Responses to Testing out the Launchpad

  1. Excellent stuff! I do particularly like your separating out the libraries. Makes the actual functioning code much easier to read and use.

    I assume you are using a Windows PC for this? But where did you pick up the breadboards? Just wondering if Radio Shack has them.

  2. Dave L Dave L says:

    Currently running on a PC, winderz 8. I will eventually resurrect my Ubuntu box and return to the land of sanity 🙂

    I honestly don’t know where i picked up that bread board. It was many many moons ago! I’m pretty sure Radio $hack has some types of bread boards, but not too sure about the cost. http://www.sparkfun.com is also a good, but not cheap, place to get things from.

Leave a Reply

Your email address will not be published. Required fields are marked *