Archive

Archive for February, 2011

Using Serial Ports in MSP430

February 28th, 2011 No comments

Using Serial Ports in MSP430

The Serial Port in the MSP430 is one of the widely used components. The serial port can be used for communication with computer and with other devices supporting serial ports. Most of the instrumentation devices come with the Serial port capability.

Serial Port Transmitter

Let us write a program that will output English characters A to Z repeatedly on the transmitter Serial Port of the MSP430. If you hook up a Serial TTL to RS232 converter from your MSP430 hardware – you should be able to see these characters on the hyperterminal ( or any serial port terminal for that matter) of your computer. If you do not have a TTL to RS232 converter, you can monitor the bits going out of MSP430 on a oscilloscope.

//******************************************************************************
// Starred reviews MSP430F149 Programming Tutorial – Understanding Serial Port
//
// This starred reviews program Transmits Character A to Z on Hyperterminal at 9600-baud UART using
// USART0 and a 32.768 kHz crystal.
// Baud rate divider with 32.768 Khz XTAL
// @9600 = 32768Hz/9600 = 3.41 => UBR00 = 0x03; UBR01 = 0x03;
// //* An external 32.768KHz crystal is required on XIN XOUT *//
//
//The Diagram below has been taken by starredreviews from TI
//
// MSP430F149
// —————–
// /|\| XIN|-
// | | | 32.768kHz
// –|RST XOUT|-
// | |
// | P3.4|———–>
// | | 9600 – 8N1
// | P3.5|<———–
//
// starredreviews.com tutorial
//******************************************************************************

#include

void delay(unsigned int ms)
{
unsigned int i, j;
for (i = 0; i <= ms; i++)
{
for (j = 0; j<=255; j++);
}
}

void main(void)
{
unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT Code copied from starredreviews
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL0; // UCLK = ACLK
UBR00 = 0x03; // 32k/9600 – 3.41
UBR10 = 0x00; //
UMCTL0 = 0x4A; // Modulation
UCTL0 &= ~SWRST; // Initialize USART state machine

// Code copied from starredreviews
while (1)

{ for (i = 0x41; i <= 0x5A ; i++)
{
while (!(IFG1 & UTXIFG0));
TXBUF0 = i;
delay(100);
}
}
// Code copied from starredreviews
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/ interrupt
}

Copy paste this starredreviews code in your IAR embedded system and run the code. It is assumed that you connect a TTL to RS232 converter between P3.4 and the input ( Pin 2 ) of the DB9 serial port connector of your computer.

If you open the hyperterminal then you should see something like this on your Hyperterminal.

Categories: Uncategorized Tags:

Using Functions in MSP430

February 28th, 2011 No comments

Functions in MSP430 are not very different from the functions in normal C programming. Note that the variables that can be passed to the functions should be only those that can be allowed.

Functions Example in MSP430

The following complete example has a functions that implements a delay. The parameters to the function delay() is the approximate amount of the delay in milliseconds. The exact amount of the delay will depend upon the clock frequency of your hardware.

 

#include
unsigned int delay(unsigned int ms)
{
// i and j are Local variables
unsigned int i, j;
for (i = 0; i <= ms; i++)
{
for (j = 0; j<=255; j++)
{
}
}
return 0;

}

int main( void )
{

WDTCTL = WDTPW + WDTHOLD;// Stop watchdog timer
P5DIR |= (BIT5); // Set P5.5 as and Output Pin
while (1)
{
P5OUT |= (BIT5) ; // P5.5 High
delay(100);
P5OUT &= ~(BIT5) ; // P 5.5 Low
delay(100);
}
}

 

Copy paste this example and see its effect on oscilloscope or an LED ( if there is one hooked to the IO port in this example).

Categories: Uncategorized Tags:

MSP430 Tutorial – Using Interrupt

February 28th, 2011 1 comment

Most of the practical embedded software will use interrupts at some point of time. Interrupts are indispensible to almost all the commercial emdedded software. Believe me, if you will thank this tutorial, that it taught you about interrupts – one of the most widely used concepts in embedded programming.

Concept of Interrupt

To understand the concept of interrupts, consider the LED blinking code in the previous example. Assume that the delay in that example is kept substantially high, let us say, 5 seconds. When the delay is kept as high as 5 seconds, the program is stuck into the delay loop while excuting. This creates some problem and real issue in practice. Assume that we want our code to capture some real time event in those 5 seconds, for example, it may wants to capture a push button action. With this code, it is very likely that it will miss capturing the input button press event. The program is stuck in the delay loop while you pressed the input button. The problem can be solved using the interrupt buttons.

We “generate” and interrupt when there is a change in the level of the IO pins. The program will continue to execute normally, but, as soon as the button is pressed, the interrupt is generated. As soon as the interrupt is generated, the normal execution of the program halts. We write a code – as to what needs to be done once the interrupt occurs. The program completes the interrupt and comes back to the normal execution fo the program.

We also have timer interrupts in a fashio similar to the IO pin interrupt. In timer interrupt, an interrupt at defined amount of time. The program keeps executing normally. At interval defined by the timer interrupt parameter, it jumps to the interrupt service routine, completes the task and comes back.

In this particular tutorial, we will learn timer interrupt. Copy paste the following code in the IAR embedded system. As before, replace the msp430x14x.h and the IO pin corresponding to your hardware. In this particular example there is an LED connected to Port P5.0 and a push button is connected at Pin P1.0. Pin P5.0 is used as an output pin and Pin P1.0 is used as an input pin. If you have an LED connected to, for example P2.3, then replace statement P5DIR |= BIT0 with P2DIR |= BIT3 and similarly the P5OUT lines.

#include “msp430x14x.h”

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog
P5DIR |= 0x01; // Port P5.0 is output
CCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 60000;
TACTL = TASSEL_2 + MC_2; // SMCLK, contmode

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
}

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
P5OUT ^= 0x01; // Toggle P5.0
CCR0 += 60000; // Add Offset to CCR0
}

If we run the program freely, it will make the LED blink. The same result as our previous LED blinking program. But the approach is different. For a detailed description of CCTL0, CCIE, CCR0,TACTL, TASSEL_2 , MC_2, their assignments and values, please refer to Chapter 11 of slau049.pdf file from TI. It has details of timer and interrupt.

Categories: Uncategorized Tags:

MSP430 – Using a Port Pin as an Input Port

February 28th, 2011 No comments

So far we have been using the Port Pins of the MSP430 only as an Output pin. In this section we will use a Port Pin as an input Pin. We will use Port Pin P1.0 as an Input Pin, in this particular example, but you may similarly use other port pins.

Imagine that a push button connected to P1.0. Assume that the MSP430 hardware is designed such that the Input to the Pin p1.0 in normally 0, and, when the button is pressed, the Pin becomes 1. We now need to write a code that will blink the LED, while the push button is kept pressed. When the push button is not pressed, there is no LED blinking.

Copy paste the code given below in the IAR embedded system. As earlier, replace the msp430x14x.h and the IO pin corresponding to your hardware. In this particular example there is an LED connected to Port P5.0 and a push button is connected at Pin P1.0. Pin P5.0 is used as an output pin and Pin P1.0 is used as an input pin. If you have an LED connected to, for example P2.3, then replace statement P5DIR |= BIT0 with P2DIR |= BIT3 and similarly the P5OUT lines.

#include
unsigned int delay ( unsigned int x)
{
unsigned int i,j;
for (i = 0; i<= x; i++)
{
for(j=0;j<=1000; j++)
;
}
return 0;
}

int main( void )
{

WDTCTL = WDTPW + WDTHOLD;

// Assign P5.0 as the output pin
P5DIR |= BIT0;

//Assign P1.0 as Input Pin
P1DIR&=~BIT0;

while(true)
{
if ( (P1IN & BIT0) == 0x01)
{
P5OUT|= BIT0 ; // P5.0 High
delay(100);
P5OUT&=~BIT0; // P5.0 Low
delay(100);
}
else
{
P5OUT|= BIT0 ; // P5.0 High
delay(100);
}
}

return 0;
}

If we run the program freely by hitting F5, it will make the LED blink if you keep the pin P1.0 high be pressing the key.

To define a pin to be used as an Input pin we use the following statement.

P1DIR&=~BIT0; //P1.0 is used as Input Pin

To completely understand this statement, you will need to go to the msp430x14x.h and search for P1DIR. You will notice that P1DIR corresponds to memory location 0x0022. If you want to read the deatils you may like to open the document slau049f.pdf from Texas Instruments and read the details of this memory location.

If you would like to get an in depth understanding of the memory map of MSP430 you may like to refer to the MSP430 datasheets to learn about the memory maps.

Categories: Uncategorized Tags:

MSP430 Tutorial – LED Blinking Program

February 28th, 2011 No comments

We will extend what we have learned so far to write something visibly interesting. Let us write a program that will make an LED hooked to an IO port of MSP430 blink. We will need to write a simple delay function. The main program will call the delay function between LED on and LED off.

Go ahead and paste the following code in the IAR embedded system. As before, replace the msp430x14x.h and the IO pin corresponding to your hardware. In this particular example there is an LED connected to Port P5.0. If you have an LED connected to, for example P2.3, then replace statement P5DIR |= BIT0 with P2DIR |= BIT3 and similarly the P5OUT lines.

#include
// Delay function
unsigned int delay ( unsigned int x)
{
unsigned int i,j;
for (i = 0; i<= x; i++)
{
for(j=0;j<=1000; j++)
;
}
return 0;
}

int main( void )
{

WDTCTL = WDTPW + WDTHOLD;

// Making P5.0 to as output port. Change as required
P5DIR |= BIT0;
while(true)
{
P5OUT|= BIT0 ; // P5.0 High
delay(100);
P5OUT&=~BIT0; // P5.0 Low
delay(100);
}

return 0;
}

If we run this program freely ( after compiling hit F5), it will make the LED go on and off in an infinite loop. You may change the duration of the blink by changing the parameter of the delay(100).

In the next section we will see how to use a port pin of MSP430 as an input pin.

Categories: Uncategorized Tags: