Pages

Saturday, 12 October 2013

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.
energia-ide
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-edgerising-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.
111020131216
The above circuit diagram shows the connections.
Tutorial:
[youtube=http://www.youtube.com/watch?v=XBJZdlFqOoE&w=640&h=360]
Project :
[youtube=http://www.youtube.com/watch?v=o1PNTw96Wsc&w=640&h=360]

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

grace_logo
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):
http://www.youtube.com/watch?v=UaSGDAgVgoQ
The led video link:
http://www.youtube.com/watch?v=1bwC9MyvPBo

Code Composer Studio : Grace !!!

grace_logo
I recently learned about this wonderful graphical user interface(GUI) called Grace which is a part of the Code Composer Studio by Texas Instruments. I must tell you that the interface is simply amazing. I mean you can initialize all the peripherals, interrupt subroutines, interrupt handling, etc using this interface. I tried this 1 sec delay program using grace and was amazed at the fact that TI has literally make it a cake walk for programmers to initialize all peripherals and interrupts in c language. I like writing code in assembly language and for that matter even in c, but as you would come to know while programming in c you need to use some explicit functions defined by the compiler to write the various interrupt subroutines. So if you want to focus on whether your concept is correct or not and want to do testing then grace is a blessing for you. Once you know that your code is working using grace, you can anytime write the assembly language program( initializing all the peripherals using their command registers, handle the interrupt vector addresses and so on).
I'll be covering how to make a project using grace in CCS very soon.

About msp430 launchpad

MSP430G2 launchpad is a development board developed by Texas Instruments. The msp430g series is a series of micro-controllers. The cost of launchpad is $9.99. This is a one time investment. Once you have the launchpad, you'll open an entire new world for yourself. Texas Instruments launchpad contains the following hardware:
  • MSP-EXP430G2 LaunchPad
  • MSP430G2553 microcontroller (with demo code)
  • MSP430G2452 microcontroller
  • Mini-USB cable
  • Quick Start Guide
  • 32kHz external crystal
(Note: You can order the board from TI itself. The link is : http://www.ti.com/tool/msp-exp430g2 )MSP-EXP30G2.jpg
Texas provides free samples of these microcontrollers. So whenever you'll need a microcontroller, you can search their website and order free sample of whatever you like. This enables to divert you money into other electronic equipment that you require. All in all I encourage all of you people out there who like coding and controllers to get your hands on launchpad.
P.S: If you have any query regarding how to order these samples, feel free to contact me.

Led patterns using msp430g2231

led-patterns
This application is generation of led patterns using msp430g2231.
Components Required:
msp430g2231 (1)
1k resistors (8)
preset 4.7k (1)
rgb led 2pin (8)
berg wires (8/9)
toggle switch(1)
705009_RB_00_FB.EPS_1000
Theory:
In this application we utilize all 8 ports of the micro-controller. Each port is connected to one of the led's via current limiting resistors. Now all we need to do is set all the ports as output and then change the patterns by varying the port pins that are high/low. We are using code composer studio for this. The program is written in c. We can either use the launchpad directly for connections via breadboard or we can make a PCB for it. The advantage of PCB is that you can carry is with you without having to take the launchpad as well your laptop with you. Or you can build a shield/booster pack of led's like in the image shown below.
021020131203
Code:
#include
main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0XFF; // set all the ports as output ports
int x = 0X01;
long i=0;
int j=1;
int k=0;
int l=0;
for(;;)
{
for(k=1;k<=5;k++){
x=0x01;
for(j=1;j<=8;j++){
P1OUT = x;
x*=2;
for(i=0;i<=10000;i++)
{
}
}
}
for(k=1;k<=5;k++){
x=0x80;
for(j=1;j<=8;j++){
P1OUT = x;
x/=2;
for(i=0;i<=10000;i++)
{
}
}
}
for(k=1;k<=5;k++){
P1OUT=0xAA;
for(l=0;l<=1;l++){
for(i=0;i<=64000;i++)
{}}
P1OUT=0x55;
for(l=0;l<=1;l++){
for(i=0;i<=64000;i++)
{}}
}
for(k=1;k<=5;k++){
P1OUT=0x18;
for(i=0;i<=20000;i++)
{}
P1OUT=0x24;
for(i=0;i<=20000;i++)
{}
P1OUT=0x42;
for(i=0;i<=20000;i++)
{}
P1OUT=0x81;
for(i=0;i<=20000;i++)
{}
}
for(k=1;k<=5;k++){
P1OUT=0x81;
for(i=0;i<=20000;i++)
{}
P1OUT=0x42;
for(i=0;i<=20000;i++)
{}
P1OUT=0x24;
for(i=0;i<=20000;i++)
{}
P1OUT=0x18;
for(i=0;i<=20000;i++)
{}
}
}
}
My instructable link:
YouTube Video:
http://www.youtube.com/watch?v=g9NRDXL0vOk
Note: If anyone wants the PCB file, just let me know.

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.
Theory:
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??).
Code:
//Author: Manpreet Singh Minhas
//Date : 2/10/2013
//learningmsp430.wordpress.com
#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.)
for(i=0;i<20000;i++){
// some delay for the human eye to observe the change
}
}
}
Video:
http://youtu.be/0gJ3TLDVwQU
*: 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.

How to create a new project in CCS5.4

In this post I'm assuming you have already installed code composer studio after downloading it from http://processors.wiki.ti.com/index.php/Download_CCS. The steps involved are covered visually via images. The first step is to open the software by double clicking on its icon. Subsequent steps are shown below.
Step2
step3step4step5step6step7step8step9
Summary:
1) Open code composer studio.
2)Select new->project
3)Select CSS project in the dialogue box that appears
4) Since I am using msp430g2231 I have selected that ic from the list you can choose whichever ic you intend to use. Give the project a suitable na
me. You have to select an empty project with main.c else you'll have to manually add c file which is not that difficult but I won't be covering that in this list.
5)Make your project as active project by clicking on it in the project explorer.
6) Expand your current project by clicking on the small plus on the folder. Then double click on the main.c to open the file.
7) Now you are ready to start the c programming in main.c
8) After writing the code you can debug it. If there are no errors then you'll get an option to run/play the program.

Things one requires for starting with MSP430 programming

MSP430 Launchpad Development toolkit, laptop/pc, Code Composer Studio.
One can purchase the kit from  http://www.ti.com/ .You'll have to make an account on the website for the same. The software code composer studio is available from www.ti.com/launchpadwiki . You'll require license for ccs v5.
The Launchpad kit contains the experimenter board, microcontroller chip, connectors, USB cable.
MSP-EXP30G2usb-a-to-b-10ft-cable
Connect the USB cable to the corresponding ports on the laptop and experimental board. There is a preinstalled programme on our microcontroller. If the LED on the board blink then everything is ok. The pattern in which they blink depends on the program that is burnt on the microcontroller.