代码一:
#include <msp430x14x.h>
void int_clk()
{
BCSCTL1 &= ~XT2OFF;
BCSCTL2 |= SELM_2 + SELS;
do
{
IFG1 &= ~OFIFG;
for(int i=;i<;i++)
_NOP();
}while((IFG1 & OFIFG)!=);
IFG1 &= ~OFIFG;
}
void int_usart()
{
U0CTL |= SWRST; //复位串口
U0CTL |= CHAR; //8位数据
U0TCTL |= SSEL1; //选择SMCLK
//波特率19200
U0BR1 = 0X01;
U0BR0 = 0XA0;
UMCTL0 = 0XC0; ME1 |= UTXE0 + URXE0; //使能接收和发送
U0CTL &= ~SWRST; //清楚串口复位信号 IE1 |= URXIE0; //使能接收中断
P3SEL |= BIT4 + BIT5;
P3DIR |= BIT4;
}
void sent_byte(unsigned char data)
{
while((IFG1 & UTXIFG0)==);
U0TXBUF = data;
}
#pragma vector=UART0RX_VECTOR
__interrupt void UART0(void)
{
unsigned char data = ;
data = U0RXBUF;
sent_byte(data);
}
int main()
{
WDTCTL = WDTPW + WDTHOLD;
int_clk();
int_usart();
_EINT();
return ;
}
代码二:
#include <msp430x14x.h>
void int_clk()
{
BCSCTL1 &= XT2OFF;
BCSCTL2 |= SELS + SELM_2;
do
{
IFG1 &= ~OFIFG;
for(int i=;i<;i++)
_NOP();
}while((IFG1 & OFIFG)!=);
IFG1 &= ~OFIFG;
}
void int_usart()
{
U0CTL |= SWRST;
U0CTL |= CHAR;
U0TCTL = SSEL1;
//
U0BR1 = 0X03;
U0BR0 = 0X41;
U0MCTL = 0X00; ME1 |= UTXE0 + URXE0;
U0CTL &= ~SWRST;
IE1 |= URXIE0; P3SEL |= BIT4 + BIT5;
P3DIR |= BIT4;
}
void send_byte(unsigned char data)
{
while((IFG1 & UTXIFG0)==);
U0TXBUF = data;
}
//串口0发送字符串函数
/*void Print_Str(char *s)
{
while(*s != '\0')
{
send_byte(*s++);
}
}
*/
#pragma vector=UART0RX_VECTOR
__interrupt void UART0_RX_ISR(void)
{
unsigned char data;
data = U0RXBUF;
P6OUT = data;
send_byte(data);
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
P6DIR = 0XFF;
P6OUT = 0XFF;
int_clk();
int_usart();
_EINT();
// Print_Str("hello!\n");
}