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.

Posted in Flip, MSP430 Tutorials | Tagged , , | 3 Comments

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.
Posted in Flip, MSP430 Tutorials | Tagged , , | 1 Comment

booster pack

Hey guys,

today I got my shipment of 430BOOSTER PACK for msp430 launch pad.Costed me around 800 to get it.Thanks to Kunal who helped me with the transaction.I immediately unpacked it.It really feels bad to tear the neatly packed cover the e14 guys mail it in.I didnt have a soldering iron at that time so i got to another friends house to get it done and finally connected the launch pad to the booster pack and tada!!! the booster pack came to life… I tried the media pad software and also a simulation of the pad itself.I think after telling all my friends about it i was satisfied and now m going through the code.it really amazing how much can be done in so less.I want to try out the other touchpads that are available here.If any of you have already done it then please share the link.So i ll get back to my new love.Hope i can do a lot more new stuff and experiments!!!

Posted in Flip | Tagged | 1 Comment

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.

Posted in Flip, MSP430 Tutorials | Tagged , | 1 Comment

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.
    }
}
Posted in Flip, MSP430 Tutorials | Tagged , , , | 2 Comments

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.

Posted in Flip, MSP430 Tutorials | Tagged | Leave a comment

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.

Posted in Flip, MSP430 Tutorials | Tagged | 7 Comments