MSP430 GPIO Programming Reading an external switch

To read an external switch using MSP430 an external switch is connected to P1.4…We uses polling method here to read the switch

/*button_press.c
ganeshredcobra@gmail.com
GPL
An external button connected to P1.4 is used in this example...
connect P1.4 to vcc through a resistor...
*/
#include 

volatile unsigned int i;//to prevent optimization
void main(void)
{
	WDTCTL=WDTPW+WDTHOLD;
 	P1DIR|=0X01;//set all bits in P1 to input except BIT0
	for(;;)
	{
	  if((P1IN & BIT4)==0)//while pushing S1
		P1OUT=0X00;//LED ON
	  else
		P1OUT=0x01;//LED OFF		 
	}
}

When we pushes the button LED will be ON and when we releases it will be OFF…
Happy Hacking 🙂

Programming MSP430 on board button

To get user input or to make a switch working in MSP430 Launchpad first we will try programming the on board button P1.3 to read user inputs

Connect P1.3 pin to Vcc through a resistor…Thecode to get user input is below

/*button_press.c
ganeshredcobra@gmail.com
GPL
on board button S2 is used in this example...
connect P1.3 to vcc through a resistor...
*/
#include <msp430g2553.h>

volatile unsigned int i;//to prevent optimization
void main(void)
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR|=0X01;//set all bits in P1 to input except BIT0
for(;;)
{
P1OUT=0x01;//BIT 0 LED ON
while((P1IN & BIT3)==0)//while pusing S1
P1OUT=0X00;//LED OFF
}
}

The above code sets the red led on whenever we pushes the button the led is turned off….The code is self explanatory with comments…

This is an example of polling…

Happy Hacking 🙂

How to setup MSP430 Launchpad in ubuntu

 

 

 

 

 

 

 

 

This will help you to set up MSP430 Launchpad in ubuntu…TI provides support only for windows.we will setup the toolchain,debugger and test ablink led c program.

First install the dependencies needed

$sudo apt-get install git-core gcc-4.4 texinfo patch libncurses5-dev
$ sudo apt-get install zlibc zlib1g-dev libx11-dev libusb-dev libreadline6-dev

Now download mspgcc compiler

git clone git://mspgcc4.git.sourceforge.net/gitroot/mspgcc4/mspgcc4
cd mspgcc4
sudo sh buildgcc.sh

The build script will ask a number of questions.  Accept the default by pressing Enter for each question except “Do you want to start build right now?”  The proper response here is of course “yes”!

Download and install mspdebug the debugger

cd ..
git clone git://mspdebug.git.sourceforge.net/gitroot/mspdebug/mspdebug
cd mspdebug
make
sudo make install

Now make a blink.c program

/* Demo app to blink the red LED on the TI Launchpad */
#include <msp430x24x.h>
int main(void) {
 volatile int i;
// stop watchdog timer
 WDTCTL = WDTPW | WDTHOLD;
 // set up bit 0 of P1 as output
 P2DIR = 0x01;
 // intialize bit 0 of P1 to 0
 P2OUT = 0x00;
// loop forever
 for (;;) {
 // toggle bit 0 of P1
 P2OUT ^= 0x01;
 // delay for a while
 for (i = 0; i < 0x6000; i++);
 }
}

Now compile it

$ /opt/msp430-gcc-4.4.5/bin/msp430-gcc -oS -o blink.elf blink.c

Now we generated the elf or executable.use mspdebug to download the program to the board

$ sudo mspdebug rf2500
(mspdebug) prog blink.elf
Erasing...
Programming...
Writing 104 bytes to fc00...
Writing  32 bytes to ffe0...
(mspdebug) run
Running. Press Ctrl+C to interrupt...

The red LED will be blinking right now…