Timer A up mode and polling concept

In this post I’ve tried to use the Timer A peripheral of msp430 to give delay in up mode. This time I am writing about the mode rather than just initializing using grace. Timer A module of msp430 is a 16 bit counter/timer. It has various modes of operation. They are listed below:

Timer-modesWe’ll be using up mode for this activity. Up mode counts from 0 to TACCR0 as mentioned in the mode description. TACCR0 is a 16 bit register which is used for the mode. You can give any value from 0 to 0FFFFh.

Now we want 1 sec delay so we’ll be using the 32KHz crystal. That is why we’ll give the Timer A that input. For one second delay we need to count till 32768. That is why we must give 08000h as the word into the TACCR0 register.


(This pic is taken from the data sheet of msp430x2xx)

Now we’ll see how to initialize the control register of Timer A. TACTL is a 16 bit register with controlling bits as shown in the image. TASSELx is used to select the clock. Since we need to use ACLK we have write 01 in TASSELx location. IDx is used to divide the frequency. Since I don’t want any division I’ll put 00 in IDx. MCx is used for selecting the mode. We want up mode so we need to put 01. TAIFG flag is used to check when 1 sec has passed. TAIFG flag should be reset first. Now when ever a roll over takes place i.e the counter reaches the maximum count in this case TACCR0 count we’ll get an interrupt. But since we haven’t enabled the interrupt by setting TAIE the program won’t go to the ISR instead will just set the TAIFG flag and make it one. Now its the duty of the programmer to manually clear this flag, since once set it’ll remain set till values are not explicitly changed.

Now I’ll tell you how exactly to send the bits that we discussed into the TACTL register. You can either use the hexadecimal command word or else use the msp430.h header file defined bits for this.

First we’ll see the good old hex word formation.

First 15-10 are unused bits do they are don’t care conditions, thus their being set or reset makes no difference to Timer A working. So I’ll make these bits reset by 000000b

TASSELx i.e bits 9 and 8. We want mode one so 01b

IDx i.e bits 7 and 6. We want no division so 00b

MCx i.e bits 5 and 4. We want up mode so 01b

Bit 3 unused => don’t care => we’ll use 0b

TACLR i.e bit 2 0b

TAIE i.e bit 1. We don’t want interrupt so 0b

TAIFG i.e bit 0 has to be reset so 0b

Now we’ll write all the binary values together 000000 01 00 01 0 0 0 0.

The hex value after conversion comes out to be 0110h. So this this the hex value that we need to write in the command word TATCL to initialize the timer A in the manner that we require for this task at hand.

Now we’ll see the better approach i.e using the header file constants.

Each and every bit field of the TACTL register is defined in the header file.

I’ll give one example to clear the usage.

#define MC_0 (00x10u) / Timer A mode control: 0 – Stop /
#define MC_1 (1
0x10u) /* Timer A mode control: 1 – Up to CCR0 /
#define MC_2 (2
0x10u) /* Timer A mode control: 2 – Continous up /
#define MC_3 (3
0x10u) /* Timer A mode control: 3 – Up/Down */

Now these above lines are copy pasted from the msp430g2553.h header file.  Now the ‘u’ in this 0x10u stands for unsigned and 0x implies that the value is in hexadecimal number. I’ll write the binary equivalent of these values.

0*0x10u = 000000 (all bits to left of 6th bit are reset)

1*0x10u = 010000 (all bits to left of 6th bit are reset)

2*0x10u = 100000 (all bits to left of 6th bit are reset)

3*0x10u = 110000 (all bits to left of 6th bit are reset)

Thus you can clearly see that when you or this predefined values with the command word only the bits desired for the mode are set. This is the case with all the other constants defined in the header file. One thing to notice is the nomenclature MC_1 and others. So the underscore number is the decimal equivalent of the mode bits given in binary. With background lets initialize this timer, shall we?

We want the ACLK so TASSEL_1, no division is needed so ID_0, up mode so MC_1, we don’t want timer interrupt to change the program flow so we won’t set that bit. That’s all there is to it.

In c language we’ll or this value with the timer command word register. I’ll be using timer 0 so TA0CTL is used.


This takes care of the initialization of the timer A.

Now we’ll see what polling is. I’ll give to two examples. First example: Suppose my sister has gone to the market to get food materials and I’m in my room writing something. Now I know she’ll be coming and I need to open the door for her. There are two approaches I can adopt. First is I would check after every 5 mins(fixed interval) whether she has come or not. In this case I’ll have to check this many times depending upon when will the event of her coming back occurs. The other approach is that my door has a bell so might as well make constructive use of it by letting it do whats its supposed to do. I’ll keep on writing and when I hear the door bell I’ll complete the sentence that I am writing and then attend the door. Example two: Suppose I’m eating gol gappe (indian dish) and then the person who is serving me those can do two things. One is that he’ll keep one piece in my plate ask me then if i say yes he’ll put another and so on. Second is keep the gol gappes in my plate one after the other till I say my stomach is full no more. In both the examples the first case is nothing but polling. You check the status of something after regular intervals of time till you finally detect the occurrence of the event being polled. This is a normal procedure for checking the status of interrupt or maybe the position of a switch and so on. The second case is kind of interrupt. When you get an interrupt you know that the event has occurred and you do whatever is required to do. In technical language you execute the ISR. But then there is the context saving followed by context switching and then normal program resume. This was a little talk about polling. Hope you understand what polling is now.

We’ll be using polling to find out when overflow occurs. Overflow is when the counter reaches the maximum count and reloads the initial condition. In our case when the timer has counted 32k times and rolls over we’ll get exactly 1 sec delay. This overflow event is marked by the setting of the interrupt overflow flag. The setting is done by the processor as soon as the count reaches the CCR0 value.


The TAIFG flag setting can be seen in the image shown above. This is directly pasted from the msp430 manual. Now we’ll come to know when the required delay has occurred. The next thing would be to reset the flag so that it can be set again on the next roll over. This is taken care in the program. And last but not the least, in order to do something after the overflow has occurred I’ll be showing 0 to 9 on a common cathode seven segment display increasing the value by 1 after each roll over and repeating the process. Again the main motive was to use timer A in up mode with our choice of delay.  Hope you understood what I tried to convey. Again thank you for reading this post. Your feedback will remain priceless like always.

P.S : If you have any doubt about how I got those hex values for ports for each and every number feel free to contact me.

Circuit Connection:

circuit connections

Program Code:

#include <msp430g2553.h>
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
TA0CCR0 = 0x8000;                           // load count for 1 sec delay
TA0CTL |= TASSEL_1|ID_0|MC_1|TACLR; // set timer A in up mode
P1OUT = 0x00;
P1DIR = 0xFF;                 // make all ports as output

P1OUT = 0x3F; // display 0
P1OUT = 0x06; TA0CTL&=~(TAIFG); // display 1 and clear the interrupt flag
P1OUT = 0x5B; TA0CTL&=~(TAIFG);
P1OUT = 0x4F; TA0CTL&=~(TAIFG);
P1OUT = 0x66; TA0CTL&=~(TAIFG);
P1OUT = 0x6D; TA0CTL&=~(TAIFG);
P1OUT = 0x7C; TA0CTL&=~(TAIFG);
P1OUT = 0x07; TA0CTL&=~(TAIFG);
P1OUT = 0x7F;TA0CTL&=~(TAIFG);
P1OUT = 0x67;TA0CTL&=~(TAIFG);

Using Grace:


Testing individual segments of a 7 Segment display

Today I took one 7 segment display from my college lab. While I’ve used these before, I saw this after a long time. Now I was trying to remember how to operate it. The one that I got was common cathode type. So in this cathode is common. Now cathode is the negative terminal of 7 segment display and needs to be grounded. So logically if you want any segment to glow you need to supply Vcc. But 7 segment display is nothing more than a group of led’s arranged in specific order and of particular shape. So like any ordinary led’s it will definitely burn if the current exceeds the rated value. That is why we normally use current limiting resistors. But testing using that set up is tedious. So we’ll learn how to test the segments using a multimeter.

One more thing, the printed code on seven segment display will have a CC mark for common cathode and A for common anode.

Above figure shows both the variants of 7 segment displays i.e the common anode and common cathode.

Steps involved in testing:

1) Keep the multimeter in diode mode.

2) Now depending upon which variant you have you’ll have to make connections.testing-diode-using-digital-multimeter

For common cathode place the negative probe of multimeter on the centre pin and then place the positive probe on the segment pin that you want to test.

For common anode place the positive probe of multimeter on the centre pin and then place the negative probe on the segment pin that you want to test.

Hope this tip will be useful.

Youtube link:

Energia + MSP430 = Arduino!!! in PDIP :D

EnergiaHello everyone. This post is regarding an IDE called Energia, for msp430 launchpad. There are millions of arduino fans out there, who love the inbuilt libraries and functions for making projects easily. Hobbyists have the natural tendency to use arduino for their projects. This is justified by the large number of  *shields that are available for arduino and the awesome arduino community.  This is a good news for them as Energia enables coding msp430 microcontrollers using the arduino libraries and functions. In fact Energia has inbuilt example ‘sketches’ just like the arduino IDE which you can build and burn in the controller.


Yes the blink led sketch and all others, you can directly set the microcontroller you want to use and get the fun started. Energia is an open source project just like arduino. It was launched in 2012 i.e last year. There have been a few updated to improve the stability. The color of Energia is red (launchpad!!) unlike arduino which is blue. Now to talk about the code efficiency and the IDE stability. Since it uses another processors IDE libraries and functions and converts it into those which are compatible with our launchpad controllers, it is slower as compared to CSS,IAR and other development environments. It may seem to test your patience, but then you are getting best of both worlds right? So one shouldn’t complain about it. So now you have various methods to reduce your coding time. If you want to interface an 16x2LCD just import the lcd library and use the inbuilt functions to perform the tasks that you require. So if a LCD is like ABCD.. type basic thing in your project you can avoid initializing those peripherals manually via control signals and command words. Makes our life easy, isn’t it?

This is the link to the Energia website if you are interested.


Here you can download the latest Energia version. There is help page as most sites have. The most important advantage of Energia is that you’ll open up the gate to a humongous community i.e arduino community.

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

* Shield refers to a hardware that performs a specific set of tasks and can be mounted onto the arduino board. On that note I’ll like to introduce the booster packs of launchpad. Booster pack is the launchpad version of the name shield.

Hardware Interrupts it is!!!!!! ;)

interrrupt_logoIn this project we’ll be dealing with the hardware interrupt functionality of msp430. An interrupt is a specific signal that the processor identifies. Interrupt triggering can be either rising edge or falling edge or level triggered. falling-edge rising-edge

So whenever an interrupt of desired from occurs, it triggers the processor. Just a brief idea about Interrupts. Whenever an interrupt occurs the processor completes the task at hand, then context saving takes place and then context switching. Context saving is nothing but the processor stores the current content of the program counter to the stack. Stack is like a scratch pad where you write data whenever you require. Only difference is that this stack cleans up once you pop the data or take the data back. The vector address (which is nothing but an address that is a hex number) is loaded into the program counter. Each interrupt has a unique vector address for interrupt subroutine(ISR). Now this ISR is executed and once it executes the return instruction(has to be written by programmer in ISR) it goes back into the main program. This is done by taking back the address of the program counter that was stored onto the stack.

Now we’ll talk about msp430 and hardware interrupts. In msp430 you can configure each and every pin of all ports as interrupt enabled. So one can have various interrupts and various parts in ISR based on that. For port 0 there is one vector address and for port 1 and so on. So irrespective of which pin the interrupt occurs the control will go to the same ISR. In order to distinguish which port pin has triggered the processor there is a flag register in msp430 called PxIFG which is port x interrupt flag. Each bit corresponds to a the port pin of that register. So whenever there is an interrupt the m pin of port x then the mth bit of PxIFG goes high. We have to clear that bit ourselves, compiler won’t do that for us.

Now coming back to the project, in this project we’ll write the led pattern code in the main program so that there are some patterns in normal state. We’ll be using 6 push buttons to give interrupts. And we have programmed the controller in such a way that led corresponding to the port pin that gives interrupt will glow. So if P2.3 gives the interrupt I’ve programmed this such that P1.3 led will glow(same port pin number just for tracking purpose.)

Note you can easily vary this program to write a code to display the pin number on a 7- segment display. All you need is a BCD to seven segment decoder and a seven segment display. I got this idea after returning home when I had purchased the push buttons. I really didn’t want to go to the shop so I stuck to this led concept.

Hope you gained some knowledge about the wonderful world of interrupts. This is a really important part of any controller and how a programmer writes code for this is also important. Thank you for reading this blog. Please do come again for some new concepts or entries.

P.S: For a couple of weeks I’ll be using grace for initializing all peripherals. Later I’ll be covering assembly language.


The above circuit diagram shows the connections.


Project :

1sec delay using 32Khz crystal and Grace!!!


I tried to experiment with a video tutorial this time. So I won’t be like writing the steps down but instead will show you how this grace magic works visually.

Something to know about the 32.768 KHz crystal that came along with your launchpad. You might be wondering why this crystal and upon that why this precise value?? The answer is simple we need external crystal to give precise clock pulses (i.e heart beats of your processor). There is an inbuilt digital controlled oscillator in msp430, but then this does not give frequency stable enough for precise operations for example a real time clock. Now regarding the 32768 Hz value. 65536 is a number that makes sense to most people who have experience with memories. Maximum 16bit data is 0FFFFh or 11111111b or 2^16 i.e 65532d and exactly half of that is 32768. So if you count every pulse generated by this crystal (counter action using Timer A), after you’ve reached 32768 counts you would have waited 1 sec exactly. Isn’t this really amazing?? Now that you have 1 sec, I don’t think there is any need for me to tell you the importance of 1 sec right??  Anyways one of the most basic and fundamental operation of this is in real tune clock(RTC) chips.

So in this program we’ve tried to see this one second delay using whatever resources we have on launchpad i.e LED1 and LED2. We’ll be toggling the led’s exactly after 1 sec.

And we should not forget the main motto i.e learning grace. So we’ll use grace to create this project. Hope this post was informative and useful for you. Thank you for reading this.

The tutorial link is(I’ll try to be more clear next time :D):

The led video link: