RSS

Category Archives: MSP430 Tutorials

Working with Anaren AIR Booster Pack

hey guys ,

I bought the Anaren booster pack a month ago. It was my first time working with a rf module and msp.When i looked at the firmware of the booster supplied by anaren it drove me crazy. I’m sure that you might have felt the same. They  have used extensive generalization to include all possibilities and not to mention the cross referencing to be done to make it work. Me and my  friends tried to understand the code . But it was too confusing and hence we stopped. So I wrote the firmware myself. In this tutorial let us see how to do the same . In the code to follow we will use two launchpads and 2 AIR booster packs. I’ve changed the pin connections a little to communicate through UART . So basically we will be sending text from one computer to another by means of the RF booster pack. We will be using MSP430G2553 controller. You have to know basic UART and SPI protocols and their setup in msp to make this work.

CC110l

So let us see how to work with CC110. This rf ic works on 4 different frequencies based on the programming and the external hardware. We will be using 867 Mhz.It is controlled by msp by means of SPI interface which includes SOMI , SIMO , SCLK , CSN pins of CC110l.The GDO pins are used to update the status of the CC110l and can be programmed to interrupt the controller on start of various events. The device is configured by writing to its configuration registers and the status is known by reading form status registers. The mode of CC110 is set by using Strobe Commands. Like any transceiver it can works as a RX or TX at a time.

So with this background let us get on the board.

The connections are as shown below.

The Connections to the Computer are to established using a serial port of a computer. In case the computer doesn’t have a serial port then use a serial-USB Converter. Connect the UART_RX to the TX pin of the serial port ,UART_TX to the RX pin of the serial port and the GND  pin of bin together.The connections of the Cc110l are as shown . The connections of the Booster pack are :

I’ve used the launchpad debugger to burn the code on the ic.In the example shown the booster and the controller are mounted on a separate board and not on the launchpad.This was done to use the UART port.For those who are not interested in using the uart port then directly connect the booster to the launchpad.But some changes to the board definitions have to be made.To run the code presented here please use the connections mentioned.

Here are the project files of this example.

  1. link1

The project consists of several files which are used to make the coding simpler.Two boards are required for this example.Each Board accepts data from the hyper terminal and transmits it.The other board receives this and displays it on the hyper terminal.The same code has to be burnt on both the msp43G2553 controllers.The required software for this is a serial port access software like hyper terminal (default in win xp) .In case of Win 7 users you’ll have to download the software online.Tera term , hyper terminal and FLIPTERM are some of the software’s available on net for Win 7 .I’m using Win 7 with hyper terminal.The UART is configured for a

  • baud -9600
  • parity – none
  • flow control = none
  • stop bits – 1
  • databits – 8

These settings are required for the terminal to operate.

The basic flow of the code is as follows :

POR => Initialize board =>Put the device in listen mode =>if data received from uart send through rf module => if data received from rf display on terminal.

The project contains different files for different initializations.The interrupt vectors are present in “pragmas.c” connect each board and program the code into them.Connect each one to a different serial port and open the corresponding hyper terminal.Once the hyper terminal is open reset the controller.This will display “*” on the terminal.Any character typed hence will displayed as a “-” on the screen.A character received will be displayed as  it is. So in the above example i ve typed “ADadw”.This was transmitted which was displayed as “-” , and received which displayed the characters themselves.

The CC110_functions.c file contains the functions to initialize the CC110l.The register values are available with the manuals provided by  Anaren.The CC110l is set to -0.5dBm power.This gives a range of about 7-10 m.To vary power edit the patable[] value to the required value.To vary the config register vary the values in this files next to the required registers.The CC110l provides the register addresses.So the device developed by me looks somewhat like this.Hope you have a fun time working with it.For any queries post a comment or mail it to me at sms219610@gmail.com.

I’ve added a schematic of the circuit used by me. NOTE THAT I HAVE USED MSP430G2553 IN 28 PIN PACKAGE.so ignore the PORT3.this circuit was used for a project of mine.If you have any queries please leave a comment.Hope this article was useful.

PLEASE NOTE:

Andy has tried the code On CCS .He is able to use the booster directly on the launchpad without much modifications. Though I ve not tried this myself he has made it work. His comment is as follows:

”  The hardware I’m using is TI launchpads with the AIR module plugged in. I was able to compile using CCS version 4 and use the virtual com ports on the launchpad. I had to make the following changes to reroute GDO0 to P1.3 and CSN to P1.4 on each AIR module. The Booster Pack User Manual describes these changes and I used 0603 resistors to make the connections. I found this manual on the Anaren site. .

1. Cut the trace between JP2 pin 1A and pin 1B. Solder a 0 ohm 0603 resistor between JP2 pin 2A and pin 2B. This routes GDO0 to P1.3 on the launchpad.

2. Cut the trace between JP3 pin 1A and pin 1B. Solder a 0 ohm 0603 resistor between 3A and 3B. This routes CSN to P1.4.

Change defines in mspboard.h to map the new connections.

#define GDO0_PIN BIT3
#define GDO2_PIN BIT0
#define CSn_PIN BIT4

Compile, flash, and execute FW each launchpad. No other changes made. I use two sessions of putty each connected to a launchpad to send and receive data.  “

 
81 Comments

Posted by on April 1, 2012 in Flip, Flip flop, MSP430 Tutorials

 

Tags: , , , , , , , , , ,

Clocks and MSP430

hi there everyone,

in this tutorial let us check out the all clocks that msp has got to offer us.After studying 8051 and comparing that to controllers like msp and AVR makes me wonder how much development has taken place over the years.The 8051 controller had just one clock.It was external with maximum range of 16Mhz.If it failed then the controller would fail.So what makes msp better.It has 2 built in clocks and one external.Some have a provision for 2 external clocks and th series 5xx and 6xx have dedicated clock built in for Real time Clock facility.When i first read through i was wondered.What do you do with all these clocks?Do we really need it?

So I read through and yeah we do  need it whn we are thinking of very low power consumption.The MSP430 has usually 3 clock sources.

  1. LFTXT1      or   External low frequency oscillator (usually 32768 Hz but can have a max of 16Mhz.)
  2. DCO            or   Internal Digitally Controlled Oscillator stabilized with Frequency Locked Loop.
  3. VLO            or   Internal  Very Low Power Very Low Frequency Oscillator.

Some of the controllers have a XT2 oscillator which is a external high frequency oscillator.And as mentioned the 5xxx and 6xxx series have a REFOCLK which is stabalized 32768Hz clock internal to the chip to be used in place of the LFTXT1.It consumes lesser power than the LFTXT1.

These Clocks source 4 Clock signals.They are

  1. MCLK       or    Master Clock.
  2. SMCLK     or   Sub-Master Clock.
  3. ACLK        or   Auxiliary Clock.
  4. ACLKn      or  Auxiliary Clock divided by n.

The different registers used to modify the Clocks are shown below.These were taken from the user guide provided by ti.


The Master Clock signal is Used to source the CPU.So the speed of the CPU depends on the MCLK and this signal is switched off during LPM0.By default in the absence of External crystal the DCO sources the MCLK.

The DCOCTL,The range select bits in BCSCTL1 and BCSCTL2 are used to configure the MCLK.

Now let us see how to control the DCO frequency.The DCOx and RSELx are together used to vary the DCO frequency from about few hundreds of hertz to 16Mhz.The MODx bits are used to obtain the intermediate frequency.The SELMx and DIVMx are used to select the source for the MCLK signal and division factor.The following code encompasses the above.

#include "msp430x20x2.h"
int main (void)
{
  WDTCTL=WDTPW+WDTHOLD;               //hold watchdog
  P1DIR=BIT0+BIT6;                   
  P1IE=BIT3;                          //enable interrupts
  int i=0;
  _EINT();            
  while(1)
    {
    for(i=0;i<30000;i++);             //toggle LED1 at PIN 0
    P1OUT^=BIT0;
    }
}

#pragma vector=PORT1_VECTOR           //ISR for PIN 3
__interrupt void pin3 (void)
{
static int range=0,dco=0;             
dco++;                                //increment the dco step
dco=dco%8;
range=range%3;
switch(range)                         // step up range every 8 clicks of S2 switch
  {
  case 2:BCSCTL1=RSEL0+RSEL1+RSEL2+RSEL3;
         break;
  case 1:BCSCTL1=RSEL0+RSEL1+RSEL2;
         break;
  case 0:BCSCTL1&=~(RSEL0+RSEL1+RSEL2+RSEL3);
         break;
  }

switch(dco)                         //step up speed every click
  {
  case 0:DCOCTL&=~(DCO0+DCO1+DCO2);
         break;
  case 1:DCOCTL=DCO0;
         break;
  case 2:DCOCTL=DCO1;
         break;  
  case 3:DCOCTL=DCO0+DCO1;
         break;
  case 4:DCOCTL=DCO2;
         break;
  case 5:DCOCTL=DCO0+DCO2;
         break;
  case 6:DCOCTL=DCO1+DCO2;
         break;
  case 7:DCOCTL=DCO0+DCO1+DCO2;         //increment the range
         range++;
         P1OUT^=BIT6;
         break;
  }
P1IFG&=~BIT3;
}

The above graph explains the variations in the DCO frequency.The above code also checks for the RSEL values and DCO values as in graph. Every time the RSEL value increments the green led toggles and the red led is a indicator of the speed of CPU as the LED is toggled by a delay loop f(or loop) which uses the CPU Registers to count .As i dont have a CRO I’ve limited the code to just the 3 values of the RSEL .Please do check if there are any variations for other values.There are modulation bits which allow us to further vary the frequency.I’ll be putting up separate post on it sometime later.Now let us see how to vary the source for the MCLK from DCO to  other sources and to change the division factor.

#include "msp430x20x2.h"
void main()
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR=BIT0+BIT6;
P1OUT=0X00;
P1IE=BIT3;
_EINT();
int i=0;
while(1)
{
for(i=0;i<32000;i++);
P1OUT^=BIT0;
}
}
//------------------------------------------interrupt isr
#pragma vector=PORT1_VECTOR
__interrupt void pin3(void)
{
static int source=0,divide=0;
divide++;
divide=divide%4;
source=source%3;
switch(source)
{
case 0:BCSCTL2&=~(SELM0+SELM1);         //Select DCO
        break;
case 1:BCSCTL2=SELM1;                   //select XT2 else LFTXT1 or VLO
        break;
case 2:BCSCTL2=SELM0+SELM1;             //select LFTXT1 or VLO
        break;        

}
switch(divide)
{
case 0:BCSCTL2&=~(DIVM0+DIVM1);         //Divide the source / 1
        break;
case 1:BCSCTL2=DIVM0;                   //divide by 2
        break;
case 2:BCSCTL2=DIVM1;                   //divide by 4
        break;        
case 3:BCSCTL2=DIVM0+DIVM1;             //divide by 8
        source++;
        P1OUT^=BIT6;
        break;
}
P1IFG&=~BIT3;
}

So in the above code toggling of the green led is an indicator of the change of source and the red light toggle indicates the frequency due to change in the division factor.The same applies to the SMCLK and ACLK too.The SELSx and DIVx bits are used to change the source and the division factor for the SMCLK.The DIVAx bits are used for the ACLK.The default clock for ACLK in case the external oscillator is not present is VLO.To check the frequency of the ACLK ,SMCLK and MCLK the following code can be used.The ACLK and SMCLK are usually connected to the GPIO’s in case of the launchpad it being the PIN0 and PIN5.The MCLK can be checked by a delay loop.

#include "msp430x20x2.h"
void main()
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR=BIT0+BIT5+BIT6;
P1SEL=BIT0+BIT5;            //Select the secondary pin funcitons i.e ACLK SCLK.

while(1)
{
P1OUT^=BIT6;
}
}

The Frequency will have to be checked on a CRO as the Flicker on the led too fast to see.Also there are default values stored in the memory of the chip which can be used to configure the controller to predefined frequencies.These are given in the library file as CALDCO_16MHZ,CALBC1_16MHZ,.. etc.These can be directly entered on the code to get the desired calibration.Please leave a comment if there is something I can help with or if there is an error.I hope this blog was useful.Thank you for reading.Have a nice day 🙂

 
Leave a comment

Posted by on October 5, 2011 in Flip, MSP430 Tutorials

 

Tags: ,

msp’s watchdog

hey guys

have been  fooling around with the msp4305438 board so couldn’t post for a long time… so I’ll be posting the tutorial on watchdog timer today.This is something which bugged me a lot when i first started with msp, which i feel is really stupid considering the fact that it was really simple.So what is a watchdog timer.We can imagine it as a small hardware which will kick the controller if at all the controller hangs up.It basically resets the controller when it is activated.

Principle

The watchdog timer has a 16 bit timer which counts from 0x00 to 0xffff.When it hits the max value of oxffff the watchdog resets the controller.Our job is to prevent this from happening and we’ve to keep resetting the timer.If the code hangs then the timer is not reset and the watchdog timer overflows and resets the timer.

Timer Mode

The watchdog can also be configured to be a timer which provides interrupts instead of the reset of the controller.But the time delay can be varied to a great extent.It can be varied by varying the clock sources itself or varying the the division factors.

Registers

The watchdog can be configured by using the WDTCTL register.The Upper 8 bits of the WDTCTL has to be written with 0x5A .This is the password protect feature of the watchdog. A failure to write this or writing anything to this nibble will cause a access violation and reset the controller.The lower 8 bits are the control bits and are given as follows.

The Bit definitions are as follows:

WDTHOLD => disables the watchdog timer when the bit is 1.

WDTNMIES =>selects the triggering edge for the non maskable interrupt on RST/NMI pin. 0 indicates a rising edge and a 1 represents a falling edge.

WDTNMI  =>enables the nmi function on the RST/NMI pin.

WDTTMSEL =>selects the timer mode for the watchdog when the bit is 1.

WDTCNTCL => watchdog counter clear resets WDT counter.When the bit is set the counter is reset and the bit is reset immediately.

WDTSSEL => selects the source for the watchdog timer. 0 selects the SMCLK and 1 selects the ACLK.

WDTISx => The division factor for the clock source.

  1. 00 => clk source / 32768
  2. 01 => clk source /8192
  3. 10 => clk source /512
  4. 11 => clk source / 64

There are two other registers which are used with the watchdog.IE1 and IFG1.These have the interrupt enable bits and the interrupt flags for the corresponding sources.

Program Watchdog Mode

Now let us first learn to use the watchdog timer in watchdog mode (WDTTMSEL = 0).

Usually we begin the programs by entering the following code.

WDTCTL=WDTPW+WDTHOLD;

Here we are  first writing the password by writing WDTPW which is a macro of value 0x5A , then we are setting the WDTHOLD bit to stop the watchdog.This is usually done at the time of debugging as the watchdog feature is not required.Now let us assume that we’ve finished the debugging of a certain code and that we are adding the watchdog timer .How do we do it? The following code explains it.Please read through.

#include "io430.h"
int main( void )
 {
 // Stop watchdog timer to prevent time out reset
 WDTCTL = WDTPW + WDTHOLD;
 P1DIR=BIT0+BIT6;
 P1OUT=BIT0;
 int i=0;
 while(1)
 {
 for(i=0;i<32000;i++);
 P1OUT^=(BIT0+BIT6);
 }
 return 0;
 }

The above code stops the watchdog timer and blinks the leds.Now lets us have the watchdog on and see the required changes

#include "io430.h"
 int main( void )
 {
 // Stop watchdog timer to prevent time out reset
 //WDTCTL = WDTPW + WDTHOLD; // doNOT DISABLE the watchdog
 P1DIR=BIT0+BIT6;
 P1OUT=BIT0;
 int i=0;
 while(1)
 {
 for(i=0;i<32000;i++)
 {
 WDTCTL = WDTPW + WDTCNTCL; //keep clearing the counter to prevent the reset
 }
 P1OUT^=(BIT0+BIT6);
 }
 return 0;
 }

In the above code we have to keep clearing the counter at approximately 32ms i.e, the time required by the counter
to count to oxffff with SMCLK as the source.So we have to keep clearing the counter based on the program written.
Since the code I’ve written has only a for loop i’ve added the clearing line in it.

Timer Mode

Now let us use the watchdog as a timer to blink an led.For this we’ve to set the WDTTMSEL bit which enables the timer mode in the WDTCTL register.Then we’ve to enable the interrupt in the IE1 register and right the required routine.The following code is an example.

#include "msp430x20x2.h"
int main( void )
{
  WDTCTL = WDTPW + WDTTMSEL;           //select the timer mode
  IE1=WDTIFG;                          //enable the interrupt
  P1DIR=BIT0+BIT6;
  P1OUT=BIT0;
  _EINT();
  while(1);
}
#pragma vector=WDT_VECTOR
__interrupt void wdttimer(void)
{
P1OUT^=BIT0+BIT6;
IFG1&=~WDTIFG;                          //clear the timer interrupt flag
}

Now lets vary the clock sources and the division factor for the watchdog.I’ll write a program to use the SMCLK and use various division factors and a program to change the clock sources.The ACLK can be used by setting the WDTSSEL bit.

// WDT with SMCLK as Source and varying the division factors.
#include "msp430x20x2.h"
#define WDTCONFIG (WDTPW+WDTCNTCL+WDTTMSEL) //configure wdt in timer mode and clear the count register

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTTMSEL + WDTCNTCL;
  IE1=WDTIFG;
  P1DIR=BIT0+BIT6;
  P1OUT=BIT0;
  P1IE=BIT3;

  BCSCTL2=DIVS_3;       // slow down the smclk by division to make the blinking slow.details in clock
                        // module tutorial

  _EINT();
  while(1);
}
#pragma vector=PORT1_VECTOR
__interrupt void pin3(void)
{
static int i=0;
i++;
i=i%4;
switch(i)
{
case 0: WDTCTL=WDTCONFIG;   //source divided by 32768
        break;
case 1: WDTCTL=WDTCONFIG+WDTIS0;   //source divided by 8192
        break;
case 2: WDTCTL=WDTCONFIG+WDTIS1;   //source divided by 512
        break;
case 3: WDTCTL=WDTCONFIG+WDTIS0+WDTIS1;   //source divided by 64
        break;       

}
P1IFG&=~BIT3;
}
#pragma vector=WDT_VECTOR
__interrupt void wdttimer(void)
{
P1OUT^=BIT0+BIT6;
IFG1&=~WDTIFG;
}

In the above program the Switch s2 is used to change the speed of the blinking by varying the division factor for the Source of the watchdog which in this case is SMCLK. I’ve slowed down the frequency of the SMCLK by dividing the source signal to it which I’ll discuss in my next tutorial on clocks.Basically the blinking will be slow and noticeable only for the first two clicks of the switch.Later the blinking is too fast to notice.It can be seen using a CRO.

In the program i use the WDTIS0 AND WDTIS1 bits to set the division factor which is as follows.

WDTIS0            WDTIS1

WDTIS_0 =>        0                          0             SOURCE DIVIDED /32768

WDTIS_1 =>         1                           0            SOURCE DIVIDED / 8192

WDTIS_2 =>         0                          1             SOURCE DIVIDED /512

WDTIS_3=>         1                           1            SOURCE DIVIDED /64

So thats about it for now.Also check out the code examples for all the modules in the TI website.The WDT+ feature are explained.Thank you for reading.Have a nice day.

 
2 Comments

Posted by on October 3, 2011 in Flip, MSP430 Tutorials

 

Tags: , ,

Interrupting msp430!!!

The key feature of msp is the lower power mode which requires us to know how to put it to sleep and wake it. This is one main reason why we should and must know interrupts in msp.The interrupts can be classified into three types as:

  • System Reset
  • Maskable NMI
  • Non maskable NMI

The system Reset is of types

  • Power up clear (PUC)
  • Power on Reset (POR)

POR is associated with powering up of the device and a reset signal on RST/NMI pin. PUC is triggered by a POR and also watchdog timer and security violation (unauthorised memory access by programs).

Maskable interrupts are caused by peripherals and are enabled or disabled by the GIE(global interrupt enable) bit in Status register.The different maskable interrupts are

  • External interrupts
  • Timer Interrupts
  • Serial
  • ADC …etc.

The Non maskable interrupt refers to an interrupt which when enabled is not disabled by the GIE bit (global interrupt enable) in Status register.There are basically three different NMI’s.

  1. an edge on RST/NMI pin
  2. oscillator fault
  3. memory access violation

Each interrupt is associated with a corresponding Interrupt service routine which is located in a different location based on the interrupt.In C programming we define the ISR by using the directive #pragma and using keyword vector.The executed interrupt depends on the value of the vector.For example if we need to use the interrupts for the PORT1 pins then the vector should be declared as

#pragma vector=PORT1_VECTOR
__interrupt void isr(void)
{}

The different vector s can be found in the header file.They are

************************************************************
 * Interrupt Vectors (offset from 0xFFE0)
 ************************************************************/
 #define PORT1_VECTOR                (2 * 2u)  /* 0xFFE4 Port 1 */
 #define PORT2_VECTOR               (3 * 2u)  /* 0xFFE6 Port 2 */
 #define USI_VECTOR                     (4 * 2u)  /* 0xFFE8 USI */
 #define ADC10_VECTOR               (5 * 2u)  /* 0xFFEA ADC10 */
 #define TIMERA1_VECTOR          (8 * 2u)  /* 0xFFF0 Timer A CC1, TA */
 #define TIMERA0_VECTOR         (9 * 2u)  /* 0xFFF2 Timer A CC0 */
 #define WDT_VECTOR                  (10 * 2u) /* 0xFFF4 Watchdog Timer */
 #define NMI_VECTOR                   (14 * 2u) /* 0xFFFC Non-maskable */
 #define RESET_VECTOR              (15 * 2u) /* 0xFFFE Reset [Highest Priority] */
***************************************************************

By replacing the value of the vector the function will serve as the ISR for that particular interrupt.If PORT1_VECTOR is replaced with NMI_VECTOR then the ISR will run on the ocurence of NMI and not PIN interrupt.

All the maskable interrupts have to be enabled first before they can be used.To use the interrupts first we have to set the global interrupt enable bit in SR.This can be done by using the intrinsic function

__EINT();     if this function is not used then even if we enable individual interrupts they wont function.

External Interrupts On GPIO

Each port is associated with PxIE , PxIES , PxIFG registers which are used to control the interrupts.The interrupt on a particular pin is enabled by setting the particular bit in PxIE.ex:

P1IE=BIT3;             //enebles interrupt on pin 3

The PxIES is used to select if we need rising edge or the falling edge of the signal to trigger the interrupt.setting a bit makes the interrupt on that pin to be triggered on a rising edge and resetting makes it a falling edge triggered.

0= rising edge

1= falling edge

When an interrupt is triggered on a pin, the bit corresponding to that pin is set.These bits have to be manually reset once the ISR has been served.

Given below is  a code which uses a port pin as an interrupt to toggle the led.

#include"msp430x20x2.h"
main()
{
 WDTCTL=WDTPW+WDTHOLD;        //STOP WATCHDOG TIMER

P1DIR=BIT0+BIT6;              // MAKE PIN 0 AND PIN 3 AS OUTPUTS.
P1OUT=BIT0;                   //MAKE PIN 0 HIGH
P1IE=BIT3;                    //ENABLE THE INTERRUPT FOR THE PIN 3
P1IES&=~BIT3;                 // TRIGGER INTERRUPT FOR RISING EDGE
_EINT();                      //ENABLE GLOBAL INTERRUPT ; SET GIE BIT IN SR

while(1)
{};
}

#pragma vector=PORT1_VECTOR     //ISR FOR PORT1
__interrupt void P1(void)
{
P1OUT^= (BIT0+BIT6);            //TOGGLE
P1IFG&=~BIT3;                   //CLEAR THE INTERRUPT FLAG FOR PIN3

}

Timer Interrupt

I’ll explain the controlling of a timer later but at the moment we should know that by giving the vector value as TIMERA1_VECTOR the ISR will function on an interrupt when timer overflows.


NMI Interrupt on RST/NMI

To use RST/NMI as an interrupt pin we have to set two bits.One in WDTCTL register and one in IE1 register.The oscillator fault is triggered when there is an error in the oscillator.It is used to switch toDCO when the crystal oscillator is not present.

By modifying the WDTNMIES bit in WDTCTL we can change the triggering edge of the NMI.

The following code initializes the pin and toggles the led on interrupt.

#include"msp430x20x2.h"
main()
{
  IE1=NMIIE;                    //enable nmi
  WDTCTL=WDTPW+WDTHOLD+WDTNMI;  //select nmi function on RST/NMI
  P1DIR=BIT0+BIT6;
  P1OUT=BIT0;
  while(1)
  {};
}

#pragma vector=NMI_VECTOR
__interrupt void nmi(void)
{
 P1OUT^= (BIT0+BIT6);

 IFG1&=~NMIIFG;                      //clear nmi interrupt flag
 IE1=NMIIE;                          // enable nmi
 WDTCTL = WDTPW + WDTHOLD+WDTNMI;    // select nmi function on RST/NMI

}

Its weird that in the above code interchanging the last 2 lines in ISR will not set the RST/NMI pin nmi mode.
 
1 Comment

Posted by on September 12, 2011 in Flip, MSP430 Tutorials

 

Tags: , ,

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: