Sometimes we often face situations like lesser GPIO pins when working on “peripheral optimized” microcontrollers like MSP430 which offer a perfect balance of cost to feature ratio. The MSP430G2231 which comes with the older version of launchpad (v 1.4) has 8 I/O on Port1 and the datasheet also says that the crystal pins(XIN and XOUT) can be used as I/O with an interrupt capability but there was a lot of ambiguity on how to actually use these pins as I/O.

CrystalIOPIC1

Even the user`s guide which is very lucid and clear on all peripherals does not mention the way to use it. After failed searches over the web for the correct way to use them..I started experimenting with the launchpad to find it out.

I figured out that by default the pins for the crystal are initialized in the ” Reserved ” mode as mentioned in the user guide:
CrystalIOPIC2

Its just a simple job but it took me sometime to figure it out.. 😀
So, now all you need to do is clear the PxSEL2 bit and what you get in return is a generous gift of two extra I/O`s. This is “critically” useful while interfacing 7segs using standard multiplexing techniques .
The code is given here as follows:

/*
Using Crystal Pins as IO
Microcontroller: MSP430G2231
author: Rohit Gupta
Email:  rohit7gupta [at] gmail [dot] com
Date:   14/1/2013
Homepage:www.indiantinker.wordpress.com

Compiler: Code Composer Studio on Windows 7 64bit Machine
Copyright (C) <2013>  <Rohit Gupta>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include<msp430g2231.h>
void delay_ms(unsigned int ms )
{
      unsigned int i;
      for (i = 0; i<= ms; i++)
        __delay_cycles(1000); //Built-in function that suspends the execution for 1000 cycles
}
void main(void)
{
	WDTCTL = WDTPW + WDTHOLD; //Stop the watchdog..bow..boww
	P2SEL&=~(1<<7); //Clear the SEL bit to enable use as a IO pin
    
	P2DIR|=(1<<7); //Output at pin2.7
        P2OUT&=~(1<<7); //Turn On LED on Pin 2.7
	while(1)
	{
		P2OUT|=(1<<7);
		delay_ms(500);
		P2OUT&=~(1<<7);
		delay_ms(500);
	}


	
}

They youtube video is being uploaded to see this in action.

So, Go ahead and grab those extra I/O`s being left unused :D.

Happy New Year 2013!

Cheers,
Indian Tinker

Advertisements

4 thoughts on “Tutorial: Using Crystal Pins as I/O on MSP430

  1. Amazing! Love it, thanks for the help. And by the way amazing blog, a lot of information is useful to successfully develop something. I am working in a watch, thanks to you I am just 10 hours of work closer to finish.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s