RSS

Monthly Archives: August 2011

MSP430 io programming

In my previous post we saw how a led and a switch can be used.There is another way to use the individual bits of the pin.These are defined in the header file of the device.Suppose we’ve to check if a switch is pressed and switch on led then we can use the code similar to the one we wrote earlier where we did bitwise and on the P1IN register to find the value or do the following.

#include "io430.h"
#define           led             P1OUT_bit.P1OUT_0
 #define          switch       P1IN_bit.P1IN_3 
 #define          on               1
 #define          off               0
int main( void )
 {
  // Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
  P1DIR=BIT0+BIT6;               // make pin 0 and 6 output
  P1OUT=0x00;                         // switch off  led
while(1)
  {
  if(switch == 0)
  led=on;
  else
  led=off; 
 }
 }

In the above code we’ve defined macros which refer to the particular pin of a port and whether it is input or output.These are present in the file io430x202.h which gets added by default.If the file is not getting added then it can be added from the folder

C:\Program Files\IAR Systems\Embedded Workbench 5.4 Kickstart\430\inc\

select the required file and add it to the source.

 
1 Comment

Posted by on August 29, 2011 in Flip, MSP430 Tutorials

 

Tags: ,

MSP430 LED’s & Switches

Now that we have the board and software ready lets blink a few led’s and switches present on the Launch Pad.The Launch pad comes with 2 LED’s ,

  1. green connected to the pin P1.0
  2. red connected to the pin P1.6.

There are 2 switches ,

  1. S1 connected to reset
  2. S2 connected to pin P1.3.

The IO’s are memory mapped in msp430 and hence no separate instructions are required to control them. The Ports are usually related to the following registers:.

>> PxDIR                    >> PxIE

>> PxOUT                  >> PxIFG

>> PxIN                       >> PxIES

>> PxSEL                      >> PxREN

We need to understand the first three for controlling the IO’s.

The PxDIR register is used to configure a particular port pin as input or output.Each bit corresponds to a pin on the port.Setting a particular bit makes that particular pin output and resetting it makes it input i.e,
1  =  output
0 =  input.       So to make the first pin P1.0 & pin P1.6 as an output we set the bits and to make the 3rd pin P1.3 input we reset the bit. So we write 0x41 to the register P1DIR  i.e, binary 0100 0001.

To write the values to the pins we set a bit in the PxOUT register to make the pin high and reset the bit to make it low.So to make the leds’s light up, which are active high we set the first and the sixth bit.The following code is used

P1OUT      =     0x41;             or           P1OUT    =    BIT0 + BIT6;

the second format makes use of the macros defined in the library files of the particular device.BIT0 is equal to 0x01 and BIT6 is equal to 0x40.These are easy to use and to understand and are defined from BIT0 to BIT15 with values 0x00 – 0xff.There are other macros defined for other registers as well which we will be seeing some other time.

To read a particular value we need to mask the PxIN register to check the bit.Since here we need to check the fourth pin or pin P1.3.This is done as follows.

(P1IN  & BIT3)

the above expression equates to a 8 bit value which will be 0 if the 4th pin is reset and equal to 0x08 if the pin is set.If the expression is equated to these values in a if condition then the required action can be written in the body of the loop.

Now coming to blinking an led.The following code is written.The code is self explanatory.

#include "io430.h"
int main( void )
  {
    int i;
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR=BIT0+BIT6;         // make pin 0 and 6 output
    P1OUT=BIT0+BIT6;        // make pin 0 and 6 high
    while(1)
    {
    P1OUT^=(BIT0+BIT6);    // toggle the led's
    for(i=0;i<9999;i++);    // delay between the toggle.
    }
   
  }

In the above code we XOR port 1 with BIT0 and BIT6 to toggle those particular bits.The loop is to give a delay so that toggling is seen.

Compile the code.It might give a warning as statement unreachable which is because of the return statement.Deleting it will give zero warnings.Then set the target options and hit download and debug.the Debug window will open.Press F5 to run the program or F11 to start Single step debugging.You can also click on menu option Debug > Go.

We see a line in the above code as WDCTL=WDTPW+WDTHOLD;

this is stop the watchdog timer which will be discussed later.But keep in mind that this line needs to be included else the cpu will reset every 32ms.

A program to toggle LED when a switch is pressed.The delay should be properly selected to compensate for the switch debouncing effect.

#include "io430.h"
  int main( void )
  {
    int i;
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
  P1DIR=BIT0+BIT6;         // make pin 0 and 6 o
utput
    P1OUT=BIT0+BIT6;        // make pin 0 and 6 high
    while(1)
    {
               if((P1IN & BIT3) == 0)
              {
                     P1OUT^=(BIT0+BIT6);    // toggle the led's
               }  
              for(i=0;i<27000;i++);    // delay between the toggle.
    }
}
 
2 Comments

Posted by on August 28, 2011 in Flip, MSP430 Tutorials

 

Tags: , , ,

MSP430 getting started

With the brief intro on msp let us start programming the controller. We ‘ll need IDE to write and compile the code and a programmer to transfer the code, and the controller.I’ll be using MSP430G2231 (Launch Pad) which has a Flash emulation tool (FET) on the board itself  and IAR Embedded workbench kick-start edition.Download and install the software.Now that we’ve the tools at hand lets begin.Open IAR and create a new project with the toolchain as MSP430 and template as C>main.Here is a screen shot of the software.

Press ok and name the project and press finish to get a c file which looks like the one shown below

I’ve named the project as blink.The file main.c is the source file and we’ll be writing the code here.On the left is the workspace which shows the file present in the project.

To select the required  controller open the target options window by clicking on the menu option :

Project > Options

select the required controller in the general options and also select the Debugger option in the same window as

  • FET debugger if the code is to be transferred to the controller
  • Simulator to simulate the code on the IDE.

Be sure to click the project on the workspace before going to options as the options may open for the other files if they were last clicked rendering the debug options greyed out.

To program a controller connect the controller via usb and hit the  Project >> Download & debug

option on the menu.

Use the following options for the launch pad.

Debugger :FET Debugger

Device : MSP430F2012

As the G series has not yet been added I use F2012 as a device .The mapping is same for both the devices  .If G2231 is available then select it.

 
Leave a comment

Posted by on August 28, 2011 in Flip, MSP430 Tutorials

 

Tags:

MSP430 introduction

MSP430 is a family of 16 bit ultra-low power microcontroller by Texas Instruments launched during the late 90’s.TI has since launched many series like 1xxx,2xxx,etc. the recent ones being 5x’s and 6x’s.The key features of these controllers are :
>>   up to 6 low power modes while consuming as low as 0.8-µA .
>>   wake up time as good as 6 micro seconds.
>>   16-bit RISC CPU with 27 RISC instructions and 24 emulated CISC instructions.
>>   Orthogonal architecture with seven addressing modes(including atomic addressing).
>>   12-bit or 10-bit ADC — 200 ksps
>>   12-bit dual-DAC
>>   Supply voltage supervisor and Brown out reset circuit.
>>   Watchdog timer.
>>   built-in Lcd drivers in recent versions.

Every new series in the family is accompanied with a user guide provided  by TI.Other resources are data sheets for the particular controller.Online community at TI’s Website and textbooks by John Davis are big sources of information on MSP.

Software and Hardware Tools:
>>  TI : Code Composer Studio v4.0
>>  Third Party Software : IAR Embedded Workbench
>>  Launch PAD
>>  Evaluation boards
>>  eZ430-F2013 USB Development tool

The softwares are available in a code limited version for free and the development boards can be ordered from TI’s website . The launch pad is the cheapest with a price of around 200rs.It has a single port which is multiplexed as I/O’s,ADC’s,UART,Comparator,Timer and a built-in temperature sensor.It is a great tool to learn msp.Recently a Capacitive booster pack has been released with libraries for the launch pad .Its a capacitance based touch pad . priced at around 500rs.Here is a picture of the launch pad.

 
6 Comments

Posted by on August 28, 2011 in Flip, MSP430 Tutorials

 

Tags: