Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

This is a simple introduction to programming on the msp430 microcontroller, which was the main microcontroller for Midnight Sun XI

Prerequisites

  • All code running on our electrical system is written in C. It would be a good idea to become familiar with C before continuing with this tutorial. You can find many resources online about programming in C such as this one.
  • Install code composer studio (CCS)
    • Simply make an account on TI website, and after you have completed that find the download link for windows, linux, or mac (depending on what you use) from this site. Just run through the installer (default options should be fine) and you will have code composer studio installed.
  • A MSP430 EXP430F5529LP launchpad. (There is some available in the bay to use or you can purchase your own here).
  • Read Software 101: Clearing, Toggling, Setting, and Checking a Bit

Setting up a project

Go to the top menu bar a click File->New->CCS Project, then make sure the dialog that appears looks something like this:


Verify that the target and project name are correct and click finish to create the project.

Blinking an LED the slow way

To get started we are going to write some code to blink an LED on the launchpad. If you look at the launch pad board you can see a small LED beside one of the buttons labeled "LED1" with the label "P2.1" on top of it. The key piece of information here is "P2.1" This tells us that the LED is connected to "Port 2 Pin 1" on the microcontroller. That means to turn on the LED we just need to turn Port 2 Pin 1 on and off to blink that LED.

MSP430 IO Ports and Pins

General purpose input/output (GPIO) on the msp430 works through the use of PxDIR, PxOUT, and PxIN registers. These are 8 bit registers where each bit in the number corresponds to a certain pin. The "x" corresponds to which port we are currently trying to access, there can be any number of ports on an MSP430 (this is something you would look for on the datasheet) but right now we are only concerned about port 2. First lets look at the PxDIR register, or in this specific case P2DIR


P2DIR is an 8 bit registers (think of registers as special variables that control input and output in the microcontroller), so it would look something like this:

uint8_t P2DIR;

and since we are concerned about the bits in it, by default it would have a value like this in binary:


0b00000000


Now the right-most bit in the number is the first bit (BIT0), and it corresponds to the first pin (Pin 1) connected to that port, in this case that would be P2.1. Now when that bit is a 0 this tells the IO controller that we want this pin to be an output so that we can turn on the LED. If we were to set this bit to 1 making:


P2DIR |= BIT0
0b00000001

Then pin 1 would become an input, which would be useful to check if a button has been pressed. But since we are trying to turn the led on leave the bit as 0.

Since we know now how to configure the pin as an output, we must now turn on the output to turn the LED on. This is where the PxOUT (or specifically P2OUT in our case) register comes into play. Like P2DIR, P2OUT is an 8 bit register where each bit corresponds to a specific pin on the port. So just like P2DIR, BIT0 will correspond to pin 1 on port 2. That means to turn on the output of P2.1 we just have to set BIT0 to 1. and then to blink the led we just have to set BIT0 back to 0 and then back to 1 again. This is all the basic information we need to blink an LED, so now we can start by looking at the code below:

Code

#include <msp430.h> 

/*
 * main.c
 */
int main(void) {
  WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

  P2OUT &= ~BIT0;
  P2DIR |= BIT0;

  for (;;) {
    P2OUT ^= BIT0;
    __delay_cycles(10000);
  }
    
    return 0;
}


You can copy and paste this code into the main.c file of your project to get started, and then you can run it by hitting the debug button  () and then the run button ()  (a dialog box might appear telling you to update firmware, just click okay and let it update). The code should begin to run on the microcontroller and you will see the LED begin to blink, congratulations! Now lets go over the rest of the code that has not been explained yet, and figure out how and why it works.

  • No labels