Custom Search

MSP430 basic coding/programing part 1

Filed under electronics, Hardware, MSP430, programing, software

basic functionality can be achieved with minimal coding. In my attempt to learning how to get the different peripherals working on my msp~2231. All MSP430 value line come with a simple universal package of hardware, Basic Clock, Timer_A, GPIO, USI (SPI,I2C,UART), WDT. then there are chips with chip specific hardware like ADC10, Comparator, Timer_B, Sigma-Delta ADC. to get most of these to work, it takes very little code, but to make them useful that will take a little more skill.

We will start with everything after the header file #include msp430xxx.h , remember that this header file is chip specific.

Lets start Here- this is where most of your code will go, including setting up the clocks, pins, interrupts, and anything else that will be run.
void main(void) {
//program goes here
}


There are a few things we need to do before the watch dog timer runs out(WDT). to do this we need to change the bits in WDRCTL register to setup the WDT, other wise it may reboot and get in an endless loop.
Most of us will disable the WDT, by using WDTCTL = WDTPW + WDTHOLD;
now your wondering how this sets the Watch dog timer to disable.
WDTPW needs to be set, this is to access the WDT registers(password protected).
WDTHOLD stops the WDT and disables it, this will be used for most applications and programs, but WDT is very useful, if you need examples of other WDT code examples, you can download them at TI.com. This should prevent it from going into an endless loop at start up. *This needs to be done early in the program, if you have a lot of variables to set up you may want to initialize them farther down in the code if you have trouble with WDT.

Next we will set up the pins, this is pretty easy to do. This part of the code just sets the pins for input or output and active or not. There are quite a bit of registers to deal with but we will only go over the main ones,
PxIN, PxOUT, PxREN, PxDIR, PxSEL. The other ones PxIE, PxIES, PxIEG are intrupt settings and we will go over those later on. P1xxx registers are for the first 8 (0-7)pins.

PxSEL – selects the pin for use with a Hardware peripheral
PxDIR – switches the pin for input or output (0 for input, 1 for output)
PxIN – selects if input is High or Low, Default is low
PxOUT – selects if output is High or Low, Default is low
PxREN – selects if a pull up or pull down resistor is used, Pull up or Pull Down is selected by the PxOUT register

Now since we now know what the registers are we need to know a little binary with a little bit of bitwise operations

Bitwise operators

  • | OR  -        1010 | 1011 = 1011, if either bit is a 1 returns a 1 if both bits are 0 then value is 0 ( 1|0=1, 0|1=1, 0|0=0)
  • & AND –    1010 & 1011 = 1010, if both bits equal 1 returns a 1, if 1 bit is 0 and the other one is 1 then it returns 0, if both bits are 0 then it returns 0 (1&1=1, 1&0=0, 0&0=0)
  • ~ compliment ~1010=0101 switches the bits to the opposite value (~0=1, ~1=0)
  • ^ XOR  10101010^01001011 = 11100001, if either bit is 1 (not both) then it returns 1, if both values are 0 or both 1s then it returns 0  (1^0 = 1, o^1=1, 1^1 or 0^0 = 0)
  • << shift left     1010 << 2 = 1000, shifts the bits to the left by given number
  • >> shift right  1010 >> 2 = 0010, shifts the bits to the right by given number

Since we now have bitwise reference we can now show you how it applies to the pins.

at first I was having a hard time understanding bitwise, but once you figure its just moving 1s and 0s around it becomes a great tool. so lets get started with how to setup the pins.

P1DIR |= "register value"; valid values between 0-255, or 0×00 to 0xff
Example Code
P1DIR |= 0x0c; this sets up p1.2 and 1.3 for output.
To make things even easier, the MSPGCC headers have an easy way to setup pins with out knowing the ins and outs of other numbering systems. MSPGCC uses #defines to certain bits, as in BIT0 = 0×01 = 00000001 = 1. another example is BIT6 = 0×20 = 00100000 = 32, hopefully this is making sense a little more
Example code using BITx
P1DIR |= BIT0 + BIT6; This activate P1.6 or pin 7 and P1.0 or pin 1 (pin numbers may not correspond to Physical pin layout). So you may be asking why I am using an OR operator, I have no idea,all the examples from IAR workbench set the pins this way, but if you do have one let me know. LOL.
Now lets put that together with the rest of the pin settings.
P1DIR |= BIT5; //0=in 1=out, sets P1.5 to output
P1OUT |=BIT5; //1 = high 0 = low, sets P1.5 to high output

This will get you P1.5 or pin 6 to go to high, or if you hook it up to an LED it will go on. At the end we will have sample program that puts this all together, well most of it.
The full code to get a pin up or input or output
P1DIR |= BITx + BITx;
P1OUT |= BITx + BITx; //for input P1IN |= BITx + BITx
this will get basic functionality to your pins, you will need more code to make it functional. We still need to setup the power mode or any other peripherals.

The different power modes give you a lot of options when it comes to low power applications, it also affects what clocks are active and if the CPU is on or off (some periphalals can run with out the cpu).
to set the different modes you call _bis_SR_register("power modes + other settings") *this is for mpsgcc 4.x

the different power modes are LPM0-LPM4 and AM. Each one has its advantage for power saving, but im not the one for power efficiency.

  • AM- CPU active, all clocks are active
  • LPM0-CPU and MCLK are disabled, SMCLK & ACLK are active
  • LMP1- CPU, MCLK are disabled DCO, and DC generator are disabled unless DCO is used for SMCLK, ACLK is active
  • LPM2-CPU, DCO SMCLK, MCLK are disabled, ACLK and DC generator are active
  • LPM3-ACLK is active, CPU, DCO, SMCLK, MCLK and DC generator are disabled
  • LPM4-CPU and all clocks are disabled

Now i will leave it up to you, or which power mode to use for you application. this now finishes up the quick walk through to get some blinking lights from your MPS430 chip. I will continue with Timer_A, ADC10, and other peripherals. Here is a quick program to blink lights with software so you can start experimenting with your chip.
#include <msp430x20x2.h> //header for chip 2231
void main(void) {
WDCTL = WDTPW + WDTHOLD; //disable WDT
P1DIR |= BIT0; //sets pin 1.0 for output
for (;;) {
volatile unsigned int i;
P1OUT ^= BIT0; //toggles pin 1 with exclusive or (XOR)
i = 5000;
do (i--);
while (i != 0);
} //end of for()
}//end of main