/****************************************************************** ; ; Description: MSP430F149 serial link example for CPE496 ; Date : 02/14/2005 ; Auth : Zexin Pan ; ******************************************************************/ #include "msp430x14x.h" /* uncomment this to make UART RX interrupt driven */ //#define UART0_INT_DRIVEN_RX unsigned char thr_char; /* hold char from UART RX*/ unsigned char rx_flag; /* receiver rx status flag */ // universal delay function void Delay(void); // global clock configuration void SpeedUpClock(void); // UART0 rx int. interrupt [UART0RX_VECTOR] void UART0_RX_interrupt(void); // UART Initializaion void UART_Initialize(void); //send char function void UART0_putchar(char c); //receive char function char UART0_getchar(void); void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer SpeedUpClock(); UART_Initialize(); #ifdef UART0_INT_DRIVEN_RX rx_flag=0x00; /* rx default state "empty" */ #endif _EINT(); // enable global interrupts #ifdef UART0_INT_DRIVEN_RX while(!(rx_flag&0x01)); /* wait until receive the character from HyperTerminal*/ #else thr_char=UART0_getchar(); #endif // send a message UART0_putchar('C'); UART0_putchar('P'); UART0_putchar('E'); UART0_putchar('4'); UART0_putchar('9'); UART0_putchar('6'); while(1) { Delay(); UART0_putchar(thr_char); // send the char we just received! } } void SpeedUpClock(void) { IFG2 = 0; IFG1 = 0; //using a 8MHz clock from LFXT1, and ACLK=LFXT1 BCSCTL1 |= XTS; /*wait in loop until crystal stabalizes*/ do { IFG1 &= ~OFIFG; } while (OFIFG & IFG1); Delay(); //Reset osc. fault flag again IFG1 &= ~OFIFG; } void Delay(void) { volatile unsigned int delay_count; for(delay_count=0xffff;delay_count!=0;delay_count--); } void UART_Initialize(void) { //////////////////////////////////////////////////////////////// // UART0 Initialization //////////////////////////////////////////////////////////////// // UART0 - 38400 bps //IMPORTANT NOTICE: the following configuration works only for 8MHZ clock rate // AND 38400 bps baud rate //you have to recalculate these values if the cpu clock rate or baud rate has changed U0MCTL = 146; U0BR0 = 208; U0BR1 = 0; P3SEL |= 0x30; // bits 4-5 are for special function UART0 ME1 |= URXE0 + UTXE0; // UART module enable U0CTL = CHAR; /* 8-bit characters */ U0TCTL = SSEL0; /* clock source ACLK, 8MHZ */ U0RCTL = 0; #ifdef UART0_INT_DRIVEN_RX IE1 |= URXIE0; // enable RX interrupts #endif } //end UART_Initialize() interrupt [UART0RX_VECTOR] void UART0_RX_interrupt(void) { thr_char = U0RXBUF; rx_flag=0x01; // signal main function receiving a char } void UART0_putchar(char c) { // wait for other character to transmit while (!(IFG1 & UTXIFG0)); U0TXBUF = c; } char UART0_getchar(void) { for (;;) { if (U0RCTL & RXERR) { // if you want to answer receive errors, do it here. U0RCTL &= ~(FE+PE+OE+BRK+RXERR); } else { if (IFG1 & URXIFG0) { // we've received a character return U0RXBUF; } } } }