Home > Uncategorized > Serial Ports in MSP430

Serial Ports in MSP430

February 28th, 2011 Leave a comment Go to comments

Let us extend the example in the previous tutorial, which sent out characters from A to Z. In this tutorial we will do the reverse. A character will be sent out from the hyperterminal of your PC to the MSP430 at 9600 baud rate ( No Parity and one stop bit) and the MSP430 will echo the character back. The Echoed character can be read at the serial port.

MSP430 Serial Port Echo Program

#include <io430x14x.h>

//******************************************************************************
//  MSP-FET430F149  USART0, UART 9600
//
//  Starredreviews Description: Echo a received character, RX ISR used. Normal mode is LPM0,
//  USART0 RX interrupt triggers TX Echo. Though not required, MCLK = LFXT1
//  ACLK = MCLK = UCLK0 = LFXT1 = 3.58MHz
//  Baud rate divider with 3.58Mhz XTAL @9600 = 3.58MHz/9600 = 372 (0174h)
//  //* An external 3.58Mhz XTAL on XIN XOUT is required for ACLK *//
//
//                MSP430F149
//             —————–
//         /|\|              XIN|-
//          | |                 | 3.58MHz
//          –|RST          XOUT|-
//            |                 |
//            |             P3.4|————>
//            |                 | 9600 – 8N1
//            |             P3.5|<————
//
//
// starredreviews.com tutorial
//******************************************************************************

void main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW + WDTHOLD;                 // Stop Watch Dog
P3SEL |= 0x30;                            // P3.4,5 = USART0 TXD/RXD
BCSCTL1 |= XTS;                           // ACLK = LFXT1 = HF XTAL

// Starredreviews.com codes

do
{
IFG1 &= ~OFIFG;                           // Clear OSCFault flag
for (i = 0xFF; i > 0; i–);               // Time for flag to set
}
while ((IFG1 & OFIFG));                   // OSCFault flag still set?

BCSCTL2 |= SELM_3;                        // MCLK = LFXT1 (safe)
ME1 |= UTXE0 + URXE0;                     // Enable USART0 TXD/RXD
UCTL0 |= CHAR;                            // 8-bit character
UTCTL0 |= SSEL0;                          // UCLK = ACLK
UBR00 = 0x74;                             // 3.58Mhz/9600 – 372
UBR10 = 0x01;                             //
UMCTL0 = 0x00;                            // no modulation
UCTL0 &= ~SWRST;                          // Initialize USART state machine
IE1 |= URXIE0;                            // Enable USART0 RX interrupt

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

#pragma vector=USART0RX_VECTOR
__interrupt void usart0_rx (void)
{
while (!(IFG1 & UTXIFG0));                // USART0 TX buffer ready?
TXBUF0 = RXBUF0;                          // RXBUF0 to TXBUF0
}

Copy paste this starredreviews.com code in your IAR embedder system and run the code. Whatever you write on the Hyperterminal in echoed back by the MSP430.

Some easy exercise for you- change the program so that it writes the next letter – for example if you write A – it should echo B.

Categories: Uncategorized Tags:
  1. No comments yet.