Interrupt concept and Low Power Modes (LPM)


I had  written about hardware interrupts earlier. Now I’ll write about interrupts in general. How to write program to enable interrupts and handle those. Basically there are two types of interrupts.

  • Hardware Interrupts
  • Software Interrupts

As the name suggests hardware interrupts are given on the pins of the controller. An example of this can be found in by blog post.

Software interrupts on the other hand are made by using software. And these are vectored interrupts i.e. the program counter is loaded with the contents of vector address of the interrupt that occurred. In fact all interrupts are vectored. All peripherals can generate an interrupt specific to their operation.

I’ll be taking the example of TimerA interrupt . But lets see a little theory about how to handle interrupts.

First I’ll tell you what is maskable and non-maskable interrupt.  Maskable interrupts can be disabled using software and non-maskable interrupt cannot be disabled. RST is a non-maskable interrupt. Now masking is done to avoid accidental triggering of the interrupt which can distort the program flow.

All the maskable interrupts have to be enabled. This is done by setting the GIE(General Interrupt Enable) bit of status register. So if you include the msp430.h file you can use the GIE macro defined in the header file and OR(bitwise) it with status register(SR), to enable the interrupts by setting that bit.

Now finally let’s see what happens when an interrupt occurs.


So when an interrupt occurs the PC and SR contents are pushed onto the stack after the current instruction is executed. This is known as context saving. Depending on priority the interrupt with highest priority is processed first. Thus the PC is loaded with the contents of the vector address and context switching takes place.


The image above shows how to return from an Interrupt Sub Routine (ISR). An ISR is nothing but the code you write for handling the interrupt. The address of this ISR is stored at the vector address of the interrupt you want to handle.

One has to use “RETI” instruction to return to the main program. What RETI does is it pops the PC and SR contents back from the stack. Thus original state of program is restored.

Interrupt Vector Addresses

Now these are device specific. So one has to refer datasheet of the required microcontroller for this. Since I’ll be using msp430g2553 for this I’ll post the vector address table for the device.


That is that.

Low Power Modes(LPM) of Operating modes of msp430

Now-a-days its all about reducing the power consumption. And Texas Instruments msp430 is a low power microcontroller. That means that there must be low power modes. Those are nothing but LPM. These are selected by setting or resetting the bits of SR. These modes are well explained in the user guide. I’ll post the page concerning these modes.


All the SCG1,SCG0,OSCOFF,CPUOFF are bits of the status register(SR). You can use the bits defined in the header file for setting these bits. I’ll explain more about this after the program.

Now when you enter a LPM. You can come out of it when there is an interrupt. Now we know that PC and SR are pushed onto the stack. So if you want to come out of the LPM on exit from ISR you need to change the SR contents pushed onto the stack. The process is given in the user guide.


The bic instruction is used to clear the corresponding bits of SR that are used for controlling LPM.

In LPM basically we shut the oscillators which consume the maximum power. Thus depending on your need you can shut down the ACLK,SMCLK,MCLK and DCO.


;Name             : Manpreet Singh Minhas
;Date                 : 22 Nov 2013
;Website        :
;Software        : IAR Embedded Workbench

Aim                  : To blink an LED by using TimerA interrupt and use msp430 in LPM.
#include “msp430g2553.h”     ; Include the header file
ORG 0FFFEH                                                                    ; Power On Reset Vector address
        DW MAIN                                                                    ; Here MAIN is nothing but a macro for 0C000H
                                                                                             ; memory location. You might as well write
                                                                                             ; 0C000h. We try to make the code as reader
                                                                                             ; friendly as possible, that is why MAIN macro.

ORG 0C000H                                                                 ; Starting address of FLASH MEMORY in
                                                                                          ; msp430g2553 where the program will be burnt.
MAIN:        MOV.W #WDTPW|WDTHOLD,&WDTCTL  ; Stop watchdog timer
                  MOV.W #03FFH,SP                                         ; Initialize stack pointer to top of
                  MOV.B #BIT2,&P1DIR                                             ; RAM. You’ll have to use device’s
                  MOV.W #08000H,&TA0CCR0                               ; datasheet for finding this address.
                  BIC.B #BIT2,&P1OUT                                            
                  MOV.W #TASSEL_1|ID_0|MC_1|TAIE,&TA0CTL ; Initialize timer
UP:           MOV.W #LPM3|GIE,SR                                             ; Enable interrupts and enter LPM3
                 JMP UP
TMR_ISR:    XOR.B #BIT2,&P1OUT                                         ; Toggle LED
                      BIC.W #TAIFG,&TA0CTL                                      ; Clear the interrupt flag bit
        DW TMR_ISR

The above code will make led connected P1.2 to blink after 1 sec delay. You can change the BIT2 to BIT0 or BIT6 if you don’t want to use an external LED. I’ve used TimerA in up mode. And I’m using ACLK for this part. I’ve connected a 32kHz crystal there.  So you need to connect it there by soldering. Then it’s given in the user manual that when the count goes from TA0CCR0 value to 0, the TAIFG flag is set. I’ve enabled the interrupts in SR by setting the GIE bit, also you need to enable it in the peripheral’s control register. I’ve enabled the TimerA interrupt by setting TAIE bit of TA0CTL. Now it’s vital to clear the interrupt flag of the interrupt that you are handling. In this case the TAIFG flag in TA0CTL register. So that we can acknowledge the next interrupt. Its easy to forget this so just beware. And regarding the LPM3 macro that I’ve used it just sets the corresponding bits of SR. The definition is:

#define LPM3                (SCG1+SCG0+CPUOFF)

So either you can write LPM3 or the corresponding SR bits. And instead of using mov instruction you can use bis instruction as well.


p>If any doubts feel free to ask. Thank you for reading this. Hope this was useful and informative.

Assembly Language Programming Inception


Well I really wanted to write about assembly language programming, so here I am writing about it. I first came across the term assembly language when I was learning about Intel’s 8085 in my junior college. There we were introduced to this concept. When we talk with other humans we use sentences to communicate. Now these sentences are made up of words and the words from alphabets. One more thing is language, the letters are specific to the language being used. If I say ”ਤੁਹਾਡਾ ਨਾਮ ਕੀ ਹੈ” you would be able to understand this only if you know Punjabi language. So the interpretation is done by the brain in normal conversations. We come across translators who translate one language to another. Now suppose that I know only Punjabi and you know only English, then we would need a translator. This enables us to use the language we know without having to learn a new language. Now correlating this example to processors, processor only understands binary language i.e. 1’s and 0’s. We understand English(well since this is an international language I’m stating this) so we can’t write 11011110000 directly. We need some English like language for talking to the controller. This is where assembly language comes into picture. The instructions are given certain English like terms so that we humans can easily write programs and understand the written programs. Now each instruction in assembly language has an opcode which is nothing but a hex value. This hex or hexadecimal value is what the processor understands. When we were learning 8085, we used to do manual assembly. We wrote the code in assembly language, then using the opcode sheet wrote the corresponding opcodes down. Now these lines in hexadecimal numbers would be our code. This code was written into the FLASH/ROM memory. Since we manually used to write the hex values into the program memory this is called manual assembly. We talk of modern microcontrollers now. Today we get IDE’s and compilers and assemblers for all the modern microcontrollers be it msp430 or Arduino or Atmel. We write the program in English like language aka assembly language. The compiler will convert this assembly program into hexadecimal values and the assembler will burn this code in the flash memory. So the compiler generates the hex file (Now the name hex file makes sense,doesn’t it??!). This hex file is burnt into the flash memory i.e assembled into the memory  by the assembler. Now in this post I won’t be going into the details of how the program is burnt. We shall focus on how to write the code for now.

Starting with msp430 assembly language

Now that we know what assembly language is, let’s study msp430 assembly language. First thing is to learn the instructions. For that please refer the user guider  msp430x2xx for this. The section 3.4 is instruction set. The instructions are so beautifully explained there that I feel no need to explain each and everything again here. In order for anyone to write in assembly language one should know the instruction set thoroughly. Now I assume you know the instruction set from this point on!!!(If you do not understand any instruction you can comment on my blog. I’ll do the best I can to help you.) . Along with instruction set please read the addressing mode section as well. It is required as well.

CPU Registers

One of the most important resources of CPU is its registers. Most of the instructions involve them as one of the operands. Following picture shows all the registers.


As we can see there are 16 registers in total. With 4 of them being special purpose registers and rest 12 general purpose registers. I’ll tell you about the PC,SP and SR for now.


PC stands for program counter. This register contains the memory location of current instruction under execution. When the chip is reset,  program counter (PC) is loaded with address contained at reset vector location (0FFFEh). So when you reset the chip, [reset vector]–> PC. The reset vector address for mspx2xx is 0FFFEh. This feature is important while writing assembly language programs. I’ll talk about this when we write our first assembly language program.


SP stands for stack pointer. Now this stack pointer points to top of stack. Stack is like a scratch pad where we store temporary data. We can push data onto stack. We use push and pop instructions while dealing with stack. Stack is nothing but RAM.


SR stands for status register. This contains information about the result obtained after arithmetic or logical operations. Then SR bits are used to configure the low power modes in msp430. Also you can enable interrupts by setting GIE bit of SR. All in all SR is an important special register and has multiple applications.


I’ll be using IAR Embedded Workbench for assembly language programs. The only reason for this is that the directives used in IAR are similar to KEIL which I learnt as a part of my curriculum. Directives are instructions used by the compiler to understand what to do with the code. You can use Code Composer Studio if you want. Note that for IAR the assembly language file is saved with .s43 extension unlike the usual .asm .

First Assembly Language program

;Name             : Manpreet Singh Minhas
;Date               : 23 Nov 2013
;Website         :
;Software        : IAR Embedded Workbench
\#include “msp430g2553.h”                                                            ;Include the header file
ORG 0FFFEh                                                                                     ;Reset vector address
ORG 0C000h                                                                                     ;Flash memory starting address
MAIN:                    mov.w #WDTPW|WDTHOLD,&WDTCTL       ;Stop the watchdog timer
mov.b #BIT0|BIT6,&P1DIR                                ;Make P1.0 and P1.6 as output
mov.b #BIT0,&P1OUT                                        ;Make LED1 ON
REPEAT:             mov.w #0FFFFh,R7                                             ;Put value in counter for delay
UP:                       dec.w R7                                                               ;Decrement counter till 0
jnz UP                                                                    ;Repeat this 0FFFFh times
xor.b #BIT0|BIT6,&P1OUT                                 ;Toggle the LED1 and LED2
jmp REPEAT                                                        ;Infinite loop

Let’s understand what we have done in this program. I’ve written the hello world program again. This is LED blinking program in assembly language. I shall explain the basic directives now.


This directive tells the compiler that where it has to write the opcodes in memory. So ORG 0FFFFh tells the compiler that the next statement’s hex equivalent has to be stored in memory location 0FFFFh.


This directive tells the compiler that we have defined a word and it is stored in memory which is given by ORG directive. So the code ORG 0FFFEh then DW 0C000h will store the word 0C000h in 0FFFEh memory location.


This directive tells the compiler that the code is over, whatever is written beyond this point is not relevant to the program.

Concept of LABELS

MAIN,REPEAT and UP are labels in the above program. Now these labels are nothing but the memory location of the statement where they are written. So MAIN is nothing but 0C000h, thus we could have very well written DW 0C000h instead of MAIN.

Numbers in assembly language

While writing numbers in assembly language you need to write 0 before a hexadecimal number starting with an alphabet. This is to make the compiler understand that the code is a number and not a label. I’ve developed a practice to write 0 before every number. But you can follow whatever you like. Thus FFFFh is not valid but 0FFFFh is valid. (and the 0 is zero and not the alphabet O)

Now we understand the code. The first two lines writes the starting address of the main program code at the reset vector address i.e. 0FFFEh. As I had discussed earlier that when the controller is reset the data at the reset vector memory location is loaded into the program counter, thus we write our main program starting address there. Then we write the main program in flash memory. Now for msp40g2553 flash starts at 0C000h, so I’ve written the main code there. You’ll have to change these addresses as per the data sheet of the controller you are using.

Then I’ve used simple mov instructions to initialize the watchdog timer and the ports. I’m giving this program for starting your assembly language coding. So you may want to use the timer section to give delay. I’ll write a code for that as well. Also if you have any doubts at all feel free to ask me. I’ll definitely share what I know.

Hope this post was informative. Thank you for reading this.


Here are two more assembly language programs just for practice.

;Name             : Manpreet Singh Minhas
;Date                : 23 Nov 2013
;Website         :
;Software        : IAR Embedded Workbench
;This is a program to generate the well known Fibonacci series.
#include “msp430g2553.h”
ORG 0FFFEH                       ;RESET VECTOR
DW MAIN                        ; GOTO MAIN LABEL
ORG 0C000H
MOV.W #00H,R4            ; FIRST ELEMENT
MOV.W #01H,R5            ; SECOND ELEMENT
MOV.W #00H,R6            ; AUXILLARY POINTER
MOV.W #010D,R7            ; NUMBER OF ELEMENTS
MOV.W R4,0x200(R6)
MOV.W R5,0x200(R6)
UP:        INCD.W R6
MOV.W R5,0x200(R6)
MOV.W 0x200(R6),R4

;Name             : Manpreet Singh Minhas
;Date                 : 23 Nov 2013
;Website        :
;Software        : IAR Embedded Workbench
;This program transfers data from one memory location to other.
#include “msp430g2553.h”
ORG 0FFFEH  ; This is the reset vector address.
ORG 0C000H ; Starting of flash memory.
MAIN:   mov.w #WDTPW|WDTHOLD,WDTCTL ; stop the watchdog timer
mov.w #03ffh,SP            ; initialize the stack pointer to top of ram
mov.w #0200h,r4            ; initialize the destination reference pointer
mov.w #0e000h,r5           ; initialize the source pointer
mov.b #010d,r6
up:     mov.w @r5+,0(r4)
incd.w r4
dec.w r6
jnz up
jmp $
ORG 0E000H

For both these you can use the IAR emulator. I’ll cover how to use that later.

Blinking LED(hello world! of micro controllers)

This post is about the blinking led’s. Those of you who know c/c++/java or any programming language for that matter would have definitely written the first program as hello world. In case of micro controllers blinking led’s is the hello world sort of thing.


On launchpad there are two surface mount led’s (smd led’s), they differ from normal led’s in the sense that there are tiny as compared to the normal ones. We generally use current limiting resistors while using led’s of any sort, but Texas has taken care of that for us already by connecting these on board led’s with current limiting smd resistors. We make use of port 1 pins 0 and 6 to which these led’s are connected. When the port is made high the led glows and when its made low the led turns OFF. Thus we first configure the P1.0 and P1.6 pins as output pins using the direction register. Then we toggle the output register after some delay so that the change is registered by human eyes(persistence of vision remember??).


//Author: Manpreet Singh Minhas
//Date : 2/10/2013
#include <msp430g2211.h>// include the header file for msp430g2211
void main(void)
WDTCTL = WDTPW + WDTHOLD;                                                                          //stop watchdog timer
P1DIR|=BIT0+BIT6;                                                                                                    //set the port 1 pin 0 and pin 6 as output you can do this by using hex                                                                                                                                              *command word as well i.e 01000001 or 0x041 or 41h
P1OUT|=BIT0; // make port1 pin0 i.e P1.0 high
int i=0;
for(;;){                                                                                                                               // infinite loop so that the action is repeated indefinitely

P1OUT ^= (BIT0+BIT6);                                                                                                 // toggle using ex-or with 1 ( refer truth table of ex-or operation.)

// some delay for the human eye to observe the change



*: Those of you who don’t know what is a command word. Command word is 8bit or 16 bit data that is given to the special function registers to configure the device how we want. In this case it is the P1DIR and P1OUT registers. Thus these two are command word registers and the data we send is command word. In msp430g2xx1 making P1DIR.x high/1 makes the port pin as output and low/0 makes it input. P1OUT makes the port high if the bit is set and makes it low if the bit is reset.