17 May 2010

Writing a first program

The typical "Hello, World!" of microcontrollers is a blinking LED. The F2132 target board has an LED connected to one of the pins on port 1 (P1.0, pin 21) by a jumper. Here's what a typical C program for this looks like:


/* blinkyF2132: test program toggling an LED connected to P1.0 on the
 * MSP430F2132.
 */


#include <msp430x21x2.h>


void main(void) {

int i;

WDTCTL = WDTPW + WDTHOLD;
P1DIR = 0x01;
P1OUT = 0x00;

for (;;) {
P1OUT ^= 0x01;

for (i=0; i<0xFFFF; i++) {
} // delay
} // infinite loop


} // main



  • The first thing to note is the included header file; all msp430 compilers come with a collection of these files that define specific tools and behavior for the various families of the controllers. In this case, since I am using an F2132, I use the x21x2 family header. (Edit: looking at the header file included in CCS4, this format for the header file is considered legacy. The preferred usage would be specific to the chip itself, so in this case you would include the msp430f2132.h header file.)
  • WDTCTL, P1DIR, P1OUT and the like are all convenient keywords defined in the header file.
    • WDTCTL points to the word (2 bytes) controlling the watchdog timer. This timer resets the chip after a certain amount of time has passed. To keep our program running, it needs to be disabled. The 8 least significant bits (0 through 7) of WDTCTL control the behavior of the watchdog timer. Bit 7 specifically puts the timer on hold if set to 1. In the header file, WDTHOLD is defined to be a 2 byte value with only the 7th bit set to 1 (ie. 0b0000000010000000, or in hex, 0x0080). By writing WDTHOLD to WDTCTL, we turn off the timer.
    • WDTCTL only uses 1 byte for its control; the 8 most significant bits are set up as a key to prevent a program from inadvertently changing the configuration. Hence, to write to WDTCTL you need to include a password of sorts. This password is the value 0x5a, and the header defines WDTPW to be 0x5a00. By adding this value to WDTHOLD you end up writing the value 0x5a80 to WDTCTL--which satisfies the necessary key and sets the hold bit to 1. (Note that you could also use WDTPW | WDTHOLD instead of adding them. They have the same effect in this case, but the TI examples use +, so I chose to use that convention.)
    • Each port can configure each of its 8 pins to be inputs or outputs individually. On the target board, the LED is on P1.0, so P1DIR is set to have bit 0 to be the value 1 (output) and each of the other bits 0 (input). Writing 0x01 (0b00000001) to P1DIR configures it this way.
    • P1OUT controls the digital values on the output pins of P1. In this program, 0x00 sets the outputs all to zero, and 0x01 sets all but pin 0 to zero with pin 0 at 1. The LED is connected with its cathode at ground, so a high value on P1.0 turns on the LED. Thus 0x00 is off, 0x01 is on.
  • After setting up the configuration of the MCU, the program then enters the infinite loop: for (;;) { }. This step may seem odd at first, but since we want our controller to keep working, we can't have a program that terminates. Practically, without the loop the MCU would then continue trying to execute statements in the next memory spaces, eventually reading well beyond the actual program to unpredictable results.
  • Inside the infinite loop, the program changes the value on P1.0. The ^, which is the C symbol for the exclusive or operation, turns the LED on if it is off, and off if it is on. Another for loop causes a delay between toggles on the LED. The length of the delay depends on how far the iterator i has to count. (It also depends on the frequency at which the MCU is operating; a faster clock speed makes the iteration count faster.)


This program is very simple, and not really useful for much other than to reassure you that everything works right and that you can actually program the msp430. Below is a short video of my target board running the program. There are certainly more elegant ways to do this trivial task, but this method is a good way to start and doesn't require much understanding of the MCU itself.



Reader Exercise: Write a similar MSP430 program using the MSP430F2001 to alternate blinking LEDs on ports P1.2 and P1.7 (ie. LED 1 is on, LED 2 is off. After some time, they switch to LED 1 off, LED 2 on. The F2001 is a 14 pin MSP430 that comes in PDIP, TSSOP, and QFN packages. P1.2 is pin 4, P1.7 is pin 9.)

Software

A few options exist for programming the MSP430.  A number of professional compilers are available, but for easy home and hobby use there are also free versions.

  • IAR Embedded Workbench Kickstart (Referred to as EW430 in MSP430 Microcontroller Basics by John H. Davies)
    • The free version is limited to 4 kb code size
    • Works in Windows (32 and 64 bit versions)
  • Code Composer Studio v.4 (CCS4)
    • Free version is limited to 16 kb code size
    • Works in Windows (32 and 64 bit versions)
  • MSPGCC
    • Unlimited code size, as it's open source software
    • Works in Linux or Windows under Cygwin (32 bit)
I'll be working with CCS4, at least to start.  On a Mac system, the MSP430 is programmable by using any of the windows software through a virtual windows installation using VMWare Fusion.  Unfortunately, VMWare requires a license, and the free, open source VirtualBox does not work with the USB FET.  MSPGCC has been ported to Mac, though.

While I would prefer working in a linux environment, it seems mspgcc is difficult to use because of the state of msp430-gdbproxy, the software that communicates through the FET.  It does not yet support some of the newer families of MCUs, and takes a good deal of effort to get working properly.  It also does not seem to support 64 bit systems yet.

For now, CCS4 works quite well and so will be the IDE I use.  Very little of what will be documented here depends specifically on the environment, however, and so the choice is pretty arbitrary.  The 16 kb limit might sound small, but typical MCU programming is actually quite dense, and so a lot of projects can easily fit into that limit.

05 May 2010

Hardware

TI makes a large variety of types and packages for the MSP430, from a basic, 14-pin PDIP to a 113-pin BGA Microstar. Selecting the right MCU depends on a number of factors, including the number and types of peripherals and devices that need to be controlled.

For the initial experiments, we'll use the MSP430F2132 and the 20/28 pin target board produced by TI.  These can all be purchased in a single package, and can include the MSP-FET430UIF flash emulation tool for programming the MCU.  This target board works with the x21x1 20-pin TSSOP variations and the x21x2 28-pin TSSOP variations.  The package comes with 2 F2132's, which are the largest of the chips usable in this board.  The x21x2 is useful for basic projects as it includes most of what could be needed, including an analog to digital converter.  Nearly everything done at this site will be doable on nearly any of the MSP430's, however.  Sometimes a small chip (even the new and inexpensive G-series types) will be sufficient, and some of the larger chips (with more pins) may be helpful in future projects.

The F2132 features:
  • 16 MHz clock (at full voltage)
  • 8 kb flash memory
  • 512 b RAM
  • 24 General Purpose Input/Outputs
  • 10-bit SAR A/D Converter
  • Universal Serial Communication Interface (communicates via UART, LIN, IrDA, I2C, or SPI)
The target board is a useful format for testing without needing to create a custom PCB or solder the MCU to anything.  It includes a socket designed to hold the TSSOP package.  Included are header pins (male and female) to attach on either side of the socket as seen in the photo above.  I've chosen to use the female pins, which allows me to connect wires to devices easily.  It also provides some protection from an accidental short from a stray wire bridging two pins that the male pins would not provide.  The general rule of thumb in rockets is that anything that is powered when disconnected (intentionally or accidentally) should be female, and that's the convention used here.

Also on the board is a place to connect a 32.678 kHz crystal for timing.  The MCU has excellent timers internally, so the crystal is not always essential; it's good to have for time critical applications, though.  The kit includes a 12.5 pF surface mount crystal, though one flaw in the board design is that there is no convenient way to ground the crystal case.  One option might be to solder a wire to the case and fit into the via that connects to ground.  The crystal included in my kit actually broke off of its fine leads when I was playing around with figuring out how to attach it, so I have not put a crystal on my target board yet.  Plated through hole form factors may be usable since the mounting place for the crystal on the target board is PTH.  A quick note about the capacitance of crystals--it seems the MSP430, or at least newer versions of it, are able to set the load capacitance internally via software, so other capacitances may be used; I'll use 12.5 pF since that's what came with the kit initially.

In addition to the F2132, some experiments will make use of the MSP430F5438 and the Experimenter Board made by TI.  The F5438 is the chip being used in my research, and so this combination gives me a way to program that particular chip while also providing an environment with lots of external devices and controls to use in experiments, including an LCD, accelerometer, and various interface devices.

Both boards can be powered by the FET or externally.  A future post will look into various options for powering an MSP430 project, and many experiments will simply use a pair of alkaline AA batteries (3.0 V).  Note that two AA's do not provide sufficient voltage for the processor to run at the full 16 MHz.

The equipment described here will not necessarily be appropriate for everyone, but are a convenient and useful place for me to start.  As I stated earlier, the ideas approached here will be useful for other projects using different chips and boards as well.  Next time we'll take a look at the software needed and start loading a simple program onto the chip.

24 April 2010

Goals and Philosophy

Microcontrollers have become ubiquitous in the tools and gadgets we use every day.  There are a lot of fun and useful things that can be done with them, but for those without a strong background in electronics and electrical engineering, approaching a microcontroller project can be daunting at best.  I'll be using TI's MSP430 in a physics research project for my Ph.D.  While I have some technical background and a good, basic understanding of electronics, I've found that the information available to me from which I can learn to use microcontrollers only come from one of two camps: very simple (and practically useless) with the expectation that you learn by reading and using other people's code, or detailed (and incomprehensible) with the expectation that you already know how to work with microcontrollers.  This blog was established to record what I learn as I stumble through the available information and discover how to do control systems in digital electronics.  Hopefully it will also help to fill in the gap between the two types of help currently available.

TI's MSP430

There are a number of different types of microcontrollers and microprocessors on the market right now, and knowing which one is "best" to use is difficult, even for experienced designers.  In my case, the selection was simple as I was told the hardware I need to design would be based on the MSP430.  However, in researching the various types of chips, from PICs to AVRs to ARMs, I can see a great deal of benefit from the MSP430 design when approached from the perspective of a scientist wishing to develop instrumentation to help with both hobby and professional experiments and research.
  • Low power.  AVR fans sometimes argue about whether the MSP430 or some AVR models are better, but regardless it cannot be denied that one thing the MSP430 does well is manage power.  Especially for remote sensing capabilities, low power draw is a major advantage.
  • C programming.  I prefer C in this setting myself, and find it comfortable.  Other MCUs can also be done in C, so this is not a unique feature to the MSP430, but a benefit nonetheless.
  • 16 bit processing.  A lot of research situations require computation done on measurements.  If the computation is to be done in the hardware, 16 bit processing is much nicer than the 8 bits available in other common, low-cost MCUs.  It also simplifies the operation of the chip to an extent.
  • Built in peripherals.  For the scientist, it helps to not have to worry about connecting and interfacing lots of different components.  Consolidating ADC, DAC, memory, clocks, timers, comparators, and other useful tools into one chip makes design simpler, more compact, and more robust.
  • Simpler architecture.  The MSP430 uses a von Neumann architecture, which is easier to understand than the Harvard architecture used in PIC and AVR.  While outside the black box this makes little difference, for the scientist it's simpler to understand, leading to better designs over all.
  • Flash memory.  This is a feature commonly found in most modern MCUs, but is regardless an important feature, namely for its reusability.  A laboratory can become quite cluttered with old instruments and scrounged tools.  Careful design of MCU devices can make them reusable in ways and places not originally intended, and reprogramming the chip is easy.
TI has published a booklet describing the MSP430, its features and uses, as well as the types of chips available.  This document can be found on TI's website (slab034u.pdf, last tested 25 Nov 2011).  TI also has a great deal of useful documentation, examples, and tips.  Most of these can be found in pdf format with a filename similar to the one linked above.  Whenever possible, I will cite the document filename with a link to make it clear where the information is coming from.

First Project

With a clear path established, it helps to also set some clear goals and targets.  This blog will not detail the specific use of the MSP430 in my research, but rather the experiments and tests I do in learning how to use this microcontroller.  Many of the projects will likely be geared toward learning to use particular features or understanding particular characteristics.  With that background established, I'll move on to more specific uses and designs as I find things to do with the chip that would be fun, educational, and useful.

Keeping all of that in mind, the current goal is to use the MSP430 in an instrument that makes a simple measurement periodically and transmits the data to a computer via USB, where it is logged for analysis.  For simplicity, I think I will do something that requires a single voltage measurement, and examine the life of a standard consumer battery.  Some topics I expect to cover in the near future include:
  • powering the MCU
  • UART/SPI/I2C
  • interfacing to USB
  • interfacing to transducers
  • ADC/DAC
  • crystals and clocks
If some of these terms are unfamiliar, don't worry; I plan to explain each clearly as I come to the different topics in later posts.  Beyond this list, I expect to eventually look into LAN and wireless interfaces, radio transmission and reception of data (telemetry), low power modes, interrupts, JTAG, memory usage, LCD/VFD interfaces, PCB design, and GPS and other various transducers as I develop other side projects.  If I get ambitious, I may even look into more advanced designs using a 32 bit processor such as the ARM Cortex.  But that's very much getting ahead of myself.

If you've found this blog looking for help, I hope you'll find some of what you need here.  If you have experience with this type of work, I hope you'll comment, correct, or suggest whatever comes to your mind freely.  TI has put together a very nice and fairly inexpensive chip for us, and I think we'll find that it's an ideal environment for developing new scientific tools.