Handling Interrupts and LPM in C language

Introduction

Well I’ve already written about handling interrupts in assembly language. Now I thought of writing about handling interrupts in C language. Since most of us are used to writing in C language, it is very important that one learns how to handle interrupts and write interrupt subroutines. Now first things first. Whatever you use be it C,C++ or assembly language, the controller understands only binary language. So when you burn the code its the hex file and nothing else that matters. Now the C language code is converted to assembly language which in turn is compiled to create the hex file. So one must know this fact to be able to use C language properly. In C language we can declare variables of various data types like char,int,etc. but when it comes to embedded C care must be taken. If your ALU size is 16 bit then the register size is 16 bit as well. So when you initialize a variable in C it is nothing but a register which is being initialized and used. Thus if you use double or any other type which is greater than the size of ALU you are bound to get error. Now to a C programmer it might seem absurd but if you know what is happening behind the scene its always easier to debug and understand what is happening. So if one knows assembly language then viewing the disassembly and debugging is easier.

Interrupts in C language

Handling interrupt is nothing but writing a code at a location whose starting address is written at the vector address of the interrupt being handled. In order to do this one must know the concept of #pragma vector. Now #pragma vector=Vector_Address is a directive just like ORG is in assembly language. What it tells the compiler is that the function that is being written next is a ISR and its starting address has to be stored at the Vector_Address. That being said, the function for ISR has a particular syntax which has to be followed.

Syntax for ISR function

__interrupt void function_name(void){}

(Note there are two underscores before the interrupt keyword). Now the function name can be anything except a keyword(which is quite obvious.). In the body of this function is what you write for handling the interrupt.

Syntax for writing a complete ISR

#pragma vector=Vector_Address

__interrupt void function_name(void)

{

// code for ISR

}

With that being covered what remains is the Vector_Address, one cannot type the vector address directly in hexadecimal value. In the msp430.h header file (rather see the device specific header file for e.g. msp430g2553.h ) there is a section for vector addresses where all vector addresses have been assigned a macro. I’ll paste the portion of header file concerning the topic at hand.

/************************************************************
* Interrupt Vectors (offset from 0xFFE0)
************************************************************/

#define VECTOR_NAME(name)       name##_ptr
#define EMIT_PRAGMA(x)          _Pragma(#x)
#define CREATE_VECTOR(name)     void (* const VECTOR_NAME(name))(void) = &name
#define PLACE_VECTOR(vector,section) EMIT_PRAGMA(DATA_SECTION(vector,section))
#define ISR_VECTOR(func,offset) CREATE_VECTOR(func); \
PLACE_VECTOR(VECTOR_NAME(func), offset)

#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define PORT1_VECTOR            “.int02”                     /* 0xFFE4 Port 1 */
#else
#define PORT1_VECTOR            (2 * 1u)                     /* 0xFFE4 Port 1 */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define PORT2_VECTOR            “.int03”                     /* 0xFFE6 Port 2 */
#else
#define PORT2_VECTOR            (3 * 1u)                     /* 0xFFE6 Port 2 */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define ADC10_VECTOR            “.int05”                     /* 0xFFEA ADC10 */
#else
#define ADC10_VECTOR            (5 * 1u)                     /* 0xFFEA ADC10 */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define USCIAB0TX_VECTOR        “.int06”                     /* 0xFFEC USCI A0/B0 Transmit */
#else
#define USCIAB0TX_VECTOR        (6 * 1u)                     /* 0xFFEC USCI A0/B0 Transmit */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define USCIAB0RX_VECTOR        “.int07”                     /* 0xFFEE USCI A0/B0 Receive */
#else
#define USCIAB0RX_VECTOR        (7 * 1u)                     /* 0xFFEE USCI A0/B0 Receive */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define TIMER0_A1_VECTOR        “.int08”                     /* 0xFFF0 Timer0)A CC1, TA0 */
#else
#define TIMER0_A1_VECTOR        (8 * 1u)                     /* 0xFFF0 Timer0)A CC1, TA0 */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define TIMER0_A0_VECTOR        “.int09”                     /* 0xFFF2 Timer0_A CC0 */
#else
#define TIMER0_A0_VECTOR        (9 * 1u)                     /* 0xFFF2 Timer0_A CC0 */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define WDT_VECTOR              “.int10”                     /* 0xFFF4 Watchdog Timer */
#else
#define WDT_VECTOR              (10 * 1u)                    /* 0xFFF4 Watchdog Timer */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define COMPARATORA_VECTOR      “.int11”                     /* 0xFFF6 Comparator A */
#else
#define COMPARATORA_VECTOR      (11 * 1u)                    /* 0xFFF6 Comparator A */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define TIMER1_A1_VECTOR        “.int12”                     /* 0xFFF8 Timer1_A CC1-4, TA1 */
#else
#define TIMER1_A1_VECTOR        (12 * 1u)                    /* 0xFFF8 Timer1_A CC1-4, TA1 */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define TIMER1_A0_VECTOR        “.int13”                     /* 0xFFFA Timer1_A CC0 */
#else
#define TIMER1_A0_VECTOR        (13 * 1u)                    /* 0xFFFA Timer1_A CC0 */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define NMI_VECTOR              “.int14”                     /* 0xFFFC Non-maskable */
#else
#define NMI_VECTOR              (14 * 1u)                    /* 0xFFFC Non-maskable */
#endif
#ifdef __ASM_HEADER__ /* Begin #defines for assembler */
#define RESET_VECTOR            “.reset”                     /* 0xFFFE Reset [Highest Priority] */
#else
#define RESET_VECTOR            (15 * 1u)                    /* 0xFFFE Reset [Highest Priority] */
#endif

/************************************************************
* End of Modules
************************************************************/

As you can see there is macro for each vector address of the controller. You have to use the macro, I tried using the “.int08” value but it gave error. If you find out any other way please share it with me. One can’t have enough knowledge.

So we covered the interrupt handling in C part. Now let’s see the LPM part.

LPM in C language

Well entering into LPM mode in assembly language is a piece of cake. Just set the required bits of SR and its done. When I first tried to enter into LPM in C language I wrote SR |= LPM3|GIE and I got an error. I remembered that in C we can’t use the registers directly. So I had to see an example project given in the MSP430ware. There I came across this BIS instruction equivalent function.

_BIS_SR(bits_to_be_set);

This is a function which will write BIS.W #bits_to_be_set,SR in the disassembly. So just write the bits you want to set in this function and you are ready to enter into LPM.

Example

_BIS_SR(LPM3_bits + GIE);

Program to demonstrate above concepts

/*
*  inception.c
*  Created on    : 24-Nov-2013 10:59:50 AM
*  Author        : Manpreet Singh Minhas
*  Website       : https://learningmsp430.wordpress.com
*
*  Description   : In this program we toggle the SMD LED1 after one 
*                  second delay.We use timer in up mode. I’ve enabled the TAIE so
*                  that we get an interrupt on roll over.Also we have to set GIE 
*                  bit in SR. And I’ve used LPM3 for this, since I need only the
*                  ACLK for this. (Note: The 32KHz crystal needs to be soldered 
*                  on the Launchpad.) 
*/

#include <msp430g2553.h>

void main(void)
{
WDTCTL = WDTPW|WDTHOLD;
P1DIR |= BIT0;
P1OUT |= BIT0;
TA0CCR0 = 0x8000;
TA0CTL = TASSEL_1|ID_0|MC_1|TAIE;

_BIS_SR(LPM3_bits + GIE);

}

#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A(void)
{
P1OUT ^= BIT0;
TA0CTL &= ~(TAIFG);
}

So I’ll be ending this post here. Hope you found it useful. Thank you for reading and as always you can ask me any doubts you have. This will help me gain new knowledge and perspective.

Leave a comment